Skip to content

Commit

Permalink
feat: Add files for Docker. Rewrite settings.py to match cookiecutter…
Browse files Browse the repository at this point in the history
… and use Docker paths for media and db.
  • Loading branch information
jpmckinney committed Sep 30, 2024
1 parent 9ff2d42 commit ec8c007
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 72 deletions.
26 changes: 26 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Dotfiles
**/.*

# Docker
**/Dockerfile*

# Requirements
**/requirements.in
**/requirements_dev.in
**/requirements_dev.txt

# Documentation
docs
LICENSE
README.md
README.rst

# Configuration
pyproject.toml

# Testing
**/tests
pytest.ini

# Generated files
**/node_modules
38 changes: 38 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Deploy
on:
workflow_run:
workflows: ["CI"]
branches: [main]
types:
- completed
jobs:
docker:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# https://github.com/docker/login-action#github-container-registry
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# https://github.com/docker/setup-buildx-action#usage
- uses: docker/setup-buildx-action@v3
# https://github.com/docker/build-push-action#usage
- uses: docker/build-push-action@v6
with:
push: true
file: Dockerfile_django
tags: |
ghcr.io/${{ github.repository }}-django:latest
cache-from: type=gha
cache-to: type=gha,mode=max
- uses: docker/build-push-action@v6
with:
push: true
file: Dockerfile_static
tags: |
ghcr.io/${{ github.repository }}-static:latest
cache-from: type=gha
cache-to: type=gha,mode=max
43 changes: 43 additions & 0 deletions Dockerfile_django
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
FROM python:3.11 as build-stage

COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /tmp/requirements.txt

WORKDIR /workdir

COPY . .

ENV DJANGO_ENV=production

RUN python manage.py collectstatic --noinput -v2

FROM python:3.11

RUN apt-get update && apt-get install -y --no-install-recommends \
gettext \
&& rm -rf /var/lib/apt/lists/*

RUN groupadd -r runner && useradd --no-log-init -r -g runner runner

# Must match the settings.DATABASES default value.
RUN mkdir -p /data/db && chown -R runner:runner /data/db
# Must match the settings.MEDIA_ROOT default value.
RUN mkdir -p /data/media && chown -R runner:runner /data/media

COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /tmp/requirements.txt

WORKDIR /workdir
USER runner:runner
COPY --chown=runner:runner . .

# Django needs a copy of the staticfiles.json manifest file.
COPY --from=build-stage --chown=runner:runner /workdir/static/staticfiles.json /workdir/static/staticfiles.json

ENV DJANGO_ENV=production
ENV WEB_CONCURRENCY=2

RUN python manage.py compilemessages

EXPOSE 8000
CMD ["gunicorn", "core.wsgi", "--bind", "0.0.0.0:8000", "--worker-tmp-dir", "/dev/shm", "--threads", "2", "--name", "cove"]
17 changes: 17 additions & 0 deletions Dockerfile_static
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM python:{{ cookiecutter.python_version }} as build-stage

COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /tmp/requirements.txt

WORKDIR /workdir
COPY . .

ENV DJANGO_ENV=production

RUN python manage.py collectstatic --noinput -v2

FROM nginxinc/nginx-unprivileged:latest as production-stage
USER root
COPY --from=build-stage --chown=nginx:root /workdir/static /usr/share/nginx/html/static
COPY --chown=nginx:root default.conf /etc/nginx/conf.d/default.conf
USER nginx
Loading

0 comments on commit ec8c007

Please sign in to comment.