From 9153e2bc734c19c92c7f6c3ae68d50ae936b6577 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 25 Apr 2020 16:50:04 +0100 Subject: [PATCH] Add Dockerfile for production deployment --- .dockerignore | 8 ++++++++ .github/workflows/docker.yml | 21 +++++++++++++++++++++ Dockerfile | 26 ++++++++++++++++++++++++++ requirements.txt | 1 + scripts/build-static.sh | 2 +- scripts/setup-node.sh | 9 +++++++++ scripts/setup-python.sh | 12 ++++++++++++ scripts/setup.sh | 20 ++++++-------------- 8 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/docker.yml create mode 100644 Dockerfile create mode 100755 scripts/setup-node.sh create mode 100755 scripts/setup-python.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..1c5e60c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +.cache +.mypy_cache +collected-static +env +htmlcov +node_modules +static/build +__pycache__ diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..5ab6920 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,21 @@ +name: Docker + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + steps: + + - uses: actions/checkout@v1 + + - name: Build container + run: docker build --tag docker.pkg.github.com/${GITHUB_REPOSITORY,,}/website:latest . + + - name: Log into GitHub Docker Registry + run: echo ${{ secrets.GITHUB_TOKEN }} | docker login docker.pkg.github.com -u ${GITHUB_REPOSITORY%/*} --password-stdin + if: github.ref == 'master' + + - name: Push the Docker container + run: docker push docker.pkg.github.com/${GITHUB_REPOSITORY,,}/website:latest + if: github.ref == 'master' diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..92053ae --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM node:lts-alpine as build + +ENV NODE_ENV production +ENV NPM_CONFIG_PROUCTION false + +WORKDIR /app + +COPY . /app + +RUN ./scripts/setup-node.sh + +FROM python:3.8-slim + +ENV DEBUG false + +WORKDIR /app +COPY . /app + +COPY --from=build /app/static/build /app/static/build + +RUN ./scripts/setup-python.sh + +RUN rm -rf ./static + +EXPOSE 8000 +CMD ["gunicorn", "-b", "0.0.0.0:8000", "--preload", "--access-logfile", "/dev/stdout", "website.wsgi:application"] diff --git a/requirements.txt b/requirements.txt index 5c6497c..0119575 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ django-debug-toolbar==2.2 whitenoise==5.0.1 brotli==1.0.7 django-environ==0.4.5 +gunicorn ==20.0.4 diff --git a/scripts/build-static.sh b/scripts/build-static.sh index 86a0d2c..02988b7 100755 --- a/scripts/build-static.sh +++ b/scripts/build-static.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh export PATH=node_modules/.bin:${PATH} diff --git a/scripts/setup-node.sh b/scripts/setup-node.sh new file mode 100755 index 0000000..098d689 --- /dev/null +++ b/scripts/setup-node.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env sh + +export PATH=node_modules/.bin:${PATH} + +set -ex + +npm ci + +./scripts/build-static.sh diff --git a/scripts/setup-python.sh b/scripts/setup-python.sh new file mode 100755 index 0000000..dca98b0 --- /dev/null +++ b/scripts/setup-python.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +set -ex + +pip install -r requirements.txt + +if [ "$DEBUG" != "false" ]; +then + pip install -r dev-requirements.txt +fi + +python3 ./manage.py collectstatic --noinput --clear -v 2 diff --git a/scripts/setup.sh b/scripts/setup.sh index bad15dc..c229b30 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -1,20 +1,12 @@ #!/usr/bin/env bash +set -e + + +./scripts/setup-node.sh + export PATH=env/bin:${PATH} -set -ex - python3 -m venv env -pip install -r requirements.txt - -if [ "$DEBUG" != "false" ]; -then - pip install -r dev-requirements.txt -fi - -npm ci - -./scripts/build-static.sh - -./manage.py collectstatic --noinput --clear -v 2 +./scripts/setup-python.sh