Skip to content

Commit

Permalink
deployment: Dockerize PostGIS Service with GADM File-Based DB Initial…
Browse files Browse the repository at this point in the history
…ization.

This setup automates the process of creating a new PostGIS database based on a
specified GADM file. It allows for running a Docker container equipped with
PostGIS and GDAL.

- Add Dockerfile to create a PostGIS image, complete with GDAL and
  initialization scripts
- Introduce docker-compose.yml for easy orchestration of the PostGIS service
- Implement Makefile for simplified build, run, and clean operations
- Provide an example .env file for setting PostgreSQL and GADM file environment
  variables
- Include init-db.sh, a script to initialize the PostGIS database and import
  GADM file data

Signed-off-by: Nikolay Martyanov <ohmspectator@gmail.com>
  • Loading branch information
OhmSpectator committed Oct 3, 2023
1 parent c42edc4 commit bd0ef62
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
7 changes: 7 additions & 0 deletions deployment/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Environment variables for PostgreSQL
POSTGRES_DB=<set_your_db_name>
POSTGRES_USER=<set_your_db_user>
POSTGRES_PASSWORD=<set_your_db_password>

# GADM gpkg file
GADM_FILE=<set_path_to_your_gadm_file>
29 changes: 29 additions & 0 deletions deployment/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM postgis/postgis:latest

# Read the .env file
ARG POSTGRES_DB
ARG POSTGRES_USER
ARG POSTGRES_PASSWORD
ARG GADM_FILE

# Copy initialization scripts and GPKG file
COPY ./init-db.sh /docker-entrypoint-initdb.d/init-db.sh

# Copy the GADM file to the container
COPY $GADM_FILE /tmp/gadm.gpkg

# Make the init script executable
RUN chmod +x /docker-entrypoint-initdb.d/init-db.sh

# Install GDAL
RUN apt-get update && apt-get install -y gdal-bin

# Run PostgreSQL on default port
EXPOSE 5432

# Use the .env file to set the environment variables
ENV POSTGRES_DB=$POSTGRES_DB
ENV POSTGRES_USER=$POSTGRES_USER
ENV POSTGRES_PASSWORD=$POSTGRES_PASSWORD

CMD ["postgres"]
39 changes: 39 additions & 0 deletions deployment/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Name of the Docker Compose project
PROJECT_NAME = tyr_postgis

# Build the Docker image using docker-compose
build:
docker-compose build

# Run the Docker container using docker-compose and forward the PostgreSQL port
run:
docker-compose up -d

# Initialize the PostGIS database and import GPKG file
init-db: build run
@echo "Database initialized and GPKG file imported."

# Forcefully re-initialize the database
reinit-db: clean-volume init-db
@echo "Database forcefully re-initialized."

# Stop and remove the Docker container
clean-container:
docker-compose down

clean-volume:
docker-compose down -v

# Remove the Docker image
clean-image:
docker-compose down --rmi all

# Remove the Docker container and image
clean-all: clean-container clean-image clean-volume

stop:
docker-compose stop

start: init-db

.PHONY: build run init-db init-force clean-container clean-image clean-all reinit-db clean-volume
22 changes: 22 additions & 0 deletions deployment/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3.8'

services:
gadm-db:
build:
context: .
args:
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
GADM_FILE: ${GADM_FILE}
environment:
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
ports:
- "5432:5432"
volumes:
- postgis_data:/var/lib/postgresql/data

volumes:
postgis_data:
5 changes: 5 additions & 0 deletions deployment/init-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -e

# Import GPKG file into PostGIS
ogr2ogr -f "PostgreSQL" PG:"dbname=$POSTGRES_DB user=$POSTGRES_USER password=$POSTGRES_PASSWORD" "/tmp/gadm.gpkg"

0 comments on commit bd0ef62

Please sign in to comment.