Django – Dockerfile and docker-compose.yaml

The below files can be used as a reference on how to deploy Django application in a container.

Dockerfile

FROM python:3.12.8-alpine
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt –no-cache-dir
COPY ./myresumebuilder/manage.py ./
COPY ./myresumebuilder/myresumebuildersite ./myresumebuildersite
COPY ./myresumebuilder/resumebuilder ./resumebuilder
RUN adduser -D myuser
USER myuser
# Collect static files (if any)
# RUN python myresumebuilder/manage.py collectstatic
EXPOSE 8000
# For production
CMD ["gunicorn", "–bind=0.0.0.0:8000", "myresumebuildersite.wsgi"]
# For development
# CMD ["python", "myresumebuilder/manage.py", "runserver", "0.0.0.0:8000"]
view raw Dockerfile hosted with ❤ by GitHub

docker-compose.yaml

# https://github.com/docker/awesome-compose/tree/master/official-documentation-samples/django/
# https://github.com/docker/awesome-compose/blob/master/django/app/Dockerfile
services:
db:
image: postgres:17.2-alpine
volumes:
– ./docker-volume/data/db:/var/lib/postgresql/data
env_file:
– .env
healthcheck:
test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"]
interval: 1s
timeout: 5s
retries: 10
web:
build: .
ports:
– "8000:8000"
env_file:
– .env
depends_on:
– db

Comments

Leave a comment