Django project
Quickstart: Compose and Django
Dockerfile for virtualenv project
FROM python:latest
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /src
WORKDIR /src
COPY requirements.txt /src/
RUN pip install -r requirements.txt
COPY . /src/version: "3"
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/src
ports:
- "8000:8000"Dockerfile for pipenv project with Postgres, Gunicorn Nginx
FROM python:latest
# set default environment variables
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
# set work directory
WORKDIR /usr/application/src
# Add current directory code to working directory
COPY . .
# install environment dependencies
RUN pip install --upgrade pip
RUN pip install pipenv gunicorn
# Install project dependencies
RUN pipenv install --skip-lock --system --dev
ENTRYPOINT ["/usr/application/src/entrypoint.sh"]version: "3.8"
services:
db:
container_name: db
image: postgres:12.0-alpine
volumes:
- ./db_data:/var/lib/postgresql/data/
networks:
- backend
environment:
POSTGRES_DB: django_db
POSTGRES_USER: django_user
POSTGRES_PASSWORD: django_user
web:
container_name: web
build: .
env_file: .env
depends_on:
- db
networks:
- backend
volumes:
- static_files:/home/{user}/webroot/static
- ./mediafiles/:/home/{user}/webroot/media
command: gunicorn {project_name}.wsgi:application --bind 0.0.0.0:8000
nginx:
image: nginx:1.19.0-alpine
container_name: nginx
volumes:
- static_files:/webroot
- ./mediafiles/:/webroot
- ./config/nginx/:/etc/nginx/conf.d
networks:
- backend
ports:
# HOST_PORT:CONTAINER_PORT When HOST_PORT is defined, the service is accessible outside
- 80:80
depends_on:
- web
networks:
backend:
volumes:
static_files:
DEBUG=false
SECRET_KEY='some random key'
ALLOWED_HOST='.localhost, 127.0.0.1,'
POSTGRES_ENGINE=django.db.backends.postgresql
POSTGRES_DB=django_db
POSTGRES_USER=django_user
POSTGRES_PASSWORD=django_user
POSTGRES_HOST=db
POSTGRES_PORT=5432from decouple import config, Csv
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', default=False, cast=bool)
ALLOWED_HOSTS = config('ALLOWED_HOSTS', default='127.0.0.1', cast=Csv())
# Database
DATABASES = {
"default": {
"ENGINE": config("POSTGRES_ENGINE"),
"NAME": config("POSTGRES_DB"),
"USER": config("POSTGRES_USER"),
"PASSWORD": config("POSTGRES_PASSWORD"),
"HOST": config("POSTGRES_HOST", "localhost"),
"PORT": config("POSTGRES_PORT", "5432"),
}
}
STATIC_ROOT = os.path.join(BASE_DIR, "static")#!/bin/sh
if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."
while ! nc -z $POSTGRES_HOST $POSTGRES_PORT; do
sleep 1
done
echo "PostgreSQL started"
fi
python manage.py flush --no-input
python manage.py migrate
exec "$@"upstream app_server {
server web:8000;
}
server {
listen 80;
location / {
proxy_pass http://app_server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
root /usr/application/src/;
}
}In production, we can execute the command in the container
To migrate the database:
docker-compose exec web python manage.py flush --no-input
docker-compose exec web python manage.py migrateTo create User:
docker-compose exec web python manage.py createsuperuserLast updated
Was this helpful?