Rapid Development
With Docker Compose
Justin Crown
https://github.com/mrname/
https://github.com/
mrname/kiwi-pycon-2017/
Workflow Requirements
• Portable (various operating systems)
• Easy to operate
• Ability to rapidly iterate code changes
• Minimal setup
Objective
• Deploy a full stack Django Rest Framework
application with:
• Nginx reverse proxy
• PostgreSQL
• Django-Q for task scheduling with redis as a
backend
docker build

(run commands in

dockerfile,

create image)
Docker

Image
docker run
Docker

Container

(instance of

image)
Base Image
Dockerfile
Do not buffer output
Dockerfile
Copy all files from CWD of 

host to /src of container
Dockerfile
This is now the root of all commands
Dockerfile
Install requirements
Dockerfile
Expose application port in container
Dockerfile
Docker Compose Version
Nginx Service
Postgres Service
Image Based Service
Build Based Service
Image Used To Create Container
Directory of Dockerfile
Command to run in container
Load Env Vars From These Files
When Should The Container Restart?
Inline Environment Variables
Mount “.” of the host to “/src/“ of the container
Wait for these containers to start before we start this one
Forward port 8000 of host to port 8000 of container
docker-compose ps
List Running Containers
docker-compose up
Bring up containers, building or pulling as necessary
Now we have… things!
docker-compose exec
Execute commands on running containers
docker-compose run
Execute a single command in a ONE OFF container
Magical Hostname… Thanks Docker Compose!
Magical Hostname… Thanks Docker Compose!
Tips, Tricks, Caveats
Container LifeCycle
docker-compose start/stop/restart
Affects the EXISTING CONTAINER. NOTHING IS
CHANGED
Tips, Tricks, Caveats
Container LifeCycle
docker-compose up
• Recreates container if something has changed
• Avoid recreating dependent services with --no-deps
• Force build with --build
• Force recreate with --force-recreate
Tips, Tricks, Caveats
Container LifeCycle
docker-compose build
• Rebuilds the image, using cached layers if available. Force a completely fresh
build with --no-cache
• DOES NOT pull your base image if it exists. Force pull base with --pull
Tips, Tricks, Caveats
Container LifeCycle
docker-compose pull
• Exactly what you expect........
Tips, Tricks, Caveats
Multiple Services With Same Built Image
Build + Image means that the built image
will be tagged with the value of
the “image” argument
Tips, Tricks, Caveats
Networks
Source: https://docs.docker.com/compose/networking/#specify-custom-networks
Tips, Tricks, Caveats
Scaling and Load Balancing
Tips, Tricks, Caveats
Scaling and Load Balancing
Round Robin DNS FOR FREE!
Tips, Tricks, Caveats
Application Configuration (Environment Variables)
Tips, Tricks, Caveats
Application Configuration (Environment Variable Files)
Tips, Tricks, Caveats
Application Configuration (Inline Environment Variables)
These variables take precedence over the env file!
Provide key and value
Infer value from shell
• Remove volumes that do not require
persistence (i.e. - code)
• Use networks
• Remove extraneous port bindings
• Use prod environment variables
• Use prod WSGI server
• Use built image instead of building on host
Release Overview
• docker-compose build (or pull)
• docker-compose scale (updated service only)
• find old container (docker ps)
• stop old container
• remove old container
• docker-compose scale back down
Growing Up
• Kubernetes
• Docker Swarm
• Apache Mesos
• Amazon ECS
• Packer
Rapid Development With Docker Compose

Rapid Development With Docker Compose