├── .gitignore ├── 01 - Introduction └── 0102 │ └── docker-compose.yml ├── 02 - Compose Basics ├── 0201 │ └── docker-compose.yml ├── 0202 │ └── docker-compose.yml └── 0203 │ ├── Makefile │ └── docker-compose.yml ├── 03 - Networking ├── 0302 │ └── docker-compose.yml ├── 0303 │ └── docker-compose.yml ├── 0304 │ ├── docker-compose.yml │ ├── scale.yml │ └── share.yml ├── 0305 │ └── docker-compose.yml └── 0307 │ └── docker-compose.yml ├── 04 - Volumes ├── 0402 │ ├── conf.d │ │ └── default.conf │ └── docker-compose.yml ├── 0403 │ └── docker-compose.yml └── 0404 │ └── docker-compose.yml ├── 05 - Logging ├── 0501 │ └── docker-compose.yml └── 0502 │ └── docker-compose.yml ├── 06 - Compose CLI ├── 0601 │ └── docker-compose.yml ├── 0603 │ └── docker-compose.yml ├── 0604 │ └── docker-compose.yml └── 0606 │ ├── .env │ ├── docker-compose.yml │ └── staging.env ├── 07 - Composing Compose ├── 0701 │ ├── docker-compose.merge.yml │ └── docker-compose.yml ├── 0702 │ ├── docker-compose.override.yml │ └── docker-compose.yml └── 0703 │ ├── base.yml │ └── docker-compose.yml ├── 08 - Compose in Production ├── .gitignore ├── deploy.sh ├── docker-compose.production.yml ├── docker-compose.yml └── provision.sh └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | volumes 2 | -------------------------------------------------------------------------------- /01 - Introduction/0102/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | hello-world: 4 | image: tutum/hello-world 5 | ports: 6 | - 8080:80 7 | -------------------------------------------------------------------------------- /02 - Compose Basics/0201/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | wordpress: 4 | image: wordpress:4.5 5 | links: 6 | - db 7 | ports: 8 | - 8080:80 9 | environment: 10 | WORDPRESS_DB_HOST: db:3306 11 | WORDPRESS_DB_PASSWORD: example 12 | restart: always 13 | 14 | db: 15 | image: mariadb 16 | environment: 17 | MYSQL_ROOT_PASSWORD: example 18 | -------------------------------------------------------------------------------- /02 - Compose Basics/0202/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | 3 | services: 4 | wordpress: 5 | image: wordpress:4.5 6 | depends_on: 7 | - db 8 | links: 9 | - db 10 | ports: 11 | - "8080:80" 12 | environment: 13 | WORDPRESS_DB_HOST: db:3306 14 | WORDPRESS_DB_USER: wordpress 15 | WORDPRESS_DB_PASSWORD: wordpress 16 | restart: always 17 | 18 | db: 19 | image: mariadb:10.1 20 | volumes: 21 | - "./volumes/db:/var/lib/mysql" 22 | restart: always 23 | environment: 24 | MYSQL_ROOT_PASSWORD: example 25 | MYSQL_DATABASE: wordpress 26 | MYSQL_USER: wordpress 27 | MYSQL_PASSWORD: wordpress 28 | -------------------------------------------------------------------------------- /02 - Compose Basics/0203/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: up down start stop cleanContainers clean 2 | 3 | up: 4 | docker-compose up $(opts) 5 | 6 | down: 7 | docker-compose down $(opts) 8 | 9 | start: 10 | docker-compose up -d $(opts) 11 | 12 | cleanContainers: 13 | docker-compose rm --force --all 14 | 15 | cleanVolumes: 16 | rm -rf volumes 17 | 18 | clean: cleanContainers cleanVolumes 19 | 20 | 21 | -------------------------------------------------------------------------------- /02 - Compose Basics/0203/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | 3 | services: 4 | wordpress: 5 | image: wordpress:4.5 6 | depends_on: 7 | - db 8 | links: 9 | - db 10 | ports: 11 | - "8080:80" 12 | environment: 13 | WORDPRESS_DB_HOST: db:3306 14 | WORDPRESS_DB_USER: wordpress 15 | WORDPRESS_DB_PASSWORD: wordpress 16 | restart: always 17 | 18 | db: 19 | image: mariadb:10.1 20 | volumes: 21 | - "./volumes/db:/var/lib/mysql" 22 | restart: always 23 | environment: 24 | MYSQL_ROOT_PASSWORD: example 25 | MYSQL_DATABASE: wordpress 26 | MYSQL_USER: wordpress 27 | MYSQL_PASSWORD: wordpress 28 | -------------------------------------------------------------------------------- /03 - Networking/0302/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | a: 4 | image: tutum/hello-world 5 | b: 6 | image: tutum/hello-world 7 | networks: 8 | default: 9 | driver: bridge 10 | -------------------------------------------------------------------------------- /03 - Networking/0303/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | a: 4 | image: tutum/hello-world 5 | networks: 6 | - frontend 7 | b: 8 | image: tutum/hello-world 9 | networks: 10 | - frontend 11 | - backend 12 | c: 13 | image: tutum/hello-world 14 | networks: 15 | - backend 16 | 17 | networks: 18 | frontend: 19 | driver: bridge 20 | backend: 21 | driver: bridge 22 | -------------------------------------------------------------------------------- /03 - Networking/0304/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | a: 4 | image: tutum/hello-world 5 | networks: 6 | - frontend 7 | b: 8 | image: tutum/hello-world 9 | networks: 10 | frontend: 11 | aliases: 12 | - web 13 | backend: 14 | aliases: 15 | - website 16 | c: 17 | image: tutum/hello-world 18 | networks: 19 | - backend 20 | 21 | networks: 22 | frontend: 23 | driver: bridge 24 | backend: 25 | driver: bridge 26 | -------------------------------------------------------------------------------- /03 - Networking/0304/scale.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | a: 4 | image: tutum/hello-world 5 | container_name: eh 6 | networks: 7 | frontend: 8 | aliases: 9 | - web 10 | b: 11 | image: tutum/hello-world 12 | networks: 13 | frontend: 14 | aliases: 15 | - web 16 | backend: 17 | aliases: 18 | - website 19 | c: 20 | image: tutum/hello-world 21 | networks: 22 | - backend 23 | 24 | networks: 25 | frontend: 26 | driver: bridge 27 | backend: 28 | driver: bridge 29 | -------------------------------------------------------------------------------- /03 - Networking/0304/share.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | a: 4 | image: tutum/hello-world 5 | networks: 6 | frontend: 7 | aliases: 8 | - web 9 | b: 10 | image: tutum/hello-world 11 | networks: 12 | frontend: 13 | aliases: 14 | - web 15 | backend: 16 | aliases: 17 | - website 18 | c: 19 | image: tutum/hello-world 20 | networks: 21 | - backend 22 | 23 | networks: 24 | frontend: 25 | driver: bridge 26 | backend: 27 | driver: bridge 28 | -------------------------------------------------------------------------------- /03 - Networking/0305/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | a: 4 | image: tutum/hello-world 5 | links: 6 | - b:bee 7 | - c:see 8 | networks: 9 | - frontend 10 | b: 11 | image: tutum/hello-world 12 | networks: 13 | - frontend 14 | - backend 15 | c: 16 | image: tutum/hello-world 17 | networks: 18 | - backend 19 | 20 | networks: 21 | frontend: 22 | driver: bridge 23 | backend: 24 | driver: bridge 25 | -------------------------------------------------------------------------------- /03 - Networking/0307/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | a: 4 | image: tutum/hello-world 5 | external_links: 6 | - hello:h 7 | networks: 8 | - frontend 9 | - superbridge 10 | b: 11 | image: tutum/hello-world 12 | networks: 13 | - frontend 14 | - backend 15 | c: 16 | image: tutum/hello-world 17 | networks: 18 | - backend 19 | 20 | networks: 21 | frontend: 22 | driver: bridge 23 | backend: 24 | driver: bridge 25 | superbridge: 26 | external: true 27 | -------------------------------------------------------------------------------- /04 - Volumes/0402/conf.d/default.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | server_name localhost; 4 | 5 | location / { 6 | return 200 "Hello, World!"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /04 - Volumes/0402/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | nginx: 4 | image: nginx 5 | volumes: 6 | - ./conf.d:/etc/nginx/conf.d:ro 7 | ports: 8 | - 80 9 | -------------------------------------------------------------------------------- /04 - Volumes/0403/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | worker: 4 | image: tutum/hello-world 5 | volumes: 6 | - results:/results 7 | reporting: 8 | image: tutum/hello-world 9 | volumes_from: 10 | - worker:ro 11 | 12 | volumes: 13 | results: 14 | 15 | 16 | -------------------------------------------------------------------------------- /04 - Volumes/0404/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | resizer: 4 | image: tutum/hello-world 5 | volumes: 6 | - images:/images:rw 7 | 8 | volumes: 9 | images: 10 | external: true 11 | -------------------------------------------------------------------------------- /05 - Logging/0501/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | worker: 4 | image: tutum/hello-world 5 | command: sh -c 'while true; do echo test; done' 6 | logging: 7 | driver: json-file 8 | options: 9 | max-file: "5" 10 | max-size: "1m" 11 | -------------------------------------------------------------------------------- /05 - Logging/0502/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | worker: 4 | image: tutum/hello-world 5 | command: sh -c 'while true; do echo test; done' 6 | logging: 7 | driver: syslog 8 | options: 9 | syslog-address: "udp://" 10 | -------------------------------------------------------------------------------- /06 - Compose CLI/0601/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | worker: 4 | image: tutum/hello-world 5 | -------------------------------------------------------------------------------- /06 - Compose CLI/0603/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | worker: 4 | image: tutum/hello-world 5 | ports: 6 | - 80 7 | -------------------------------------------------------------------------------- /06 - Compose CLI/0604/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | worker: 4 | image: tutum/hello-world 5 | ports: 6 | - 80 7 | -------------------------------------------------------------------------------- /06 - Compose CLI/0606/.env: -------------------------------------------------------------------------------- 1 | TAG=latest 2 | -------------------------------------------------------------------------------- /06 - Compose CLI/0606/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | worker: 4 | image: tutum/hello-world:${TAG} 5 | environment: 6 | - BAR 7 | env_file: 8 | - "staging.env" 9 | ports: 10 | - 80 11 | -------------------------------------------------------------------------------- /06 - Compose CLI/0606/staging.env: -------------------------------------------------------------------------------- 1 | FOO=1 2 | 3 | -------------------------------------------------------------------------------- /07 - Composing Compose/0701/docker-compose.merge.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | merge-example: 4 | image: tutum/hello-world:latest 5 | environment: 6 | FOO: 2 7 | BAR: 1 8 | ports: 9 | - 8080:80 10 | -------------------------------------------------------------------------------- /07 - Composing Compose/0701/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | merge-example: 4 | image: tutum/hello-world 5 | environment: 6 | FOO: 1 7 | ports: 8 | - 80 9 | -------------------------------------------------------------------------------- /07 - Composing Compose/0702/docker-compose.override.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | hello: 4 | ports: 5 | - 80:80 6 | 7 | -------------------------------------------------------------------------------- /07 - Composing Compose/0702/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | hello: 4 | image: tutum/hello-world 5 | ports: 6 | - 80 7 | 8 | -------------------------------------------------------------------------------- /07 - Composing Compose/0703/base.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | base: 4 | image: tutum/hello-world 5 | environment: 6 | SERVICE: base 7 | # ... other general "base" configuration 8 | -------------------------------------------------------------------------------- /07 - Composing Compose/0703/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | 3 | services: 4 | hello: 5 | extends: 6 | file: base.yml 7 | service: base 8 | ports: 9 | - 80 10 | environment: 11 | SERVICE: hello 12 | -------------------------------------------------------------------------------- /08 - Compose in Production/.gitignore: -------------------------------------------------------------------------------- 1 | docker-compose.override.yml 2 | -------------------------------------------------------------------------------- /08 - Compose in Production/deploy.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | # Simple script to illustrate deploying changes to production 3 | 4 | USAGE=$(cat < 6 | 7 | Execute docker-compose command against a remote host. 8 | 9 | Options: 10 | 11 | -h | --help Display this message 12 | -s | --swarm Name of swarm master machine (default: swarm-manager) 13 | USAGE 14 | ) 15 | 16 | while [ $# -gt 0 ]; do 17 | case "$1" in 18 | -h | --help) 19 | echo "$USAGE"; exit 2; shift;; 20 | -s | --swarm) 21 | SWARM_MASTER=$2; shift 2;; 22 | -*) echo "Invalid argument $1"; exit 1;; 23 | *) break;; 24 | esac 25 | done 26 | 27 | SWARM_MASTER=${SWARM_MASTER:-swarm-manager} 28 | 29 | eval $(docker-machine env --swarm $SWARM_MASTER) 30 | 31 | docker-compose \ 32 | --file docker-compose.yml \ 33 | --file docker-compose.production.yml \ 34 | $@ 35 | -------------------------------------------------------------------------------- /08 - Compose in Production/docker-compose.production.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | 3 | services: 4 | wordpress: 5 | environment: 6 | WORDPRESS_DB_HOST: db:3306 7 | WORDPRESS_DB_USER: wordpress 8 | WORDPRESS_DB_PASSWORD: 89222b5f81beee3bffb615699d7e717fdd8234f4 9 | db: 10 | environment: 11 | MYSQL_ROOT_PASSWORD: 4b5fd310b3b337dd946f19292b867a99b6f19f4a 12 | MYSQL_DATABASE: wordpress 13 | MYSQL_USER: wordpress 14 | MYSQL_PASSWORD: 89222b5f81beee3bffb615699d7e717fdd8234f4 15 | 16 | networks: 17 | backend: 18 | driver: overlay 19 | frontend: 20 | driver: overlay 21 | -------------------------------------------------------------------------------- /08 - Compose in Production/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | 3 | services: 4 | wordpress: 5 | image: wordpress:latest 6 | depends_on: 7 | - db 8 | ports: 9 | - "80" 10 | environment: 11 | WORDPRESS_DB_HOST: db:3306 12 | WORDPRESS_DB_USER: wordpress 13 | WORDPRESS_DB_PASSWORD: wordpress 14 | networks: 15 | - frontend 16 | restart: always 17 | 18 | db: 19 | image: mariadb:10.1 20 | volumes: 21 | - "db:/var/lib/mysql" 22 | environment: 23 | MYSQL_ROOT_PASSWORD: example 24 | MYSQL_DATABASE: wordpress 25 | MYSQL_USER: wordpress 26 | MYSQL_PASSWORD: wordpress 27 | networks: 28 | - frontend 29 | - backend 30 | restart: always 31 | 32 | volumes: 33 | db: 34 | 35 | networks: 36 | backend: 37 | frontend: 38 | -------------------------------------------------------------------------------- /08 - Compose in Production/provision.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | # Set up a Docker Swarm cluster of 3 nodes on Digital Ocean. 3 | # 4 | # Inspired by: https://docs.docker.com/swarm/provision-with-machine/ 5 | 6 | set -e 7 | 8 | USAGE=$(cat < 10 | 11 | Provision a 3-node Docker Swarm cluster with VirtualBox or Digital Ocean ($$) 12 | 13 | Options: 14 | 15 | -h | --help Display this message 16 | -d | --digital-ocean-token Use Digital Ocean; API token (or use DIGITALOCEAN_ACCESS_TOKEN) 17 | -c | --cleanup Do not provision any machines (flag). 18 | Cleans up any that have been provisioned 19 | 20 | Additional options: 21 | 22 | - DRIVER_OPTS - Used to specify additional arguments to docker-machine create 23 | USAGE 24 | ) 25 | 26 | while [ $# -gt 0 ]; do 27 | case "$1" in 28 | -h | --help) 29 | echo "$USAGE"; exit 2; shift;; 30 | -d | --digital-ocean-token) 31 | DIGITALOCEAN_ACCESS_TOKEN=$2; shift 2;; 32 | -c | --cleanup) 33 | CLEANUP=1; shift 1;; 34 | -*) echo "Invalid argument $1"; exit 1;; 35 | *) break;; 36 | esac 37 | done 38 | 39 | if [ -n "$CLEANUP" ]; then 40 | set +e 41 | docker-machine rm -f swarm-manager node-01 node-02 consul 42 | exit 0 43 | fi 44 | 45 | if [ -n "$DIGITALOCEAN_ACCESS_TOKEN" ]; then 46 | echo "Provisioning Swarm on Digital Ocean" 47 | export DIGITALOCEAN_ACCESS_TOKEN=$DIGITALOCEAN_ACCESS_TOKEN 48 | export MACHINE_DRIVER=digitalocean 49 | else 50 | echo "Provisioning Swarm with VirtualBox" 51 | export MACHINE_DRIVER=virtualbox 52 | fi 53 | 54 | create_and_launch_consul () { 55 | docker-machine create consul 56 | 57 | eval $(docker-machine env consul) 58 | 59 | docker run --detach \ 60 | --name consul \ 61 | --publish "8500:8500" \ 62 | --hostname "consul" \ 63 | progrium/consul -server -bootstrap 64 | 65 | # Wait for port 8500 to be open 66 | docker run --link consul --rm martin/wait 67 | } 68 | 69 | create_machine () { 70 | docker-machine create \ 71 | --swarm \ 72 | --swarm-discovery "consul://$(docker-machine ip consul):8500" \ 73 | --engine-opt "cluster-store=consul://$(docker-machine ip consul):8500" \ 74 | --engine-opt "cluster-advertise=eth1:2376" \ 75 | $@ 76 | } 77 | 78 | set -x 79 | 80 | create_and_launch_consul 81 | 82 | create_machine --swarm-master swarm-manager 83 | create_machine "node-01" 84 | create_machine "node-02" 85 | 86 | docker-machine env --swarm swarm-manager 87 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker Compose in Depth - Code Samples 2 | 3 | This repository contains code samples for Stone River E-Learning's [Docker Compose in Depth](http://stoneriverelearning.com/courses/docker-compose-in-depth) course. 4 | 5 | ## Pre-requisites 6 | 7 | - Docker Engine 1.11+ ([install](https://docs.docker.com/engine/installation/)) 8 | - Docker Compose ([install](https://docs.docker.com/compose/install/)) 9 | - Docker Machine ([install](https://docs.docker.com/machine/install-machine/)) 10 | 11 | ## Using this repository 12 | 13 | In any particular lecture with examples, you'll find the final version of the sample avaiable in `
/`. Follow along with commands in the lecture and you should see similar results 14 | 15 | ## License 16 | 17 | Copyright 2016 Stone River E-Learning 18 | --------------------------------------------------------------------------------