diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7f6a67b532f69a2bba2efd1781c794a0e0c5dacc..be117712f5d61b964b121ebae0762d6e605d86ac 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -31,7 +31,7 @@ test-style: -e POSTGRES_DB=$POSTGRES_DB -e POSTGRES_USER=$POSTGRES_USER -v $(pwd)/persistent:/code/persistent - "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:$CI_COMMIT_SHA" + "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG/web-container:$CI_COMMIT_SHA" test - mv persistent/htmlcov htmlcov - mv persistent/tests_http_cache.sqlite tests_http_cache.sqlite @@ -94,17 +94,32 @@ pages: - docker pull "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:latest" || true # build the image while passing commit SHA and tagging the image with it - docker build + --target web-container --build-arg CI_COMMIT_REF_SLUG --build-arg CI_COMMIT_SHA - --cache-from "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:latest" - --cache-from "$CI_REGISTRY_IMAGE/master:latest" - --tag "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:$CI_COMMIT_SHA" - --tag "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:latest" + --cache-from "$CI_REGISTRY_IMAGE/web-container/$CI_COMMIT_REF_SLUG:latest" + --cache-from "$CI_REGISTRY_IMAGE/web-container/master:latest" + --tag "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG/web-container:$CI_COMMIT_SHA" + --tag "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG/web-container:latest" . # push image as latest for the current branch - - docker push "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:latest" + - docker push "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG/web-container:latest" # push image tagged with its sha - - docker push "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:$CI_COMMIT_SHA" + - docker push "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG/web-container:$CI_COMMIT_SHA" + # build the image while passing commit SHA and tagging the image with it + - docker build + --target celery-container + --build-arg CI_COMMIT_REF_SLUG + --build-arg CI_COMMIT_SHA + --cache-from "$CI_REGISTRY_IMAGE/celery-container/$CI_COMMIT_REF_SLUG:latest" + --cache-from "$CI_REGISTRY_IMAGE/celery-container/master:latest" + --tag "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG/celery-container:$CI_COMMIT_SHA" + --tag "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG/celery-container:latest" + . + # push image as latest for the current branch + - docker push "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG/celery-container:latest" + # push image tagged with its sha + - docker push "$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG/celery-container:$CI_COMMIT_SHA" build-ippisite: diff --git a/docker-compose.yaml b/docker-compose.yaml index 53e34572dbd44977da44be3afb3f8751c2abfa65..c034881c6023f34141fe466da294f50ba764c506 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,4 +1,4 @@ -version: "3" +version: "3.4" services: db-local: @@ -15,9 +15,9 @@ services: django: build: context: ./ippisite + target: web-container env_file: - ./ippisite/ippisite/settings.example.ini - entrypoint: /docker-entrypoint.sh command: gunicorn --reload --reload-engine inotify ippisite.wsgi -b 0.0.0.0:8000 volumes: - ./ippisite:/code # for dev purpose only !!! diff --git a/ippisite/Dockerfile b/ippisite/Dockerfile index fc42418898e971660573510e0beadb784c181146..a6c8509ed0a6f7cb596c1653bbf8dc4ea5eb9515 100644 --- a/ippisite/Dockerfile +++ b/ippisite/Dockerfile @@ -1,4 +1,7 @@ -FROM python:3.9 +############################################################################### +# BAse image containing libs, dependencies and code +############################################################################### +FROM python:3.9 AS base-container EXPOSE 8000 ENV PYTHONPATH "${PYTHONPATH}:/usr/lib/python3/dist-packages" @@ -25,14 +28,34 @@ RUN pip install -r requirements.txt COPY ./*-entrypoint.sh / RUN chmod a+x /*-entrypoint.sh +COPY . /code/ + + +############################################################################### +# django web app +############################################################################### +FROM base-container AS web-container + ENTRYPOINT ["/docker-entrypoint.sh"] -COPY . /code/ +CMD ["gunicorn", "--bind", ":8000", "ippisite.wsgi:application"] ENV STATIC_ROOT /static_root ENV STATIC_ROOT_ON_STARTUP_COPY="/tmp${STATIC_ROOT}_on_startup_copy" RUN python manage.py collectstatic --noinput -CMD ["gunicorn", "--bind", ":8000", "ippisite.wsgi:application"] +USER kiwi + + +############################################################################### +# celery worker +############################################################################### +FROM base-container AS celery-container + +ENTRYPOINT ["/docker-celery-entrypoint.sh"] + +CMD ["celery", "multi", "start", "worker", "-Q", "celery-80", "-A", "ippisite", "--pidfile=/ippidb-80-celery/celery.pid", "--logfile=/ippidb-80-celery/celery.log", "--loglevel=INFO", "--time-limit=172800", "--concurrency=1", "--max-tasks-per-child=1"] + +CMD ["celery", "-A", "ippisite", "worker", "-l", "info"] USER kiwi \ No newline at end of file diff --git a/ippisite/docker-celery-entrypoint.sh b/ippisite/docker-celery-entrypoint.sh new file mode 100644 index 0000000000000000000000000000000000000000..9c1d382c5bd2eaa55682c2745b07846026b50fcd --- /dev/null +++ b/ippisite/docker-celery-entrypoint.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +function msg_info { + echo -e "\033[1;32m$1\033[0m" +} + +function msg_warning { + echo -e "\033[1;33m$1\033[0m" +} + +function msg_error { + echo -e "\033[1;31m$1\033[0m" +} + +cd /code + +if [ "$1" == "hold_on" ]; then + msg_info "holding on util you delete /tmp/hold_on" + touch /tmp/hold_on + while [ -e "/tmp/hold_on" ]; do + sleep 1 ; + echo "holding on" ; + done +fi + +exec "$@" diff --git a/ippisite/notes.md b/ippisite/notes.md index e8519eb6d1693b029d43cd724077ed0946a711fd..5cc274ef7208b4222919cb14b5fa43cf5bcd0de0 100755 --- a/ippisite/notes.md +++ b/ippisite/notes.md @@ -1,6 +1,6 @@ How to build and run the app ``` -docker build . -t test_ippidb && docker run -it -e ALLOWED_HOSTS=localhost -e SECRET_KEY=a -e "STATIC_URL=/static" -e "POSTGRES_NAME=postgres" -e "POSTGRES +docker build . -t test_ippidb --target web-container && docker run -it -e ALLOWED_HOSTS=localhost -e SECRET_KEY=a -e "STATIC_URL=/static" -e "POSTGRES_NAME=postgres" -e "POSTGRES _USER=postgres" -e "POSTGRES_PASSWORD=postgres" -e "POSTGRES_HOST=db" -e "USE_SQLITE_AS_DB=True" -v $(pwd):/code -p 8095:8000 test_ippidb ``` \ No newline at end of file