My Docker cheatsheet

Docker

Installing docker

You need to install docker engine v1.4 as minimum, some distros doesn't come with lates version

Docker SWARM

Docker swarm is usde to manage docker cluster through rest api. To install swarm first you need to install golang (min v 1.4.1) # Download latest golang version from: Golang homepage

host$ wget https://storage.googleapis.com/golang/go1.5.1.linux-amd64.tar.gz

# here you will install go globaly

host$ sudo tar -C /usr/local -xzf go1.5.1.linux-amd64.tar.gz

# or you can install it in user folder

host$ cd ~ && mkdir go
host$ sudo tar -C ~/go -xzf go1.5.1.linux-amd64.tar.gz
Now need to setup environment variables:
#create new file that will export GOROOT variable

host$ vi /etc/profile.d/go.sh
GOROOT=/usr/local/go
export GOROOT

# add go path

host$ export PATH=host$ PATH:/usr/local/go/bin

# or local if you chose install go localy

host$ export PATH=host$ PATH:host$ GOROOT/bin
Once that's done you can download and install swarm
host$ GOHOME=~/go
host$ export GOHOME
host$ go get -u github.com/docker/swarm

#this will install swarm in your home dir ~/go/bin

host$ PATH=host$ PATH:~go/bin
host$ swarm -v
Now you can start building your docker cluster/swarm :) # Start docker engines with -H tcp://192.168.1.100:2375 -H unix:///var/run/docker.sock

host$ cat /tmp/my_cluster
192.168.1.100:2375
192.168.1.101:2375
192.168.1.102:2375

host$ swarm manage -H 0.0.0.0:4243 file:///tmp/my_cluster & - swarm with all nodes from file

# or you can use etcd or add nodes via cli:

host$ swarm manage -H 0.0.0.0:4243 nodes://192.168.1.101:2375,192.168.1.102:2375,192.168.1.103:2375 &
Now we can start running docker containers via swarm & apply constraints:
host$ DOCKER_HOST="192.168.1.100:4243"
host$ export DOCKER_HOST

# constraints are two type, standard (values returned by docker info) and custom

host$ docker run -d --name c1 -e constraint:operatingsystem==Deb* nginx
host$ docker run -d --name c1 -e constraint:operatingsystem==Fedor* nginx

# custom constraints (labels, only from docker v1.4)
# add label to docker engine via docker.conf file (DOCKER_OPTS variable) eg --label zone=dmz --label site=london

host$ vi /etc/default/docker
DOCKER_OPST="-H 192.168.1.100:2375 -H unix:///var/run/docker.sock --label zone=dmz --label site=london"
host$ service docker restart
host$ docker info - (run on local node and not swarm, and at the botom you will see labels assigned to host)

# Now schedule container on specific zone/location (this will work via sworm as well)

host$ docker run -d --name london1 -e constraint:site==london -e constraint:zone==dmz

# or you can specify NOT to run on site

host$ docker run -d --name notlondon1 -e constraint:site!=london -constraing:zone!=dmz"

Docker compose

Docker-compose let's you build service chains from docker images:
# Check home page for latest version and chang URL bellow VERSION_NUM with lates bersion number

host$ sudo curl -L https://github.com/docker/compose/releases/download/VERSION_NUM/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
host$ docker-compose --version

Shipyard

Shipyard is Web UI for docker cluster @ Shipyard Project
Shipyard comes with Etcd running as container and it listens on ports 4001 and 7001. To join new docker engine to swarm cluster
issues command bellow:
curl -L http://127.0.0.1:4001/v2/keys/docker/swarm/nodes/192.168.1.57:2375 -XPUT -d value="192.168.1.57:2375" This will create new key 192.168.1.57:2375 with value 192.168.1.57:2375
If you want to delete key, issue the following command:
curl -L http://127.0.0.1:4001/v2/keys/docker/swarm/nodes/192.168.1.57:2375 -XDELETE This command will delete whole key.
# Open in browser url bellow to see key tree:
http://127.0.0.1:4001/v2/keys/
# URL bellow will show all nodes in swarm cluster:
http://127.0.0.1:4001/v2/keys/docker/swarm/nodes/
Here's API guide to managing etcd via curl Etcd

Using docker network * only from docker version 1.9.0

First you need update docker config file and add following:
DOCKER_OPTS="--cluster-store=etcd://10.1.1.10:42000/netroot --cluster-advertise=10.1.1.100:0 -s overlay"
# - or following if you are using consul as your key value store
DOCKER_OPTS="--cluster-store=consul://10.1.1.10:42000/netroot --cluster-advertise=10.1.1.100:0 -s overlay"
# this assumes that your etcd is running on 10.1.1.10 and listening on port 42000
# - netroot is new namespace if you want have separate cluster. overlay here is storage driver

host$ docker network ls - show what networks are configured & available, none, host & bridge are enabled by default for backward compatibility
host$ docker network create singlehost - create new network named singlehost
host$ docker run --net=singlehost --name=c1 -itd busybox sh - create container connected to singlehost network
host$ docker run --net=singlehost --name=c2 -itd busybox sh - created second container in the same network
host$ docker run --net=bridge --name=c3 -itd busybox sh - created third container in default network
host$ docker network inspect singlehost - show properties of network, what containers attached, ip subnet etc

# Creating overlay(multihost) network
host1$ docker network create -d operlay multihost1
host1$ docker run --net=multihost1 --name=m1 -it busybox sh - create container connected to multihost1 network
host2$ docker run --net=multihost1 --name=m2 -it busybox sh - created second container in the same network

# Create overlay network with specific IP subnet allocated
host1$ docker network create -d operlay --subnet=20.1.1.0/30 --subnet=30.1.1.0/30 multihost2
# Now we will have only 2 IP's per subnet, and only single IP fvailable or container as other will be used by GW. So if we would like to crete two containers they will be in two different subnets. But both containers will be abletto ping each other even if they are in different IP subnets

# Now we can dynamicaly attach container to existing network
host2$ docker network connect multihost2 m1 - this will attach container named m1 to network multihost2 and create new interface
host2$ docker network disconnect multihost1 m1 - deatach m1 from multihost1 network
More can be found on this blog post and this page
Some example network setup scripts can be found here

Optimizing Docker images

Here's good article on how to optimize docker images