From 3a3d1227558a2c405456e3ddab451675e7bbd552 Mon Sep 17 00:00:00 2001 From: jfriedli Date: Wed, 28 Aug 2019 08:33:28 -0700 Subject: [PATCH] Resolve "Create a docker image" --- .gitlab-ci.yml | 17 +++++++++++++++++ Dockerfile | 8 -------- Dockerfile.development | 15 +++++++++++++++ Dockerfile.production | 24 ++++++++++++++++++++++++ README.md | 21 ++++++++++++++++++++- config/uwsgi.config | 2 +- docker-compose.yml | 4 +++- main.py | 3 ++- startup-server.sh | 3 +++ 9 files changed, 85 insertions(+), 12 deletions(-) delete mode 100644 Dockerfile create mode 100644 Dockerfile.development create mode 100644 Dockerfile.production create mode 100644 startup-server.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9c2b39e..481d32a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,9 +1,14 @@ image: debian:testing stages: + - build - linting - test +variables: + CONTAINER_TEST_IMAGE: registry.0xacab.org/jvoisin/mat2-web:$CI_BUILD_REF_NAME + CONTAINER_RELEASE_IMAGE: registry.0xacab.org/jvoisin/mat2-web:latest + pyflakes: stage: linting script: @@ -28,3 +33,15 @@ tests:debian: - pip3 install -r requirements.txt - python3-coverage run --branch --include main.py -m unittest discover -s test - python3-coverage report -m + +build-docker: + stage: build + image: + name: gcr.io/kaniko-project/executor:debug + entrypoint: [""] + script: + - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json + - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile.production --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG + only: + - tags + - master \ No newline at end of file diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 5ca3fd8..0000000 --- a/Dockerfile +++ /dev/null @@ -1,8 +0,0 @@ -FROM python:3.7 -ADD . /mat2-web -WORKDIR /mat2-web -RUN apt-get update -RUN apt install -y python3-gi python3-gi-cairo gir1.2-poppler-0.18 \ -gir1.2-gdkpixbuf-2.0 libimage-exiftool-perl libgirepository1.0-dev -RUN pip install -r requirements.txt -CMD flask run --host=0.0.0.0 \ No newline at end of file diff --git a/Dockerfile.development b/Dockerfile.development new file mode 100644 index 0000000..b804959 --- /dev/null +++ b/Dockerfile.development @@ -0,0 +1,15 @@ +FROM python:3.7 +ADD . /mat2-web +WORKDIR /mat2-web +RUN apt-get update \ +&& apt-get install --no-install-recommends --no-install-suggests --yes \ + python3-gi \ + python3-gi-cairo \ + gir1.2-poppler-0.18 \ + gir1.2-gdkpixbuf-2.0 \ + libimage-exiftool-perl \ + libgirepository1.0-dev \ +&& pip install -r requirements.txt \ +&& rm -rf /var/cache/apt/* /var/lib/apt/lists/* +CMD flask run --host=0.0.0.0 + diff --git a/Dockerfile.production b/Dockerfile.production new file mode 100644 index 0000000..1d5b0a1 --- /dev/null +++ b/Dockerfile.production @@ -0,0 +1,24 @@ +From debian:buster-slim +WORKDIR /var/www/mat2-web +COPY . /var/www/mat2-web +RUN apt-get update \ +&& apt-get install --no-install-recommends --no-install-suggests --yes \ + systemd \ + mat2 \ + uwsgi \ + uwsgi-plugin-python3 \ + nginx-light \ + python3-pip \ + python3-setuptools \ + python3-wheel \ +&& rm -rf /var/cache/apt/* /var/lib/apt/lists/* \ +&& pip3 install -r requirements.txt \ +&& mkdir ./uploads \ +&& chown -R www-data:www-data . \ +&& cp ./config/uwsgi.config /etc/uwsgi/apps-enabled/mat2-web.ini \ +&& rm /etc/nginx/sites-enabled/default \ +&& mkdir -p /etc/nginx/sites-enabled/ \ +&& cp ./config/nginx.config /etc/nginx/sites-enabled/mat2.conf \ +&& chmod +x ./startup-server.sh + +CMD ["./startup-server.sh"] \ No newline at end of file diff --git a/README.md b/README.md index fd723c8..2546c5c 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ configure, feel free to copy [this file](https://0xacab.org/jvoisin/mat2-web/tree/master/config/uwsgi.config) to `/etc/uwsgi/apps-enabled/mat2-web.ini` and [this one](https://0xacab.org/jvoisin/mat2-web/tree/master/config/nginx.config) -to `/etc/nginx/site-enabled/mat2-web`. +to `/etc/nginx/sites-enabled/mat2-web`. Nginx is the recommended web engine, but you can also use Apache if you prefer, by copying [this file](https://0xacab.org/jvoisin/mat2-web/tree/master/config/apache2.config) @@ -73,6 +73,18 @@ collector cronjob to remove leftover files. Besides, it can create a the uploads folder, to ensure that the uploaded files won't be recoverable between reboots. + +# Deploy using Docker +You can find the ready to run docker image here: +https://0xacab.org/jvoisin/mat2-web/container_registry + +Example: +`docker run -p 80:80 -d -e MAT2_ALLOW_ORIGIN_WHITELIST='https://myhost1.org' registry.0xacab.org/jvoisin/mat2-web:latest` + +Make sure to add +`find /var/www/mat2-web/uploads/ -type f -mtime +1 -exec rm {} \;` as cron job +run inside the container. + # Development Install docker and docker-compose and then run `docker-compose up` to setup the docker dev environment. Mat2-web is now accessible on your host machine at `localhost:5000`. @@ -137,6 +149,13 @@ The `file` parameter is the base64 encoded file which will be cleaned. ] ``` +# Docker +There are two Dockerfiles present in this repository. The file called `Dockerfile.development` is used for development +and `Dockerfile.production` is used for production deployments. + +You can find the automated docker builds in the registry of this +repository: https://0xacab.org/jvoisin/mat2-web/container_registry + # Custom templates You can override the default templates from `templates/` by putting replacements diff --git a/config/uwsgi.config b/config/uwsgi.config index 7108d33..177ddaf 100644 --- a/config/uwsgi.config +++ b/config/uwsgi.config @@ -2,7 +2,7 @@ module=main chdir = /var/www/mat2-web/ callable = app -wsgi-file = main.py +wsgi-file = /var/www/mat2-web/main.py master = true workers = 1 diff --git a/docker-compose.yml b/docker-compose.yml index fda006e..e758801 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,9 @@ version: '2' services: web: - build: . + build: + context: . + dockerfile: Dockerfile.development environment: - FLASK_APP=main.py - FLASK_ENV=development diff --git a/main.py b/main.py index 75299de..b059bfe 100644 --- a/main.py +++ b/main.py @@ -187,6 +187,7 @@ def create_app(test_config=None): return app +app = create_app() if __name__ == '__main__': # pragma: no cover - create_app().run() + app.run() diff --git a/startup-server.sh b/startup-server.sh new file mode 100644 index 0000000..d4449f7 --- /dev/null +++ b/startup-server.sh @@ -0,0 +1,3 @@ +#!/bin/bash +/etc/init.d/nginx restart +uwsgi --ini /etc/uwsgi/apps-enabled/mat2-web.ini \ No newline at end of file