Django Dockerized
Using a Django app in a Docker container is very easy and straightforward.
- In a new directory, create a
Dockerfile
.
FROM python:3.8-slim-buster
ENV PYTHONUNBUFFERED=1
WORKDIR /django
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
- It isn't necessary to have created a virtual environment. However, in the case of our example, we need a
requirements.txt
with our dependencies.
asgiref==3.3.1
Django==3.1
pytz==2021.1
sqlparse==0.4.1
- Create a
docker-compose.yml
. A possible option is the following:
version: "3.8"
services:
app:
build: .
volumes:
- .:/django
ports:
- 8000:8000
image: app:django
container_name: django_container
command: python manage.py runserver 0.0.0.0:8000
- In order to work with a postgres database, our
Dockerfile
can be:
FROM python:3.8 # slim-buster has dependency problems for postgresql
ENV PYTHONUNBUFFERED=1
WORKDIR /django
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
and our docker-compose.yml
:
version: "3.8"
services:
app:
build: .
volumes:
- .:/django
ports:
- 8000:8000
image: app:django
container_name: django_container
command: python manage.py runserver 0.0.0.0:8000
depends_on:
- db
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
container_name: postgres_db
- To create the Docker image, run
docker build -t python-django .
- And compose it and pass additional commands as:
docker-compose run --rm app django-admin startproject core .
- Then to compose from the image:
docker compose up
- To run commands in bash of the OS in the container:
docker exec -it django_container /bin/bash
- You can run migrations in the prompt.
- Remember to edit your
settings.py
to include the postgresql db:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "postgres",
"USER": "postgres",
"PASSWORD": "postgres",
"HOST": "db",
"PORT": 5432,
}
}
- Check these tutorials:
React - Django dockerized
- Django's
Dockerfile
FROM python:3.8-alpine
ENV PYTHONUNBUFFERED=1
WORKDIR /django
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
- React's Dockerfile
FROM node:15.13-alpine
WORKDIR /react
COPY . .
RUN npm run build
- In the parent directory, enclosing both Django and React, a
docker-compose.yml
file:
version: "3"
services:
backend:
build:
context: ./django
command: gunicorn core.wsgi --bind 0.0.0.0:8000
ports:
- 8000:8000
frontend:
build:
context: ./react/blogapi
volumes:
- react_build:/react/build
nginx:
image: nginx:latest
ports:
- 80:8080
volumes:
- ./nginx/nginx-setup.conf:/etc/nginx/conf.d/default.conf:ro
- react_build:/var/www/react
depends_on:
- backend
- frontend
volumes:
react_build:
- In a nginx directory, same level as React and Django, a
nginx-setup.conf
file:
upstream api {
server backend:8000;
}
server {
listen 8080;
location / {
root /var/www/react;
}
location /api/ {
proxy_pass http://api;
proxy_set_header Host $http_host;
}
}
- Run
docker-compose build
docker-compose up
Backlinks
Knowledge Base
- [[django-dockerized]]