Leonardo & Docker
Requirements
-
docker
- docker-compose
Basic setup
Leonardo under Gunicorn worker and Postgresql, Memcached as Cache backend and Nginx as server.
git clone https://github.com/django-leonardo/django-leonardo cd django-leonardo cat docker-compose.yml web: restart: always build: . expose: - "8000" links: - postgres:postgres - memcached:memcached env_file: ./contrib/django/.env ports: - "8000:8000" volumes: - /var/lib/leonardo/static - /var/lib/leonardo/media command: > bash -c "python /usr/lib/leonardo/myproject/manage.py makemigrations && python /usr/lib/leonardo/myproject/manage.py migrate --noinput && python /usr/lib/leonardo/myproject/manage.py sync_all -f && gunicorn leonardo_site.wsgi:application -w 2 -b :8000" nginx: restart: always build: ./contrib/nginx ports: - "80:80" volumes: - /var/lib/leonardo/static - /var/lib/leonardo/media volumes_from: - web links: - web:web postgres: restart: always image: postgres:latest env_file: ./contrib/django/.env volumes_from: - data ports: - "5432:5432" memcached: image: memcached ports: - "11211:11211" data: env_file: ./contrib/django/.env restart: always image: postgres:latest volumes: - /var/lib/postgresql command: "true" docker-compose up docker-compose logs
Firstly when you start web conainer we create database schema and compress static files, then you can run the scale command
note: run docker-compose build --no-cache for building fresh image
Scaling
Now we replace Nginx with HAProxy and scale web server.
cd contrib/haproxy cat docker-compose.yml web: restart: always build: ../../ expose: - "8000" .. lb: image: tutum/haproxy links: - web ports: - "80:80" environment: - BACKEND_PORT=8000 - BALANCE=roundrobin .. docker-compose up -d Creating haproxy_memcached_1 Creating haproxy_data_1 Creating haproxy_postgres_1 Creating haproxy_web_1 Creating haproxy_lb_1 docker-compose scale web=3 Creating and starting 2 ... done Creating and starting 3 ... done docker-compose up --force-recreate -d Recreating haproxy_memcached_1 Recreating haproxy_data_1 Recreating haproxy_postgres_1 Recreating haproxy_web_2 Recreating haproxy_web_1 Recreating haproxy_lb_1 docker-compose ps Name Command State Ports --------------------------------------------------------------------------------------------------------- haproxy_data_1 /docker-entrypoint.sh true Restarting 5432/tcp haproxy_lb_1 python /haproxy/main.py Up 1936/tcp, 443/tcp, 0.0.0.0:80->80/tcp haproxy_memcached_1 /entrypoint.sh memcached Up 0.0.0.0:11211->11211/tcp haproxy_postgres_1 /docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp haproxy_web_1 bash -c python /usr/lib/le ... Up 8000/tcp haproxy_web_2 bash -c python /usr/lib/le ... Up 8000/tcp haproxy_web_3 bash -c python /usr/lib/le ... Up 8000/tcp docker logs
note: you must wait for migrations before run scale command
Read More
- https://docs.docker.com/engine/userguide/dockerimages/
- https://github.com/django-leonardo/django-leonardo/blob/master/docker-compose.yml
- https://github.com/django-leonardo/django-leonardo/blob/master/contrib/haproxy/docker-compose.yml