Skip to content

Commit

Permalink
🐳 Added quickstart docker
Browse files Browse the repository at this point in the history
  • Loading branch information
joeribekker committed Oct 22, 2020
1 parent 0828564 commit 25f7dbb
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 76 deletions.
78 changes: 28 additions & 50 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,91 +1,69 @@
# This is a multi-stage build file, which means a stage is used to build
# the backend (dependencies), the frontend stack and a final production
# stage re-using assets from the build stages. This keeps the final production
# image minimal in size.

# Stage 1 - Backend build environment
# includes compilers and build tooling to create the environment
FROM python:3.7-buster AS backend-build
# Stage 1 - Compile needed python dependencies
FROM python:3.7-buster AS build

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

WORKDIR /app
RUN mkdir /app/src

# Ensure we use the latest version of pip
RUN pip install pip setuptools -U
COPY ./requirements /app/requirements
RUN pip install pip setuptools -U
RUN pip install -r requirements/production.txt


# Stage 2 - Install frontend deps and build assets
FROM node:13-buster AS frontend-build

RUN apt-get update && apt-get install -y --no-install-recommends \
git \
&& rm -rf /var/lib/apt/lists/*
# Stage 2 - build frontend
FROM mhart/alpine-node:10 AS frontend-build

WORKDIR /app

# copy configuration/build files
COPY ./build /app/build/
COPY ./*.json ./*.js ./.babelrc /app/

# install WITH dev tooling
COPY ./*.json /app/
RUN npm ci

# copy source code
COPY ./src /app/src
COPY ./gulpfile.js ./webpack.config.js ./.babelrc /app/
COPY ./build /app/build/

# build frontend
COPY src/objecttypes/sass/ /app/src/objecttypes/sass/
COPY src/objecttypes/js/ /app/src/objecttypes/js/
RUN npm run build


# Stage 3 - Build docker image suitable for production
FROM python:3.7-buster
# Stage 3 - Build docker image suitable for execution and deployment
FROM python:3.7-buster AS production

# Stage 3.1 - Set up the needed production dependencies
# install all the dependencies for GeoDjango
RUN apt-get update && apt-get install -y --no-install-recommends \
procps \
vim \
postgresql-client \
# lxml deps
# libxslt \
&& rm -rf /var/lib/apt/lists/*

COPY --from=build /usr/local/lib/python3.7 /usr/local/lib/python3.7
COPY --from=build /usr/local/bin/uwsgi /usr/local/bin/uwsgi

# Stage 3.2 - Copy source code
WORKDIR /app
COPY ./bin/docker_start.sh /start.sh
RUN mkdir /app/log
RUN mkdir /app/media

# copy backend build deps
COPY --from=backend-build /usr/local/lib/python3.7 /usr/local/lib/python3.7
COPY --from=backend-build /usr/local/bin/uwsgi /usr/local/bin/uwsgi
COPY --from=backend-build /app/src/ /app/src/
RUN mkdir /app/log /app/config

# copy frontend build statics
COPY --from=frontend-build /app/src/objecttypes/static /app/src/objecttypes/static

# copy source code
COPY --from=frontend-build /app/src/objecttypes/static/css /app/src/objecttypes/static/css
COPY --from=frontend-build /app/src/objecttypes/static/js /app/src/objecttypes/static/js
COPY ./src /app/src

RUN useradd -M -u 1000 maykin
RUN chown -R maykin /app

# drop privileges
USER maykin

ARG COMMIT_HASH
ARG RELEASE
ENV GIT_SHA=${COMMIT_HASH}
ENV RELEASE=${RELEASE}

ENV DJANGO_SETTINGS_MODULE=objecttypes.conf.docker

ARG SECRET_KEY=dummy

# Run collectstatic, so the result is already included in the image
RUN python src/manage.py collectstatic --noinput

LABEL org.label-schema.vcs-ref=$COMMIT_HASH \
org.label-schema.vcs-url="https://github.com/maykinmedia/objecttypes-api" \
org.label-schema.version=$RELEASE \
org.label-schema.name="Objecttypes API"

EXPOSE 8000
CMD ["/start.sh"]
CMD ["/start.sh"]
6 changes: 3 additions & 3 deletions README.NL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ Quickstart

.. code:: bash
$ wget https://raw.githubusercontent.com/maykinmedia/objecttypes-api/master/docker-compose.yml
$ docker-compose up
$ wget https://raw.githubusercontent.com/maykinmedia/objecttypes-api/master/docker-compose-quickstart.yml
$ docker-compose -f docker-compose-quickstart.yml up -d
$ docker-compose exec web src/manage.py createsuperuser
2. In de browser, navigeer naar ``http://localhost:8000/`` om de admin en de
2. In de browser, navigeer naar ``http://localhost:8001/`` om de admin en de
API te benaderen.


Expand Down
7 changes: 3 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Objecttypes API
:Version: 0.1.0
:Source: https://github.com/maykinmedia/objecttypes-api
:Keywords: objects, assets, zaakobjecten
:PythonVersion: 3.7

|docs|

Expand Down Expand Up @@ -57,11 +56,11 @@ Quickstart

.. code:: bash
$ wget https://raw.githubusercontent.com/maykinmedia/objecttypes-api/master/docker-compose.yml
$ docker-compose up
$ wget https://raw.githubusercontent.com/maykinmedia/objecttypes-api/master/docker-compose-quickstart.yml
$ docker-compose -f docker-compose-quickstart.yml up -d
$ docker-compose exec web src/manage.py createsuperuser
2. In the browser, navigate to ``http://localhost:8000/`` to access the admin
2. In the browser, navigate to ``http://localhost:8001/`` to access the admin
and the API.


Expand Down
24 changes: 18 additions & 6 deletions bin/docker_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export PGPORT=${DB_PORT:-5432}
fixtures_dir=${FIXTURES_DIR:-/app/fixtures}

uwsgi_port=${UWSGI_PORT:-8000}
uwsgi_processes=${UWSGI_PROCESSES:-4}
uwsgi_threads=${UWSGI_THREADS:-1}
uwsgi_processes=${UWSGI_PROCESSES:-2}
uwsgi_threads=${UWSGI_THREADS:-2}

mountpoint=${SUBPATH:-/}

until pg_isready; do
>&2 echo "Waiting for database connection..."
Expand All @@ -24,18 +26,28 @@ done
>&2 echo "Apply database migrations"
python src/manage.py migrate

# Load any JSON fixtures present
if [ -d $fixtures_dir ]; then
echo "Loading fixtures from $fixtures_dir"

for fixture in $(ls "$fixtures_dir/"*.json)
do
echo "Loading fixture $fixture"
python src/manage.py loaddata $fixture
done
fi

# Start server
>&2 echo "Starting server"
uwsgi \
--http :$uwsgi_port \
--http-keepalive \
--module objecttypes.wsgi \
--manage-script-name \
--mount $mountpoint=objecttypes.wsgi:application \
--static-map /static=/app/static \
--static-map /media=/app/media \
--chdir src \
--enable-threads \
--processes $uwsgi_processes \
--threads $uwsgi_threads \
--post-buffering=8192 \
--buffer-size=65535
# processes & threads are needed for concurrency without nginx sitting inbetween
--buffer-size=65535
19 changes: 19 additions & 0 deletions docker-compose-quickstart.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '3'

services:
db:
image: postgres
environment:
- POSTGRES_USER=${DB_USER:-objecttypes}
- POSTGRES_PASSWORD=${DB_PASSWORD:-objecttypes}

web:
build: .
image: maykinmedia/objecttypes-api:latest
environment:
- DJANGO_SETTINGS_MODULE=objecttypes.conf.docker
- SECRET_KEY=${SECRET_KEY:-fgv=c0hz&tl*8*3m3893@m+1pstrvidc9e^5@fpspmg%cy$15d}
ports:
- 8001:8000
depends_on:
- db
8 changes: 3 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# Inspired by https://docs.docker.com/compose/django/
version: '3'

services:
db:
# NOTE: No persistance storage configured.
# See: https://hub.docker.com/_/postgres/
image: postgres
# NOTE: this works for bitnami, not sure if this works for regular
# postgres image
volumes:
- ./docker-init-db.sql:/docker-entrypoint-initdb.d/init_db.sql
environment:
- POSTGRES_USER=${DB_USER:-objecttypes}
- POSTGRES_PASSWORD=${DB_PASSWORD:-objecttypes}

web:
build: .
Expand Down
4 changes: 0 additions & 4 deletions docker-init-db.sql

This file was deleted.

8 changes: 4 additions & 4 deletions src/objecttypes/conf/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

from django.core.exceptions import ImproperlyConfigured

os.environ.setdefault("DB_USER", os.getenv("DATABASE_USER", "postgres"))
os.environ.setdefault("DB_NAME", os.getenv("DATABASE_NAME", "postgres"))
os.environ.setdefault("DB_PASSWORD", os.getenv("DATABASE_PASSWORD", ""))
os.environ.setdefault("DB_HOST", os.getenv("DATABASE_HOST", "db"))
os.environ.setdefault("DB_USER", os.getenv("DB_USER", "objecttypes"))
os.environ.setdefault("DB_NAME", os.getenv("DB_NAME", "objecttypes"))
os.environ.setdefault("DB_PASSWORD", os.getenv("DB_PASSWORD", "objecttypes"))
os.environ.setdefault("DB_HOST", os.getenv("DB_HOST", "db"))

from .base import * # noqa isort:skip

Expand Down

0 comments on commit 25f7dbb

Please sign in to comment.