From 408e820e07a24beb632c3a1e3cff50c22a2ca4ef Mon Sep 17 00:00:00 2001 From: Jeremy Satterfield Date: Mon, 24 Aug 2020 22:29:04 +0000 Subject: [PATCH] add docker setup --- .dockerignore | 12 +++++++ .env | 12 +++++++ Dockerfile | 29 +++++++++++++++++ docker-compose.yml | 78 ++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 7 +++++ 5 files changed, 138 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..ee1a5c2 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,12 @@ +**/__pycache__ +**/*.pyc +.tox +.vim/ +.vim* +.git/ +*.sql +*.sql.gz +.mypy_cache/ +.pytest_cache/ +.ipython/ +docker-compose.yml diff --git a/.env b/.env index 2d7fb39..60a5429 100644 --- a/.env +++ b/.env @@ -1,3 +1,15 @@ +## DOCKER COMPOSE SETTINGS + +#COMPOSE_APP_PATH=. +#COMPOSE_CACHE_PORT=637 +#COMPOSE_DB_PORT=5432 +#COMPOSE_PYTHON_PKG_PATH= +#COMPOSE_TASK_CONCURRENCY=500 +#COMPOSE_TASK_LOG_LEVEL=info +#COMPOSE_WEB_LOG_LEVEL=info +#COMPOSE_WEB_PORT=8002 +#COMPOSE_WEB_WORKERS=4 + ## DJANGO SETTINGS ALLOWED_HOSTS=* diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b21f434 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +FROM python:3.8 + +ENV PYTHONUNBUFFERED=1 +ENV PYTHONDONTWRITEBYTECODE=1 +ENV POETRY_VIRTUALENVS_CREATE=0 + +RUN adduser --uid 1000 --gecos --quiet --disabled-password app_user + +RUN mkdir -p /usr/src/app \ + && chown app_user.app_user /usr/src/app +WORKDIR /usr/src/app + +COPY --chown=app_user requirements.txt ./ + +ARG PIP_ARGS= +RUN set -eux \ + && apt update \ + && apt upgrade -y \ + && apt install -y curl\ + && pip3 install -r requirements.txt $PIP_ARGS \ + && apt autoremove -y \ + && rm -rf /root/.cache/pip /var/cache/apt + +COPY --chown=app_user . . + +USER app_user + +CMD ["/usr/local/bin/gunicorn", "base_app.asgi:application", "-k", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:8000"] + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..4d63125 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,78 @@ +version: '3.8' + +volumes: + python-pkgs: + db-data: + cache-data: + +x-common: &app-common + build: + context: . + env_file: .env + environment: + DATABASE_URL: psql://db/base_app + DATABASE_USER: postgres + DATABASE_PASSWORD: postgres + CACHE_URL: redis://cache:6379/0 + CELERY_BROKER_URL: redis://cache:6379/1 + CELERY_RESULT_BACKEND: redis://cache:6379/2 + user: ${UID:-app_user} + volumes: + - ${COMPOSE_APP_PATH:-.}:/usr/src/app:delegated + - ${COMPOSE_PYTHON_PKG_PATH:-python-pkgs}:/usr/local/lib/python3.8/site-packages:z + +services: + app: + <<: *app-common + command: + - uvicorn + - base_app.asgi:application + - --workers=${COMPOSE_WEB_WORKERS:-4} + - --host=0.0.0.0 + - --port=8000 + - --reload + - --log-level=${COMPOSE_WEB_LOG_LEVEL:-info} + - --lifespan=off + restart: unless-stopped + depends_on: + - cache + - db + tty: true + ports: + - "${COMPOSE_WEB_PORT:-8002}:8000" + + task_worker: + <<: *app-common + command: + - celery + - worker + - --events + - --app + - base_app.celery + - --loglevel=${COMPOSE_TASK_LOG_LEVEL:-info} + - --pool=gevent + - --concurrency=${COMPOSE_TASK_CONCURRENCY:-100} + restart: unless-stopped + depends_on: + - cache + - db + + db: + image: postgres:12-alpine + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: base_app + restart: unless-stopped + ports: + - '${COMPOSE_DB_PORT:-0}:5432' + volumes: + - db-data:/var/lib/postgresql/data + + cache: + image: redis:5-alpine + restart: unless-stopped + volumes: + - cache-data:/var/lib/redis + ports: + - '${COMPOSE_CACHE_PORT:-0}:6379' diff --git a/requirements.txt b/requirements.txt index 8ef2a33..d5d811d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,6 +10,13 @@ djangorestframework>=3.11 python-json-logger structlog +# infra +gevent +gunicorn +psycopg2-binary +redis +uvicorn + # tests, linting, code quality bandit django-stubs