Я собрал пример конфигурации.
docker -compose.yml
version: '3'
volumes:
local_postgres_data: {}
local_postgres_data_backups: {}
services:
web:
build:
context: .
dockerfile: ./compose/python/Dockerfile
ports:
- "8000:8000"
depends_on:
- postgres
env_file:
- ./.envs/.postgres
command: /start
postgres:
build:
context: .
dockerfile: ./compose/postgres/Dockerfile
image: app_production_postgres
volumes:
- local_postgres_data:/var/lib/postgresql/data
- local_postgres_data_backups:/backups
env_file:
- ./.envs/.postgres
ports:
- "5432:5432"
compose / postgres / Dockerfile
FROM postgres:11.3
compose / python / Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY ./compose/python/start /start
RUN sed -i 's/\r$//g' /start
RUN chmod +x /start
COPY ./compose/python/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
RUN chmod +x /entrypoint
ENTRYPOINT ["/entrypoint"]
compose / python / entrypoint
#!/bin/sh
set -o errexit
set -o nounset
if [ -z "${POSTGRES_USER}" ]; then
base_postgres_image_default_user='postgres'
export POSTGRES_USER="${base_postgres_image_default_user}"
fi
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
postgres_ready() {
python << END
import sys
import psycopg2
try:
psycopg2.connect(
dbname="${POSTGRES_DB}",
user="${POSTGRES_USER}",
password="${POSTGRES_PASSWORD}",
host="${POSTGRES_HOST}",
port="${POSTGRES_PORT}",
)
except psycopg2.OperationalError:
sys.exit(-1)
sys.exit(0)
END
}
until postgres_ready; do
>&2 echo 'Waiting for PostgreSQL to become available...'
sleep 1
done
>&2 echo 'PostgreSQL is available'
exec "$@"
compose / python / start
#!/bin/sh
set -o errexit
set -o nounset
python -m http.server
needs.txt
psycopg2>=2.7,<3.0
.envs /.postgres
# PostgreSQL
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_DB=your_app
POSTGRES_USER=debug
POSTGRES_PASSWORD=debug
Эта конфигурация является урезанной версией docker проекта, созданного django cookiecutter