principlesqldockerMinor
Best practice for Postgres setup with docker-compose
Viewed 0 times
postgrespracticewithdockersetupforcomposebest
Problem
I have a docker-compose script which starts a Postgres container and an app container like so
This all works fine and 'app' can talk to 'db'. However, the postgres server is in a basically unusable state as it has no configuration with regards to having databases, users, etc. setup (apart from the defaults). It appears that the recommended way of fixing this is to create your own 'db image' pulling from postgres and running some shell script to take care of business (and then your composition uses this image), but this seems very hacky and convoluted to me.
Does anyone have a better way of setting up a postgres server with a known configuration inside a docker-compose based system?
db:
image: postgres:10.6
restart: always
ports:
- 5432:5432
app:
image: test_app:latest
restart: always
depends_on:
- dbThis all works fine and 'app' can talk to 'db'. However, the postgres server is in a basically unusable state as it has no configuration with regards to having databases, users, etc. setup (apart from the defaults). It appears that the recommended way of fixing this is to create your own 'db image' pulling from postgres and running some shell script to take care of business (and then your composition uses this image), but this seems very hacky and convoluted to me.
Does anyone have a better way of setting up a postgres server with a known configuration inside a docker-compose based system?
Solution
First, use alpine images if possible.
Second, use ENV variables for prepare database configuration. More info.
Third, use
Example stack with devlopment environment.
Second, use ENV variables for prepare database configuration. More info.
Third, use
/docker-entrypoint-initdb.d directory for extend image. More info.Example stack with devlopment environment.
version: '3.2'
services:
example:
image: "${EXAMPLE_SERVICE}:${EXAMPLE_SERVICE__VERSION}-development"
build:
context: .
dockerfile: Dockerfile.development
networks:
- net
ports:
- 5000:5000
depends_on:
- example__migrator
stdin_open: true
tty: true
environment:
POSTGRES_PASSWORD: example
POSTGRES_DB: example
POSTGRES_USER: example
POSTGRES_HOST: pg.example_net
POSTGRES_PORT: 5432
FLASK_APP: app.py
FLASK_DEBUG: 1
volumes:
- .:/home/example
example__migrator:
image: "${EXAMPLE_SERVICE}:${EXAMPLE_SERVICE__VERSION}-development"
build:
context: .
command: sh -c './wait-for pg.example_net:5432 -- python manage.py db upgrade'
networks:
- net
depends_on:
- pg
environment:
POSTGRES_PASSWORD: example
POSTGRES_DB: example
POSTGRES_USER: example
POSTGRES_HOST: pg.example_net
POSTGRES_PORT: 5432
pg:
image: postgres:10.0-alpine
networks:
- net
ports:
- 5432:5432
volumes:
- pg_data:/var/lib/postgresql/data/pg_data
environment:
POSTGRES_PASSWORD: example
POSTGRES_DB: example
POSTGRES_USER: example
POSTGRES_HOST: pg.example_net
POSTGRES_PORT: 5432
PGDATA: /var/lib/postgresql/data/pg_data
networks:
net:
volumes:
pg_data:Code Snippets
version: '3.2'
services:
example:
image: "${EXAMPLE_SERVICE}:${EXAMPLE_SERVICE__VERSION}-development"
build:
context: .
dockerfile: Dockerfile.development
networks:
- net
ports:
- 5000:5000
depends_on:
- example__migrator
stdin_open: true
tty: true
environment:
POSTGRES_PASSWORD: example
POSTGRES_DB: example
POSTGRES_USER: example
POSTGRES_HOST: pg.example_net
POSTGRES_PORT: 5432
FLASK_APP: app.py
FLASK_DEBUG: 1
volumes:
- .:/home/example
example__migrator:
image: "${EXAMPLE_SERVICE}:${EXAMPLE_SERVICE__VERSION}-development"
build:
context: .
command: sh -c './wait-for pg.example_net:5432 -- python manage.py db upgrade'
networks:
- net
depends_on:
- pg
environment:
POSTGRES_PASSWORD: example
POSTGRES_DB: example
POSTGRES_USER: example
POSTGRES_HOST: pg.example_net
POSTGRES_PORT: 5432
pg:
image: postgres:10.0-alpine
networks:
- net
ports:
- 5432:5432
volumes:
- pg_data:/var/lib/postgresql/data/pg_data
environment:
POSTGRES_PASSWORD: example
POSTGRES_DB: example
POSTGRES_USER: example
POSTGRES_HOST: pg.example_net
POSTGRES_PORT: 5432
PGDATA: /var/lib/postgresql/data/pg_data
networks:
net:
volumes:
pg_data:Context
StackExchange DevOps Q#5735, answer score: 5
Revisions (0)
No revisions yet.