From ba4fb7934947cb6fa931106d85cd2ab5c4f71524 Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Mon, 2 Sep 2019 16:43:19 +0200 Subject: [PATCH] docker: enhancement to choose light containers with Alpine and debian:slim (base) --- README.rst | 2 ++ cookiecutter.json | 3 ++- .../Dockerfile.base | 19 +++++++++++++++++++ .../docker-services.yml | 12 ++++++++++++ .../docker/haproxy/Dockerfile | 4 ++++ .../docker/nginx/Dockerfile | 4 ++++ .../docker/postgres/Dockerfile | 4 ++++ .../docker/wait-for-services.sh | 4 ++++ 8 files changed, 51 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index d9a80ba..f8e92b7 100644 --- a/README.rst +++ b/README.rst @@ -85,6 +85,8 @@ To generate correct files, please provide the following input to Cookiecutter: without one (`None`). `datamodel_extension_class` Name of the class of your custom datamodel. `datamodel_pid_name` Name of the Persistent IDentifier attribute of the datamodel. +`docker_size` Either you want to use Alpine Docker containers or normal + ones. ============================ ============================================================== Further documentation is available on https://invenio.readthedocs.io/ diff --git a/cookiecutter.json b/cookiecutter.json index a4f1946..f03a102 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -14,5 +14,6 @@ "elasticsearch": ["6", "5"], "datamodel": ["Custom", "None"], "datamodel_extension_class": "{{ cookiecutter.project_name | replace('-', '') | replace(' ', '') }}", - "datamodel_pid_name": "id" + "datamodel_pid_name": "id", + "docker_size": ["light", "normal"] } diff --git a/{{cookiecutter.project_shortname}}/Dockerfile.base b/{{cookiecutter.project_shortname}}/Dockerfile.base index 6ebf6fd..986ab1b 100644 --- a/{{cookiecutter.project_shortname}}/Dockerfile.base +++ b/{{cookiecutter.project_shortname}}/Dockerfile.base @@ -7,6 +7,25 @@ # and includes Pip, Pipenv, Node.js, NPM and some few standard libraries # Invenio usually needs. +{%- if cookiecutter.docker_size == 'light'%} +FROM inveniosoftware/debian10-python:3.6-slim +{%- else %} FROM inveniosoftware/centos7-python:3.6 +{%- endif %} COPY Pipfile Pipfile.lock ./ +{%- if cookiecutter.docker_size == 'light'%} +# As we get a tiny image, we install needed packages (to build UWSGI deps) +RUN set -ex \ + && buildDeps=' \ + gcc \ + python-dev \ +' \ +&& apt-get update \ +&& apt-get install --no-install-recommends -y \ + $buildDeps \ +&& pipenv install --deploy --system \ +&& apt-get purge -y --autoremove $buildDeps \ +&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* +{%- else %} RUN pipenv install --deploy --system +{%- endif %} diff --git a/{{cookiecutter.project_shortname}}/docker-services.yml b/{{cookiecutter.project_shortname}}/docker-services.yml index 2941d94..97c1bdf 100644 --- a/{{cookiecutter.project_shortname}}/docker-services.yml +++ b/{{cookiecutter.project_shortname}}/docker-services.yml @@ -39,14 +39,22 @@ services: - "80" - "443" cache: + {%- if cookiecutter.docker_size == 'light'%} + image: redis:alpine + {%- else %} image: redis + {%- endif %} restart: "always" read_only: true ports: - "6379:6379" db: {%- if cookiecutter.database == 'postgresql'%} + {%- if cookiecutter.docker_size == 'light'%} + image: postgres:9.6-alpine + {%- else %} image: postgres:9.6 + {%- endif %} restart: "always" environment: - "POSTGRES_USER={{cookiecutter.project_shortname}}" @@ -66,7 +74,11 @@ services: - "3306:3306" {%- endif %} mq: + {%- if cookiecutter.docker_size == 'light'%} + image: rabbitmq:3-management-alpine + {%- else %} image: rabbitmq:3-management + {%- endif %} restart: "always" ports: - "15672:15672" diff --git a/{{cookiecutter.project_shortname}}/docker/haproxy/Dockerfile b/{{cookiecutter.project_shortname}}/docker/haproxy/Dockerfile index 4d1e43e..20b87bc 100644 --- a/{{cookiecutter.project_shortname}}/docker/haproxy/Dockerfile +++ b/{{cookiecutter.project_shortname}}/docker/haproxy/Dockerfile @@ -1,5 +1,9 @@ {% include 'misc/header.py' %} +{%- if cookiecutter.docker_size == 'light'%} +FROM haproxy:1.8-alpine +{%- else %} FROM haproxy:1.8 +{%- endif %} RUN mkdir -p /usr/local/var/lib/haproxy/ COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg COPY haproxy_cert.pem /usr/local/etc/cert.pem diff --git a/{{cookiecutter.project_shortname}}/docker/nginx/Dockerfile b/{{cookiecutter.project_shortname}}/docker/nginx/Dockerfile index 8330063..6d57d64 100644 --- a/{{cookiecutter.project_shortname}}/docker/nginx/Dockerfile +++ b/{{cookiecutter.project_shortname}}/docker/nginx/Dockerfile @@ -1,5 +1,9 @@ {% include 'misc/header.py' %} +{%- if cookiecutter.docker_size == 'light'%} +FROM nginx:alpine +{%- else %} FROM nginx +{%- endif %} COPY nginx.conf /etc/nginx/nginx.conf COPY conf.d/* /etc/nginx/conf.d/ COPY test.key /etc/ssl/private/test.key diff --git a/{{cookiecutter.project_shortname}}/docker/postgres/Dockerfile b/{{cookiecutter.project_shortname}}/docker/postgres/Dockerfile index 578aeba..addf657 100644 --- a/{{cookiecutter.project_shortname}}/docker/postgres/Dockerfile +++ b/{{cookiecutter.project_shortname}}/docker/postgres/Dockerfile @@ -1,3 +1,7 @@ {% include 'misc/header.py' %} +{%- if cookiecutter.docker_size == 'light'%} +FROM postgres:9.6-alpine +{%- else %} FROM postgres:9.6 +{%- endif %} COPY ./init-app-db.sh /docker-entrypoint-initdb.d/10-init-app-db.sh diff --git a/{{cookiecutter.project_shortname}}/docker/wait-for-services.sh b/{{cookiecutter.project_shortname}}/docker/wait-for-services.sh index 03293c1..253500e 100755 --- a/{{cookiecutter.project_shortname}}/docker/wait-for-services.sh +++ b/{{cookiecutter.project_shortname}}/docker/wait-for-services.sh @@ -27,7 +27,11 @@ check_ready "MySQL" _db_check _es_check(){ curl --output /dev/null --silent --head --fail http://localhost:9200 &>/dev/null; } check_ready "Elasticsearch" _es_check +{%- if cookiecutter.docker_size == 'light'%} +_redis_check(){ docker-compose exec cache sh -c 'redis-cli ping' | grep 'PONG' &> /dev/null; } +{%- else %} _redis_check(){ docker-compose exec cache bash -c 'redis-cli ping' | grep 'PONG' &> /dev/null; } +{%- endif %} check_ready "Redis" _redis_check _rabbit_check(){ docker-compose exec mq bash -c "rabbitmqctl status" &>/dev/null; }