r/docker • u/Mukul-nst • 8d ago
Containerised my Django app
So i have containerised my Nginx, Django backend and postgresql db on my vm. All the containers run through a single 'docker compose up' command. I have created a custom network for the containers to communicate. I have also mounted docker volumes to containers for persistent storage.
So far i am finding DevOps very interesting and will now learn CI/CD using Github actions.
Will be very grateful if you share your thoughts.
3
u/Signal_Strength_5054 8d ago
I have this running in prod, on a vps. There is others containers too like redis, etc. It works prety nice, and I have the ci/CD running on github actions, using gh artifactory as a register, it works like a charm.
- Never share your django ports or the gunicorn ports.
- use the nginx as a reverse proxy
- scale de gunicorn to more threads
- configure the size cpu/ram for each container
- setup cloud flare in front your nginx, and only allow CF isso and https connection by CF cert.
3
u/MiserableDocument509 8d ago
nice, this is a solid first setup. two things that bit me early on: add a healthcheck to the postgres container and use depends_on with condition service_healthy, otherwise django can start before the db is ready and you get connection errors on first boot. and don't forget migrations + collectstatic belong in the deploy step, not baked into the image build, so you can run them after pulling. also keep your secrets in a .env file and gitignore it now before you wire up CI, saves a headache later
1
u/Mukul-nst 8d ago
Sure I will add a healthcheck and depends_on and also I have to add migration and collectstatic in deployment step. Thanks for the suggestions.
1
u/HeiiHallo 8d ago
i like to use haloy for this, theres a django example here: https://haloy.dev/docs/django-sqlite
1
u/Mukul-nst 7d ago
UPDATE: i added healthcheck key for database container
healthcheck:
test: ["CMD-SHELL","pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 15s
timeout: 10s
retries: 5
also added 'depends_on' in django container config
depends_on:
postgres:
condition: service_healthy
I also removed all the hardcoded values replaced them by giving .env file to containers under key env_file.
1
u/LLMCitizen 6d ago
You finally got a stripe on that white belt! It only gets harder until it gets easier
5
u/Hansehart 8d ago
Sounds good. Next steps could be to built an automatic CI/CD pipe, that builds your images on a tag release, pushes to registry and deploys then automatically. I also love traefik as proxy, because you declare your configuration inside the compose. Others like caddy too.