From d954dc687cecd6c5ec875f875171893af336c44d Mon Sep 17 00:00:00 2001 From: Markus <9neblung@informatik.uni-hamburg.de> Date: Sat, 20 Apr 2024 17:26:36 +0200 Subject: [PATCH 1/2] add dev docker --- {{ cookiecutter.project_slug }}/compose.yaml | 33 +++++++++++ .../docker/backend/Dockerfile | 21 +++++++ .../docker/backend/env.dev.local | 1 + .../docker/backend/start.sh | 8 +++ .../docker/frontend/Dockerfile | 15 +++++ .../docker/frontend/start.sh | 4 ++ .../docker/webserver/Dockerfile | 5 ++ .../docker/webserver/nginx.conf | 58 +++++++++++++++++++ 8 files changed, 145 insertions(+) create mode 100644 {{ cookiecutter.project_slug }}/compose.yaml create mode 100644 {{ cookiecutter.project_slug }}/docker/backend/Dockerfile create mode 100644 {{ cookiecutter.project_slug }}/docker/backend/env.dev.local create mode 100644 {{ cookiecutter.project_slug }}/docker/backend/start.sh create mode 100644 {{ cookiecutter.project_slug }}/docker/frontend/Dockerfile create mode 100644 {{ cookiecutter.project_slug }}/docker/frontend/start.sh create mode 100644 {{ cookiecutter.project_slug }}/docker/webserver/Dockerfile create mode 100644 {{ cookiecutter.project_slug }}/docker/webserver/nginx.conf diff --git a/{{ cookiecutter.project_slug }}/compose.yaml b/{{ cookiecutter.project_slug }}/compose.yaml new file mode 100644 index 0000000..214f56c --- /dev/null +++ b/{{ cookiecutter.project_slug }}/compose.yaml @@ -0,0 +1,33 @@ +services: + database: + image: postgres + volumes: + - ./dev_db:/var/lib/postgresql/data + environment: + - POSTGRES_USER={{ cookiecutter.project_slug }} + - POSTGRES_PASSWORD={{ cookiecutter.project_slug }} + - POSTGRES_DB={{ cookiecutter.project_slug }} + + backend: + build: + context: ./ + dockerfile: ./docker/backend/Dockerfile + volumes: + - ./:/app/backend + depends_on: + - database + + frontend: + build: docker/frontend + volumes: + - ./src/{{ cookiecutter.project_slug }}_gui:/app/frontend + + webserver: + build: docker/webserver + depends_on: + - backend + - frontend + - database + ports: + - 8000:8000 + - 3000:3000 diff --git a/{{ cookiecutter.project_slug }}/docker/backend/Dockerfile b/{{ cookiecutter.project_slug }}/docker/backend/Dockerfile new file mode 100644 index 0000000..539e41c --- /dev/null +++ b/{{ cookiecutter.project_slug }}/docker/backend/Dockerfile @@ -0,0 +1,21 @@ +FROM python:3.11-slim + +# Install normal dependencies +RUN apt-get update && apt-get -y --no-install-recommends install build-essential nginx && rm -rf /var/lib/apt/lists/* + +# Add project code to container +VOLUME /app/backend +WORKDIR /app/backend + +# Install Python dependencies +RUN pip3 install pipenv==v2022.4.20 + +RUN usermod -u 2009 -g 33 -d /app/backend www-data + +ADD ./docker/backend/env.dev.local /app/config/.env.dev.local + +EXPOSE 8000 + +#USER www-data:www-data +ADD ./docker/backend/start.sh /usr/local/bin/start.sh +CMD ["bash", "/usr/local/bin/start.sh"] diff --git a/{{ cookiecutter.project_slug }}/docker/backend/env.dev.local b/{{ cookiecutter.project_slug }}/docker/backend/env.dev.local new file mode 100644 index 0000000..223721b --- /dev/null +++ b/{{ cookiecutter.project_slug }}/docker/backend/env.dev.local @@ -0,0 +1 @@ +DJANGO_DB=postgres://{{ cookiecutter.project_slug }}:{{ cookiecutter.project_slug }}@database:5432/{{ cookiecutter.project_slug }} diff --git a/{{ cookiecutter.project_slug }}/docker/backend/start.sh b/{{ cookiecutter.project_slug }}/docker/backend/start.sh new file mode 100644 index 0000000..5e4fad2 --- /dev/null +++ b/{{ cookiecutter.project_slug }}/docker/backend/start.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +ln -sf /app/config/.env.dev.local /app/backend/.env.dev.local + +pipenv install --dev --system --ignore-pipfile + +python3 src/manage.py migrate +python3 src/manage.py runserver 0.0.0.0:8000 diff --git a/{{ cookiecutter.project_slug }}/docker/frontend/Dockerfile b/{{ cookiecutter.project_slug }}/docker/frontend/Dockerfile new file mode 100644 index 0000000..6a466fe --- /dev/null +++ b/{{ cookiecutter.project_slug }}/docker/frontend/Dockerfile @@ -0,0 +1,15 @@ +FROM node:21-slim + +VOLUME /app/frontend +WORKDIR /app/frontend + +ADD ./start.sh /usr/local/bin/start.sh + +RUN npm install -g pnpm + +RUN chmod 774 /usr/local/bin/start.sh + +USER 1000 +EXPOSE 3000 + +CMD ["bash", "/usr/local/bin/start.sh"] diff --git a/{{ cookiecutter.project_slug }}/docker/frontend/start.sh b/{{ cookiecutter.project_slug }}/docker/frontend/start.sh new file mode 100644 index 0000000..2943ca6 --- /dev/null +++ b/{{ cookiecutter.project_slug }}/docker/frontend/start.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +pnpm install +npm run dev diff --git a/{{ cookiecutter.project_slug }}/docker/webserver/Dockerfile b/{{ cookiecutter.project_slug }}/docker/webserver/Dockerfile new file mode 100644 index 0000000..2252628 --- /dev/null +++ b/{{ cookiecutter.project_slug }}/docker/webserver/Dockerfile @@ -0,0 +1,5 @@ +FROM nginx:stable + +ADD nginx.conf /etc/nginx/nginx.conf + +EXPOSE 8000 diff --git a/{{ cookiecutter.project_slug }}/docker/webserver/nginx.conf b/{{ cookiecutter.project_slug }}/docker/webserver/nginx.conf new file mode 100644 index 0000000..3e863fc --- /dev/null +++ b/{{ cookiecutter.project_slug }}/docker/webserver/nginx.conf @@ -0,0 +1,58 @@ +user nginx; +worker_processes auto; + +error_log /var/log/nginx/error.log notice; +pid /var/run/nginx.pid; + + +events { + worker_connections 1024; +} + + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" '; + + access_log /var/log/nginx/access.log main; + + sendfile on; + + keepalive_timeout 65; + + server { + listen 8000; + server_name default_server; + + location ~* ^/(django-static|api|admin|media).* { + proxy_pass http://backend:8000; + proxy_hide_header Upgrade; + client_max_body_size 0; + proxy_set_header Host $http_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + location / { + proxy_pass http://frontend:3000/; + proxy_hide_header Upgrade; + client_max_body_size 0; + proxy_set_header Host $http_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + location /_nuxt { + proxy_pass http://frontend:3000/_nuxt; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + } + } +} From 71a9cc4686a2cb9ba0bcf02c911b0c64975b4018 Mon Sep 17 00:00:00 2001 From: Markus <9neblung@informatik.uni-hamburg.de> Date: Tue, 23 Apr 2024 12:12:39 +0200 Subject: [PATCH 2/2] add description for docker setups --- {{ cookiecutter.project_slug }}/Dockerfile | 6 +++--- {{ cookiecutter.project_slug }}/README.md | 12 ++++++++++++ {{ cookiecutter.project_slug }}/compose.yaml | 6 +++--- .../docker/{ => dev}/backend/Dockerfile | 4 ++-- .../docker/{ => dev}/backend/env.dev.local | 0 .../docker/{ => dev}/backend/start.sh | 0 .../docker/{ => dev}/frontend/Dockerfile | 0 .../docker/{ => dev}/frontend/start.sh | 0 .../docker/{ => dev}/webserver/Dockerfile | 0 .../docker/{ => dev}/webserver/nginx.conf | 0 .../docker/{ => prod}/nginx.conf | 0 .../docker/{ => prod}/start_backend.sh | 0 .../docker/{ => prod}/start_frontend.sh | 0 .../docker/{ => prod}/start_nginx.sh | 0 .../docker/{ => prod}/supervisord.conf | 0 15 files changed, 20 insertions(+), 8 deletions(-) rename {{ cookiecutter.project_slug }}/docker/{ => dev}/backend/Dockerfile (78%) rename {{ cookiecutter.project_slug }}/docker/{ => dev}/backend/env.dev.local (100%) rename {{ cookiecutter.project_slug }}/docker/{ => dev}/backend/start.sh (100%) rename {{ cookiecutter.project_slug }}/docker/{ => dev}/frontend/Dockerfile (100%) rename {{ cookiecutter.project_slug }}/docker/{ => dev}/frontend/start.sh (100%) rename {{ cookiecutter.project_slug }}/docker/{ => dev}/webserver/Dockerfile (100%) rename {{ cookiecutter.project_slug }}/docker/{ => dev}/webserver/nginx.conf (100%) rename {{ cookiecutter.project_slug }}/docker/{ => prod}/nginx.conf (100%) rename {{ cookiecutter.project_slug }}/docker/{ => prod}/start_backend.sh (100%) rename {{ cookiecutter.project_slug }}/docker/{ => prod}/start_frontend.sh (100%) rename {{ cookiecutter.project_slug }}/docker/{ => prod}/start_nginx.sh (100%) rename {{ cookiecutter.project_slug }}/docker/{ => prod}/supervisord.conf (100%) diff --git a/{{ cookiecutter.project_slug }}/Dockerfile b/{{ cookiecutter.project_slug }}/Dockerfile index 84c9eff..433421b 100644 --- a/{{ cookiecutter.project_slug }}/Dockerfile +++ b/{{ cookiecutter.project_slug }}/Dockerfile @@ -22,8 +22,8 @@ FROM docker.io/debian:bookworm as final ENV DEBIAN_FRONTEND=noninteractive RUN apt-get update &&\ apt-get install -y --no-install-recommends nodejs python3 python-is-python3 pipenv nginx gunicorn xz-utils supervisor -COPY docker/supervisord.conf /etc/supervisor/supervisord.conf -COPY docker/start_backend.sh docker/start_frontend.sh docker/start_nginx.sh /usr/local/bin/ +COPY docker/prod/supervisord.conf /etc/supervisor/supervisord.conf +COPY docker/prod/start_backend.sh docker/prod/start_frontend.sh docker/prod/start_nginx.sh /usr/local/bin/ CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"] # install django server @@ -32,7 +32,7 @@ ADD Pipfile Pipfile.lock ./ RUN pipenv install --system --deploy ADD LICENSE README.md ./ ADD src/ ./src/ -ADD docker/nginx.conf /etc/nginx/sites-enabled/default +ADD docker/prod/nginx.conf /etc/nginx/sites-enabled/default # add built frontend sources COPY --from=gui_build /usr/local/src/{{ cookiecutter.project_slug }}_gui/.output ./src/{{ cookiecutter.project_slug }}_gui/dist/ diff --git a/{{ cookiecutter.project_slug }}/README.md b/{{ cookiecutter.project_slug }}/README.md index 1a01e6e..0d0c9ae 100644 --- a/{{ cookiecutter.project_slug }}/README.md +++ b/{{ cookiecutter.project_slug }}/README.md @@ -28,3 +28,15 @@ The following variables are defined: | `NUXT_PUBLIC_OPENID_ISSUER` | *mafiasi-identity* | no | The openid issuer which the frontend uses for authentication (should be the same as the one in configured for django) | For the configuration of these variables in `dev` mode, see the configuration in [.env.dev](./.env.dev). + +## Docker Setup + +This project contains two different docker setup. One is for production, one for development. + +### Production Setup + +The Docker Setup for production consists of the `Dockerfile` in the root of this project. The required assets are in the `/docker/prod` directory. + +### Development Setup + +The Docker Setup for development is based on the `compose.yaml` in the root of this project. All Dockerfiles and other files required for this are in the `/docker/dev` directory. diff --git a/{{ cookiecutter.project_slug }}/compose.yaml b/{{ cookiecutter.project_slug }}/compose.yaml index 214f56c..17f1819 100644 --- a/{{ cookiecutter.project_slug }}/compose.yaml +++ b/{{ cookiecutter.project_slug }}/compose.yaml @@ -11,19 +11,19 @@ services: backend: build: context: ./ - dockerfile: ./docker/backend/Dockerfile + dockerfile: ./docker/dev/backend/Dockerfile volumes: - ./:/app/backend depends_on: - database frontend: - build: docker/frontend + build: docker/dev/frontend volumes: - ./src/{{ cookiecutter.project_slug }}_gui:/app/frontend webserver: - build: docker/webserver + build: docker/dev/webserver depends_on: - backend - frontend diff --git a/{{ cookiecutter.project_slug }}/docker/backend/Dockerfile b/{{ cookiecutter.project_slug }}/docker/dev/backend/Dockerfile similarity index 78% rename from {{ cookiecutter.project_slug }}/docker/backend/Dockerfile rename to {{ cookiecutter.project_slug }}/docker/dev/backend/Dockerfile index 539e41c..0d53ca2 100644 --- a/{{ cookiecutter.project_slug }}/docker/backend/Dockerfile +++ b/{{ cookiecutter.project_slug }}/docker/dev/backend/Dockerfile @@ -12,10 +12,10 @@ RUN pip3 install pipenv==v2022.4.20 RUN usermod -u 2009 -g 33 -d /app/backend www-data -ADD ./docker/backend/env.dev.local /app/config/.env.dev.local +ADD ./docker/dev/backend/env.dev.local /app/config/.env.dev.local EXPOSE 8000 #USER www-data:www-data -ADD ./docker/backend/start.sh /usr/local/bin/start.sh +ADD ./docker/dev/backend/start.sh /usr/local/bin/start.sh CMD ["bash", "/usr/local/bin/start.sh"] diff --git a/{{ cookiecutter.project_slug }}/docker/backend/env.dev.local b/{{ cookiecutter.project_slug }}/docker/dev/backend/env.dev.local similarity index 100% rename from {{ cookiecutter.project_slug }}/docker/backend/env.dev.local rename to {{ cookiecutter.project_slug }}/docker/dev/backend/env.dev.local diff --git a/{{ cookiecutter.project_slug }}/docker/backend/start.sh b/{{ cookiecutter.project_slug }}/docker/dev/backend/start.sh similarity index 100% rename from {{ cookiecutter.project_slug }}/docker/backend/start.sh rename to {{ cookiecutter.project_slug }}/docker/dev/backend/start.sh diff --git a/{{ cookiecutter.project_slug }}/docker/frontend/Dockerfile b/{{ cookiecutter.project_slug }}/docker/dev/frontend/Dockerfile similarity index 100% rename from {{ cookiecutter.project_slug }}/docker/frontend/Dockerfile rename to {{ cookiecutter.project_slug }}/docker/dev/frontend/Dockerfile diff --git a/{{ cookiecutter.project_slug }}/docker/frontend/start.sh b/{{ cookiecutter.project_slug }}/docker/dev/frontend/start.sh similarity index 100% rename from {{ cookiecutter.project_slug }}/docker/frontend/start.sh rename to {{ cookiecutter.project_slug }}/docker/dev/frontend/start.sh diff --git a/{{ cookiecutter.project_slug }}/docker/webserver/Dockerfile b/{{ cookiecutter.project_slug }}/docker/dev/webserver/Dockerfile similarity index 100% rename from {{ cookiecutter.project_slug }}/docker/webserver/Dockerfile rename to {{ cookiecutter.project_slug }}/docker/dev/webserver/Dockerfile diff --git a/{{ cookiecutter.project_slug }}/docker/webserver/nginx.conf b/{{ cookiecutter.project_slug }}/docker/dev/webserver/nginx.conf similarity index 100% rename from {{ cookiecutter.project_slug }}/docker/webserver/nginx.conf rename to {{ cookiecutter.project_slug }}/docker/dev/webserver/nginx.conf diff --git a/{{ cookiecutter.project_slug }}/docker/nginx.conf b/{{ cookiecutter.project_slug }}/docker/prod/nginx.conf similarity index 100% rename from {{ cookiecutter.project_slug }}/docker/nginx.conf rename to {{ cookiecutter.project_slug }}/docker/prod/nginx.conf diff --git a/{{ cookiecutter.project_slug }}/docker/start_backend.sh b/{{ cookiecutter.project_slug }}/docker/prod/start_backend.sh similarity index 100% rename from {{ cookiecutter.project_slug }}/docker/start_backend.sh rename to {{ cookiecutter.project_slug }}/docker/prod/start_backend.sh diff --git a/{{ cookiecutter.project_slug }}/docker/start_frontend.sh b/{{ cookiecutter.project_slug }}/docker/prod/start_frontend.sh similarity index 100% rename from {{ cookiecutter.project_slug }}/docker/start_frontend.sh rename to {{ cookiecutter.project_slug }}/docker/prod/start_frontend.sh diff --git a/{{ cookiecutter.project_slug }}/docker/start_nginx.sh b/{{ cookiecutter.project_slug }}/docker/prod/start_nginx.sh similarity index 100% rename from {{ cookiecutter.project_slug }}/docker/start_nginx.sh rename to {{ cookiecutter.project_slug }}/docker/prod/start_nginx.sh diff --git a/{{ cookiecutter.project_slug }}/docker/supervisord.conf b/{{ cookiecutter.project_slug }}/docker/prod/supervisord.conf similarity index 100% rename from {{ cookiecutter.project_slug }}/docker/supervisord.conf rename to {{ cookiecutter.project_slug }}/docker/prod/supervisord.conf