From 0d461bef5e6838784efd6f89346ec42d5ccd9706 Mon Sep 17 00:00:00 2001 From: Eliot Berriot <contact@eliotberriot.com> Date: Mon, 21 Jan 2019 13:54:46 +0100 Subject: [PATCH] Based Docker image on alpine to have a smaller (and faster to build) image --- api/Dockerfile | 58 ++++++++++++++++++-------- api/compose/django/dev-entrypoint.sh | 2 +- api/compose/django/entrypoint.sh | 2 +- api/docker/Dockerfile.test | 24 ----------- changes/changelog.d/alpine.enhancement | 1 + dev.yml | 48 +++++++++++---------- 6 files changed, 68 insertions(+), 67 deletions(-) delete mode 100644 api/docker/Dockerfile.test create mode 100644 changes/changelog.d/alpine.enhancement diff --git a/api/Dockerfile b/api/Dockerfile index f82ab3e8..92a4d7f4 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -1,25 +1,47 @@ -FROM python:3.6 +FROM alpine:3.8 -ENV PYTHONUNBUFFERED 1 - -# Requirements have to be pulled and installed here, otherwise caching won't work -RUN echo 'deb http://httpredir.debian.org/debian/ jessie-backports main' > /etc/apt/sources.list.d/ffmpeg.list -COPY ./requirements.apt /requirements.apt -RUN apt-get update; \ - grep "^[^#;]" requirements.apt | \ - grep -Fv "python3-dev" | \ - xargs apt-get install -y --no-install-recommends; \ - rm -rf /usr/share/doc/* /usr/share/locale/* +RUN \ + echo 'installing dependencies' && \ + apk add \ + bash \ + git \ + gettext \ + musl-dev \ + gcc \ + postgresql-dev \ + python3-dev \ + py3-psycopg2 \ + py3-pillow \ + libldap \ + ffmpeg \ + libpq \ + libmagic \ + libffi-dev \ + zlib-dev \ + openldap-dev && \ + \ + \ + ln -s /usr/bin/python3 /usr/bin/python +RUN mkdir /requirements COPY ./requirements/base.txt /requirements/base.txt -RUN pip install -r /requirements/base.txt - -COPY . /app - -# Since youtube-dl code is updated fairly often, we split it here -RUN pip install --upgrade youtube-dl +RUN \ + echo 'fixing requirements file for alpine' && \ + sed -i '/Pillow/d' /requirements/base.txt && \ + \ + \ + echo 'installing pip requirements' && \ + pip3 install --no-cache-dir --upgrade pip && \ + pip3 install --no-cache-dir setuptools wheel && \ + pip3 install --no-cache-dir -r /requirements/base.txt -WORKDIR /app +ARG install_dev_deps=0 +COPY ./requirements/*.txt /requirements/ +RUN \ + if [ "$install_dev_deps" = "1" ] ; then echo "Installing dev dependencies" && pip3 install --no-cache-dir -r /requirements/local.txt -r /requirements/test.txt ; else echo "Skipping dev deps installation" ; fi ENTRYPOINT ["./compose/django/entrypoint.sh"] CMD ["./compose/django/daphne.sh"] + +COPY . /app +WORKDIR /app diff --git a/api/compose/django/dev-entrypoint.sh b/api/compose/django/dev-entrypoint.sh index 6deeebb0..4ba16114 100755 --- a/api/compose/django/dev-entrypoint.sh +++ b/api/compose/django/dev-entrypoint.sh @@ -1,3 +1,3 @@ -#!/bin/bash +#!/bin/sh set -e exec "$@" diff --git a/api/compose/django/entrypoint.sh b/api/compose/django/entrypoint.sh index ac85f116..3fc06a41 100755 --- a/api/compose/django/entrypoint.sh +++ b/api/compose/django/entrypoint.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh set -e # This entrypoint is used to play nicely with the current cookiecutter configuration. # Since docker-compose relies heavily on environment variables itself for configuration, we'd have to define multiple diff --git a/api/docker/Dockerfile.test b/api/docker/Dockerfile.test deleted file mode 100644 index 9e3202f9..00000000 --- a/api/docker/Dockerfile.test +++ /dev/null @@ -1,24 +0,0 @@ -FROM python:3.6 - -ENV PYTHONUNBUFFERED 1 - -# Requirements have to be pulled and installed here, otherwise caching won't work -RUN echo 'deb http://httpredir.debian.org/debian/ jessie-backports main' > /etc/apt/sources.list.d/ffmpeg.list -COPY ./requirements.apt /requirements.apt -RUN apt-get update; \ - grep "^[^#;]" requirements.apt | \ - grep -Fv "python3-dev" | \ - xargs apt-get install -y --no-install-recommends; \ - rm -rf /usr/share/doc/* /usr/share/locale/* - -RUN mkdir /requirements -COPY ./requirements/base.txt /requirements/base.txt -RUN pip install -r /requirements/base.txt -COPY ./requirements/local.txt /requirements/local.txt -RUN pip install -r /requirements/local.txt -COPY ./requirements/test.txt /requirements/test.txt -RUN pip install -r /requirements/test.txt - -COPY . /app -WORKDIR /app -ENTRYPOINT ["compose/django/dev-entrypoint.sh"] diff --git a/changes/changelog.d/alpine.enhancement b/changes/changelog.d/alpine.enhancement new file mode 100644 index 00000000..a7744a87 --- /dev/null +++ b/changes/changelog.d/alpine.enhancement @@ -0,0 +1 @@ +Based Docker image on alpine to have a smaller (and faster to build) image diff --git a/dev.yml b/dev.yml index 3eac5e63..2b1e7c5f 100644 --- a/dev.yml +++ b/dev.yml @@ -39,17 +39,20 @@ services: networks: - internal - celeryworker: + api: env_file: - .env.dev - .env - build: + build: &backend context: ./api - dockerfile: docker/Dockerfile.test - links: - - postgres - - redis - command: celery -A funkwhale_api.taskapp worker -l debug -B + dockerfile: Dockerfile + args: + install_dev_deps: 1 + entrypoint: compose/django/dev-entrypoint.sh + command: python /app/manage.py runserver 0.0.0.0:${FUNKWHALE_API_PORT-5000} + volumes: + - ./api:/app + - "${MUSIC_DIRECTORY_PATH-./data/music}:/music:ro" environment: - "FUNKWHALE_HOSTNAME=${FUNKWHALE_HOSTNAME-localhost}" - "FUNKWHALE_HOSTNAME_SUFFIX=funkwhale.test" @@ -57,22 +60,23 @@ services: - "FUNKWHALE_PROTOCOL=${FUNKWHALE_PROTOCOL-http}" - "DATABASE_URL=postgresql://postgres@postgres/postgres" - "CACHE_URL=redis://redis:6379/0" - volumes: - - ./api:/app - - "${MUSIC_DIRECTORY_PATH-./data/music}:/music:ro" + links: + - postgres + - redis networks: - internal - api: + cap_add: + - SYS_PTRACE + + celeryworker: env_file: - .env.dev - .env - build: - context: ./api - dockerfile: docker/Dockerfile.test - command: python /app/manage.py runserver 0.0.0.0:${FUNKWHALE_API_PORT-5000} - volumes: - - ./api:/app - - "${MUSIC_DIRECTORY_PATH-./data/music}:/music:ro" + build: *backend + links: + - postgres + - redis + command: celery -A funkwhale_api.taskapp worker -l debug -B environment: - "FUNKWHALE_HOSTNAME=${FUNKWHALE_HOSTNAME-localhost}" - "FUNKWHALE_HOSTNAME_SUFFIX=funkwhale.test" @@ -80,13 +84,11 @@ services: - "FUNKWHALE_PROTOCOL=${FUNKWHALE_PROTOCOL-http}" - "DATABASE_URL=postgresql://postgres@postgres/postgres" - "CACHE_URL=redis://redis:6379/0" - links: - - postgres - - redis + volumes: + - ./api:/app + - "${MUSIC_DIRECTORY_PATH-./data/music}:/music:ro" networks: - internal - cap_add: - - SYS_PTRACE nginx: command: /entrypoint.sh env_file: -- GitLab