From 793249576d26ab448d6f55497a6bef05c8e27ede Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Wed, 3 Jul 2019 15:04:02 +0200 Subject: Setting up a method for automatically applying migrations in production. Making a separate Dockerfile for local docker-compose builds. --- docker/app/Dockerfile | 14 ++++++++-- docker/app/Dockerfile.local | 49 +++++++++++++++++++++++++++++++++ docker/app/migrate_and_serve.sh | 20 -------------- docker/app/scripts/migrate.sh | 10 +++++++ docker/app/scripts/migrate_and_serve.sh | 20 ++++++++++++++ docker/pysite.dockerapp | 2 +- 6 files changed, 91 insertions(+), 24 deletions(-) create mode 100644 docker/app/Dockerfile.local delete mode 100755 docker/app/migrate_and_serve.sh create mode 100755 docker/app/scripts/migrate.sh create mode 100755 docker/app/scripts/migrate_and_serve.sh (limited to 'docker') diff --git a/docker/app/Dockerfile b/docker/app/Dockerfile index 52cc6b32..c91c013a 100644 --- a/docker/app/Dockerfile +++ b/docker/app/Dockerfile @@ -1,14 +1,17 @@ FROM bitnami/python:3.7-prod +# I have no idea what this does. STOPSIGNAL SIGQUIT ARG EXTRAS=deploy +# Create a user. RUN adduser \ --disabled-login \ --no-create-home \ --uid 1500 \ pysite +# Install prerequisites needed to complete the dependency installation. RUN apt-get update -y \ && \ apt-get install --no-install-recommends -y \ @@ -21,24 +24,29 @@ RUN apt-get update -y \ && \ rm -rf /var/lib/apt/lists/* +# Set up the working directory. WORKDIR /app - COPY Pipfile Pipfile.lock /app/ +# Pip install the stuff we'll need. RUN rm -r /opt/bitnami/python/lib/python3.*/site-packages/setuptools* && \ pip install --no-cache-dir -U setuptools - RUN python3 -m pip install pipenv \ && python3 -m pipenv install --system --deploy \ && pip install uwsgi==2.0.18 +# Copy everything into the docker environment. COPY . . -RUN SECRET_KEY=placeholder DATABASE_URL=sqlite:// python3 manage.py collectstatic --no-input --clear --verbosity 0 +# RUN SECRET_KEY=placeholder DATABASE_URL=sqlite:// python3 manage.py collectstatic --no-input --clear --verbosity 0 +# Remove the prerequisites, dependency installation is now complete. RUN apt-get purge -y \ gcc \ libc-dev \ libpq-dev +# Migrate, collect and start the app. +RUN chmod +x /docker/app/scripts/migrate.sh +ENTRYPOINT ["/docker/app/scripts/migrate.sh"] CMD ["uwsgi", "--ini", "docker/app/uwsgi.ini"] diff --git a/docker/app/Dockerfile.local b/docker/app/Dockerfile.local new file mode 100644 index 00000000..c332c757 --- /dev/null +++ b/docker/app/Dockerfile.local @@ -0,0 +1,49 @@ +FROM bitnami/python:3.7-prod + +# I have no idea what this does. +STOPSIGNAL SIGQUIT +ARG EXTRAS=deploy + +# Create a user. +RUN adduser \ + --disabled-login \ + --no-create-home \ + --uid 1500 \ + pysite + +# Install prerequisites needed to complete the dependency installation. +RUN apt-get update -y \ + && \ + apt-get install --no-install-recommends -y \ + gcc \ + libc-dev \ + libpq-dev \ + git \ + && \ + apt-get clean \ + && \ + rm -rf /var/lib/apt/lists/* + +# Set up the working directory. +WORKDIR /app +COPY Pipfile Pipfile.lock /app/ + +# Pip install the stuff we'll need. +RUN rm -r /opt/bitnami/python/lib/python3.*/site-packages/setuptools* && \ + pip install --no-cache-dir -U setuptools +RUN python3 -m pip install pipenv \ + && python3 -m pipenv install --system --deploy \ + && pip install uwsgi==2.0.18 + +# Copy everything into the docker environment. +COPY . . + +RUN SECRET_KEY=placeholder DATABASE_URL=sqlite:// python3 manage.py collectstatic --no-input --clear --verbosity 0 + +# Remove the prerequisites, dependency installation is now complete. +RUN apt-get purge -y \ + gcc \ + libc-dev \ + libpq-dev + +CMD ["uwsgi", "--ini", "docker/app/uwsgi.ini"] diff --git a/docker/app/migrate_and_serve.sh b/docker/app/migrate_and_serve.sh deleted file mode 100755 index 42bf67a3..00000000 --- a/docker/app/migrate_and_serve.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh -eu - -### NOTE -# This file is intended to be used by local setups. -# You do not want to run the Django development server -# in production. The default Dockerfile command will -# run using uWSGI, this script is provided purely as -# a convenience to run migrations and start a development server. - -echo [i] Applying migrations. -python manage.py migrate --verbosity 1 - -echo [i] Collecting static files. -python manage.py collectstatic --no-input --clear --verbosity 0 - -echo [i] Creating a superuser. -echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('admin', 'admin', 'admin') if not User.objects.filter(username='admin').exists() else print('Admin user already exists')" | python manage.py shell - -echo [i] Starting server. -python manage.py runserver 0.0.0.0:8000 diff --git a/docker/app/scripts/migrate.sh b/docker/app/scripts/migrate.sh new file mode 100755 index 00000000..94b5f7c4 --- /dev/null +++ b/docker/app/scripts/migrate.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +echo --- Applying migrations --- +python manage.py migrate --verbosity 1 + +echo --- Collecting static files --- +python manage.py collectstatic --no-input --clear --verbosity 0 + +echo --- Starting uwsgi --- +exec "$@" # This runs the CMD at the end of the Dockerfile diff --git a/docker/app/scripts/migrate_and_serve.sh b/docker/app/scripts/migrate_and_serve.sh new file mode 100755 index 00000000..42bf67a3 --- /dev/null +++ b/docker/app/scripts/migrate_and_serve.sh @@ -0,0 +1,20 @@ +#!/bin/sh -eu + +### NOTE +# This file is intended to be used by local setups. +# You do not want to run the Django development server +# in production. The default Dockerfile command will +# run using uWSGI, this script is provided purely as +# a convenience to run migrations and start a development server. + +echo [i] Applying migrations. +python manage.py migrate --verbosity 1 + +echo [i] Collecting static files. +python manage.py collectstatic --no-input --clear --verbosity 0 + +echo [i] Creating a superuser. +echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('admin', 'admin', 'admin') if not User.objects.filter(username='admin').exists() else print('Admin user already exists')" | python manage.py shell + +echo [i] Starting server. +python manage.py runserver 0.0.0.0:8000 diff --git a/docker/pysite.dockerapp b/docker/pysite.dockerapp index 2426008e..4e90ff87 100644 --- a/docker/pysite.dockerapp +++ b/docker/pysite.dockerapp @@ -13,7 +13,7 @@ services: django: build: context: . - command: docker/app/migrate_and_serve.sh + command: docker/app/scripts/migrate_and_serve.sh ports: - "127.0.0.1:4000:4000" environment: -- cgit v1.2.3