Skip to content

Commit ec8c007

Browse files
committed
feat: Add files for Docker. Rewrite settings.py to match cookiecutter and use Docker paths for media and db.
1 parent 9ff2d42 commit ec8c007

File tree

6 files changed

+329
-72
lines changed

6 files changed

+329
-72
lines changed

.dockerignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Dotfiles
2+
**/.*
3+
4+
# Docker
5+
**/Dockerfile*
6+
7+
# Requirements
8+
**/requirements.in
9+
**/requirements_dev.in
10+
**/requirements_dev.txt
11+
12+
# Documentation
13+
docs
14+
LICENSE
15+
README.md
16+
README.rst
17+
18+
# Configuration
19+
pyproject.toml
20+
21+
# Testing
22+
**/tests
23+
pytest.ini
24+
25+
# Generated files
26+
**/node_modules

.github/workflows/docker.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Deploy
2+
on:
3+
workflow_run:
4+
workflows: ["CI"]
5+
branches: [main]
6+
types:
7+
- completed
8+
jobs:
9+
docker:
10+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
# https://github.com/docker/login-action#github-container-registry
15+
- uses: docker/login-action@v3
16+
with:
17+
registry: ghcr.io
18+
username: ${{ github.actor }}
19+
password: ${{ secrets.GITHUB_TOKEN }}
20+
# https://github.com/docker/setup-buildx-action#usage
21+
- uses: docker/setup-buildx-action@v3
22+
# https://github.com/docker/build-push-action#usage
23+
- uses: docker/build-push-action@v6
24+
with:
25+
push: true
26+
file: Dockerfile_django
27+
tags: |
28+
ghcr.io/${{ github.repository }}-django:latest
29+
cache-from: type=gha
30+
cache-to: type=gha,mode=max
31+
- uses: docker/build-push-action@v6
32+
with:
33+
push: true
34+
file: Dockerfile_static
35+
tags: |
36+
ghcr.io/${{ github.repository }}-static:latest
37+
cache-from: type=gha
38+
cache-to: type=gha,mode=max

Dockerfile_django

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
FROM python:3.11 as build-stage
2+
3+
COPY requirements.txt /tmp/requirements.txt
4+
RUN pip install --no-cache-dir -r /tmp/requirements.txt
5+
6+
WORKDIR /workdir
7+
8+
COPY . .
9+
10+
ENV DJANGO_ENV=production
11+
12+
RUN python manage.py collectstatic --noinput -v2
13+
14+
FROM python:3.11
15+
16+
RUN apt-get update && apt-get install -y --no-install-recommends \
17+
gettext \
18+
&& rm -rf /var/lib/apt/lists/*
19+
20+
RUN groupadd -r runner && useradd --no-log-init -r -g runner runner
21+
22+
# Must match the settings.DATABASES default value.
23+
RUN mkdir -p /data/db && chown -R runner:runner /data/db
24+
# Must match the settings.MEDIA_ROOT default value.
25+
RUN mkdir -p /data/media && chown -R runner:runner /data/media
26+
27+
COPY requirements.txt /tmp/requirements.txt
28+
RUN pip install --no-cache-dir -r /tmp/requirements.txt
29+
30+
WORKDIR /workdir
31+
USER runner:runner
32+
COPY --chown=runner:runner . .
33+
34+
# Django needs a copy of the staticfiles.json manifest file.
35+
COPY --from=build-stage --chown=runner:runner /workdir/static/staticfiles.json /workdir/static/staticfiles.json
36+
37+
ENV DJANGO_ENV=production
38+
ENV WEB_CONCURRENCY=2
39+
40+
RUN python manage.py compilemessages
41+
42+
EXPOSE 8000
43+
CMD ["gunicorn", "core.wsgi", "--bind", "0.0.0.0:8000", "--worker-tmp-dir", "/dev/shm", "--threads", "2", "--name", "cove"]

Dockerfile_static

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM python:{{ cookiecutter.python_version }} as build-stage
2+
3+
COPY requirements.txt /tmp/requirements.txt
4+
RUN pip install --no-cache-dir -r /tmp/requirements.txt
5+
6+
WORKDIR /workdir
7+
COPY . .
8+
9+
ENV DJANGO_ENV=production
10+
11+
RUN python manage.py collectstatic --noinput -v2
12+
13+
FROM nginxinc/nginx-unprivileged:latest as production-stage
14+
USER root
15+
COPY --from=build-stage --chown=nginx:root /workdir/static /usr/share/nginx/html/static
16+
COPY --chown=nginx:root default.conf /etc/nginx/conf.d/default.conf
17+
USER nginx

0 commit comments

Comments
 (0)