Skip to content

Instantly share code, notes, and snippets.

@aybruhm
Last active December 13, 2023 09:55
Show Gist options
  • Save aybruhm/c7e120479bb8062edd9e0df07e2d237c to your computer and use it in GitHub Desktop.
Save aybruhm/c7e120479bb8062edd9e0df07e2d237c to your computer and use it in GitHub Desktop.
A complete docker stack {postgres, pgadmin, redis, celery worker, celery beat, flower} for your python web application.
version: "3.9"
services:
api:
build: .
restart: always
container_name: api
ports:
- "3456:3456"
volumes:
- .:/api
command: >
sh -c "<command to run server>"
env_file:
- ./.env
environment:
ADMIN_DATABASE_URL: postgresql://${ADMIN_INITDB_ROOT_USERNAME}:${ADMIN_INITDB_ROOT_PASSWORD}@db/${ADMIN_INITDB_DATABASE}
depends_on:
redis:
condition: service_started
db:
condition: service_healthy
db:
image: postgres:15.1-alpine
container_name: db
env_file:
- ./.env
volumes:
- db-data:/var/lib/postgresql/data/
ports:
- "5435:5432"
environment:
POSTGRES_USER: ${ADMIN_INITDB_ROOT_USERNAME}
POSTGRES_PASSWORD: ${ADMIN_INITDB_ROOT_PASSWORD}
POSTGRES_DB: ${ADMIN_INITDB_DATABASE}
healthcheck:
test:
[
'CMD-SHELL',
'pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB'
]
interval: 10s
timeout: 5s
retries: 5
pgadmin:
image: dpage/pgadmin4
container_name: db_admin
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin@root.com
PGADMIN_DEFAULT_PASSWORD: admin
ports:
- "5050:80"
volumes:
- pgadmin-data:/var/lib/pgadmin
redis:
image: redis:7.0
container_name: redis
worker:
build: .
restart: always
container_name: celery_worker
volumes:
- .:/api
command: python -m celery -A config worker -l info
env_file:
- ./.env
depends_on:
redis:
condition: service_started
beat:
build: .
restart: always
container_name: celery_beat
volumes:
- .:/api
command: python -m celery -A config beat
env_file:
- ./.env
depends_on:
redis:
condition: service_started
flower:
image: mher/flower:0.9.7
container_name: celery_analytics
command:
[
'flower',
'--broker=redis://redis:6379',
'--port=5555',
'--basic_auth=${FLOWER_AUTH_USER}:${FLOWER_AUTH_PASSWORD}'
]
ports:
- "5555:5555"
env_file:
- ./.env
depends_on:
redis:
condition: service_started
volumes:
db-data:
pgadmin-data:
@aybruhm
Copy link
Author

aybruhm commented May 28, 2023

In your root directory, create a file named .env and include the following environment variables:

ADMIN_INITDB_ROOT_USERNAME="user"
ADMIN_INITDB_ROOT_PASSWORD="password"
ADMIN_INITDB_DATABASE="db"
ADMIN_INITDB_DATABASE_HOST="db" # service name

FLOWER_AUTH_USER="abram" 
FLOWER_AUTH_PASSWORD="password"

Change the database root username, password, and database before using the environment variables. If you wish to change the database host, make sure to also update the service name in the YAML file. Do not forget to change the auth user and password for flower authentication. Good luck, and happy hacking! 🤓

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment