added Dockerstuff for dev and prod using traefik

This commit is contained in:
gw3000 2023-07-11 18:24:36 +02:00
parent 17c06d1eaa
commit 879366eb95
8 changed files with 221 additions and 0 deletions

2
.dockerignore Normal file
View file

@ -0,0 +1,2 @@
.venv
db.sqlite3

3
Dockerfile.traefik Normal file
View file

@ -0,0 +1,3 @@
FROM traefik:v2.9.6
COPY ./traefik.prod.toml ./etc/traefik/traefik.toml

18
app/Dockerfile Normal file
View file

@ -0,0 +1,18 @@
# app/Dockerfile
# pull the official docker image
FROM python:3.11.2-slim
# set work directory
WORKDIR /app
# set env variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .

62
app/Dockerfile.prod Normal file
View file

@ -0,0 +1,62 @@
###########
# BUILDER #
###########
# pull official base image
FROM python:3.11-slim as builder
# set work directory
WORKDIR /app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install system dependencies
RUN apt-get update && \
apt-get install -y --no-install-recommends gcc
# lint
RUN pip install --upgrade pip
RUN pip install flake8==6.0.0
COPY . .
RUN flake8 --ignore=E501,F401 .
# install python dependencies
COPY requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
#########
# FINAL #
#########
# pull official base image
FROM python:3.11-slim
# create directory for the app user
RUN mkdir -p /home/app
# create the app user
RUN addgroup --system app && adduser --system --group app
# create the appropriate directories
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
WORKDIR $APP_HOME
# install dependencies
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /app/requirements.txt .
RUN pip install --upgrade pip
RUN pip install --no-cache /wheels/*
# copy project
COPY . $APP_HOME
# chown all the files to the app user
RUN chown -R app:app $APP_HOME
# change to the app user
USER app

53
docker-compose.prod.yml Normal file
View file

@ -0,0 +1,53 @@
version: '3.8'
services:
web:
build:
context: ./app
dockerfile: Dockerfile.prod
command: bash -c 'while !</dev/tcp/db/5432; do sleep 1; done; gunicorn --bind 0.0.0.0:8000 config.wsgi'
expose:
- 8000
environment:
- DEBUG=0
- DATABASE_URL=postgresql://django_traefik:django_traefik@db:5432/django_traefik
- DJANGO_ALLOWED_HOSTS=.your-domain.com
depends_on:
- db
labels:
- "traefik.enable=true"
- "traefik.http.routers.django.rule=Host(`django-traefik.your-domain.com`)"
- "traefik.http.routers.django.tls=true"
- "traefik.http.routers.django.tls.certresolver=letsencrypt"
db:
image: postgres:15-alpine
volumes:
- postgres_data_prod:/var/lib/postgresql/data/
expose:
- 5432
environment:
- POSTGRES_USER=django_traefik
- POSTGRES_PASSWORD=django_traefik
- POSTGRES_DB=django_traefik
traefik:
build:
context: .
dockerfile: Dockerfile.traefik
ports:
- 80:80
- 443:443
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "./traefik-public-certificates:/certificates"
labels:
- "traefik.enable=true"
- "traefik.http.routers.dashboard.rule=Host(`dashboard-django-traefik.your-domain.com`)"
- "traefik.http.routers.dashboard.tls=true"
- "traefik.http.routers.dashboard.tls.certresolver=letsencrypt"
- "traefik.http.routers.dashboard.service=api@internal"
- "traefik.http.routers.dashboard.middlewares=auth"
- "traefik.http.middlewares.auth.basicauth.users=testuser:$$apr1$$jIKW.bdS$$eKXe4Lxjgy/rH65wP1iQe1"
volumes:
postgres_data_prod:
traefik-public-certificates:

39
docker-compose.yaml Normal file
View file

@ -0,0 +1,39 @@
version: '3.8'
services:
web:
build: ./app
command: bash -c 'while !</dev/tcp/db/5432; do sleep 1; done; python manage.py runserver 0.0.0.0:8000'
volumes:
- ./app:/app
expose:
- 8000
environment:
- DEBUG=1
- DATABASE_URL=postgresql://django_traefik:django_traefik@db:5432/django_traefik
depends_on:
- db
labels:
- "traefik.enable=true"
- "traefik.http.routers.django.rule=Host(`django.localhost`)"
db:
image: postgres:15-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
expose:
- 5432
environment:
- POSTGRES_USER=django_traefik
- POSTGRES_PASSWORD=django_traefik
- POSTGRES_DB=django_traefik
traefik:
image: traefik:v2.9.6
ports:
- 8008:80
- 8081:8080
volumes:
- "$PWD/traefik.dev.toml:/etc/traefik/traefik.toml"
- "/var/run/docker.sock:/var/run/docker.sock:ro"
volumes:
postgres_data:

18
traefik.dev.toml Normal file
View file

@ -0,0 +1,18 @@
# listen on port 80
[entryPoints]
[entryPoints.web]
address = ":80"
# Traefik dashboard over http
[api]
insecure = true
[log]
level = "DEBUG"
[accessLog]
# containers are not discovered automatically
[providers]
[providers.docker]
exposedByDefault = false

26
traefik.prod.toml Normal file
View file

@ -0,0 +1,26 @@
[entryPoints]
[entryPoints.web]
address = ":80"
[entryPoints.web.http]
[entryPoints.web.http.redirections]
[entryPoints.web.http.redirections.entryPoint]
to = "websecure"
scheme = "https"
[entryPoints.websecure]
address = ":443"
[accessLog]
[api]
dashboard = true
[providers]
[providers.docker]
exposedByDefault = false
[certificatesResolvers.letsencrypt.acme]
email = "your@email.com"
storage = "/certificates/acme.json"
[certificatesResolvers.letsencrypt.acme.httpChallenge]
entryPoint = "web"