Here's my recommendation.
For just PostgreSQL with Postgis
Assuming you are on Linux or Mac, create a directory called gis
using mkdir gis
and go into the directory by doing cd gis
.
Create a file called Dockerfile.pg
.
from postgres
run apt-get update
run apt-get upgrade -y
run apt-get install postgis -y
Create a file called gis.sh
that contains:
#!/bin/bash
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
create extension postgis;
EOSQL
Create a file called docker-compose.yml
version: '3.8'
services:
gis:
build:
context: .
dockerfile: Dockerfile.pg
container_name: gis
image: gis
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: test
POSTGRES_DB: postgres
volumes:
- pg:/var/lib/postgresql/data
- ./gis.sh:/docker-entrypoint-initdb.d/gis.sh
volumes:
pg:
Then, do docker compose up -d
.
That'll use your Dockerfile.pg to:
- build the postgres image and call it gis
- run apt-get update
- run apt-get upgrade -y
- run apt-get install postgis
It'll create a container and call it gis
It'll mount a named volume called pg
so you can identify the volume easily.
It'll run gis.sh, which will create an extension called postgis after logging into PostgreSQL.
When you do docker image ls
, you will see a gis
image.
When you do docker container ls
, it'll show you the running container name.
You can log on to the container usingi docker exec -it gis /bin/bash
and you'll see
% docker exec -it gis /bin/bash
root@d3bdba37a76b:/#
You can verify that the extension has been added by doing:
root@d3bdba37a76b:/# psql postgres postgres
psql (15.3 (Debian 15.3-1.pgdg120+1))
Type "help" for help.
postgres=# SELECT oid, extname, extversion FROM pg_extension;
oid | extname | extversion
-------+---------+------------
13561 | plpgsql | 1.0
16384 | postgis | 3.3.3
(2 rows)
postgres=# exit
Once you are done using the container, you can bring remove the container using docker compose down -v
. The -v
will remove the volume as well.
Hint about your app, if you want to run it along with PostgreSQL
While I don't know how your app works, you might want to try something like this to bring up your database and app. Tweak as you deem fit.
In the gis
directory, you now have 3 files:
- Dockerfile.pg
- gis.sh
- docker-compose.yml
Add another file called Dockerfile
- that's your app's Dockerfile.
FROM node:12.18.3-alpine3.12 as builder
RUN apk update && apk add --no-cache make git
WORKDIR /usr/src/app
ARG NPM_TOKEN
COPY package*.json ./
RUN true
COPY package-lock.json ./
RUN true
COPY .npmrc /root/.npmrc
RUN npm i && rm /root/.npmrc && npm run postinstall; exit 0
COPY . .
RUN npm run build:all
Change the docker-compose.yml to:
version: '3.8'
services:
gis:
build:
context: .
dockerfile: Dockerfile.pg
image: gis
container_name: gis
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: test
POSTGRES_DB: postgres
volumes:
- pg:/var/lib/postgresql/data
- ./gis.sh:/docker-entrypoint-initdb.d/gis.sh
ports:
- "5434:5432"
app:
build:
context: .
dockerfile: Dockerfile
image: nodeapp
container_name: nodeapp
depends_on:
- gis
ports:
- "4000:4000"
volumes:
pg:
Then you can run docker compose up -d
and see the output.
Here's a blog that could also help you: https://michalzalecki.com/docker-compose-for-nodejs-and-postgresql/