Skip to content

Commit

Permalink
add docker setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jsatt committed Aug 25, 2020
1 parent a054efe commit 408e820
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
**/__pycache__
**/*.pyc
.tox
.vim/
.vim*
.git/
*.sql
*.sql.gz
.mypy_cache/
.pytest_cache/
.ipython/
docker-compose.yml
12 changes: 12 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -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=*
Expand Down
29 changes: 29 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]

78 changes: 78 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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'
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 408e820

Please sign in to comment.