Escolar Documentos
Profissional Documentos
Cultura Documentos
2
Agenda
1. Introduction Django Stack
2. Run Django Stack
3. Common activity
4. Problems and solutions (P&S)
5. Snippets
6. Q&A
3
1. Introduction Django Stack
Nginx
Django
Redis Postgres
4
1. Introduction Django Stack: Project Structure
├── docker-compose.yml
├── docker-env
├── nginx
│ ├── Dockerfile
│ └── sites-enabled
│ └── django_project
├── production.yml
├── README.md
└── web
├── docker_django
│ ├── apps
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── Dockerfile
├── manage.py
├── requirements.txt
└── static
├── images
│ └── django-pony.png
└── main.css
5
1. Introduction Django Stack: docker-compose.yml
version: "2" web:
#restart: always
services: build: ./web
nginx: expose:
#restart: always - "8000"
build: ./nginx/ depends_on:
ports: - postgres
- "80:80" - redis
volumes: volumes:
- /www/static - ./web:/usr/src/app
volumes_from: - ./web/static:/usr/src/app/static
- web env_file: docker-env
depends_on: environment:
- web DEBUG: 'true'
command: /usr/local/bin/gunicorn
docker_django.wsgi:application -w 2 -b :8000
--access-logfile /var/log/gunicorn.log
6
1. Introduction Django Stack: docker-compose.yml
postgres: redis:
#restart: always #restart: always
image: postgres:9.5 image: redis:3.2
ports: ports:
- "5432:5432" - "6379:6379"
volumes: volumes:
- pgdata:/var/lib/postgresql/data/ - redisdata:/data
volumes:
pgdata:
redisdata:
7
2. Run Django Stack
# build stack
docker-compose build
# up stack
docker-compose up -d
# migrate data
docker-compose run --rm web /usr/local/bin/python manage.py
migrate
8
3. Common Activity: Run container
● View web
● Run command in container
● Show logs
● Connect database to container
9
3. Common Activity: Update git
10
3. Common Activity: Build container
11
4. P&S: Handle `docker stop`
● When call docker stop, docker will send SIGTERM to main process
inside the container. And after a grace period (default 10s), docker will
send SIGKILL.
● The main process need to handle the signal and graceful stop.
import sys, signal, time
while True:
time.sleep(6)
13
4. P&S: Startup order
Solution:
Reference: https://docs.docker.com/compose/startup-order/ 14
4. P&S: Startup order
wait-for-it.sh wait-for-postgres.sh
Use .dockerignore
16
4. P&S: Speed up `docker build`
Build cache
● The orders of command -> image layers -> build speed & image size &
storage capacity
17
4. P&S: Limit Resources
cpu_shares: 73
cpu_quota: 50000
cpuset: 0,1
mem_limit: 1000000000
memswap_limit: 2000000000
18
5. Snippets
#--- Docker stats ---
# stats all service in docker compose directory
docker stats `docker-compose ps | tail -n+3|awk '{print $1}'`
19
5. Snippets (cont.)
Save all the images on docker-compose.yml and deploy on machine not
connected to the internet
# Save Compressed Images
IMAGES=`grep '^\s*image' docker-compose.yml | sed 's/image://' | sort | uniq`
20
References
[1] https://github.com/tranhuucuong91/docker-training
[2] https://realpython.com/blog/python/django-development-with-docker-co
mpose-and-machine/
[3] https://github.com/pydanny/cookiecutter-django
[4] https://docs.docker.com/engine/reference/builder/
[5] https://docs.docker.com/compose/compose-file
21
Q&A
22