-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deployment: Dockerize PostGIS Service with GADM File-Based DB Initial…
…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
1 parent
c42edc4
commit bd0ef62
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |