Docker swarm is a clustering system for docker. Today I configured a cluster of 2 machines and started 2 containers. Here is how I built a small swarm of 2 nodes.

Steps:

  1. Install latest docker
  2. Install latest swarm
  3. Create a swarm
  4. Create nodes
  5. Start docker manager
  6. Ship containers to the swarm

My system:

  • Debian Jessie
  • Docker 1.5.0
  • Docker swarm 0.2.0

I use 3 computers: PC1, PC2, PC3 PC1 runs the swarm manager and PC2 and PC3 are nodes.

IPs:

  • PC1: 172.16.43.232
  • PC2: 172.16.43.230
  • PC3: 172.16.43.229

1- Install latest docker

Download the docker binary from the web:

wget https://get.docker.com/builds/Linux/x86_64/docker-latest -O docker
chmod +x docker

Run these commands all computers, PC1, PC2 and PC3

2- Install latest swarm

First, install the go compiler, clone the swarm repository from github and compile.

apt-get install -y golang git;mkdir ~/gocode; export GOPATH=~/gocode;go get github.com/tools/godep

mkdir -p $GOPATH/src/github.com/docker/
cd $GOPATH/src/github.com/docker/
git clone https://github.com/docker/swarm
cd swarm
$GOPATH/bin/godep go install .

Run these commands all computers, PC1, PC2 and PC3

3- Create a swarm

The swarm command generate the swarm ID.

$GOPATH/bin/swarm create
a0c405a37b044f6503bb382c196b0346

Run on PC1

4- Create nodes

Start the docker daemon and join the swarm using the swarm id.

./docker -d -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
$GOPATH/bin/swarm join --addr=172.16.43.230:2375 token://a0c405a37b044f6503bb382c196b0346

Run on PC2, PC3

5- Start docker manager

To start docker manager, you need:

  • ip address of PC1: 172.16.43.232
  • choose a swarm port: 3456
  • swarm id: a0c405a37b044f6503bb382c196b0346
./docker -d
$GOPATH/bin/swarm manage -H tcp://172.16.43.232:3456 token://a0c405a37b044f6503bb382c196b0346

Run on PC1

6- Ship containers to the swarm

Run wordpress with mysql in the swarm, I chose port 50000 for wordpress:

./docker -H tcp://172.16.43.232:3456 run -d --name my -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql
./docker -H tcp://172.16.43.232:3456 run --name some-app --link my:mysql -p 50000:80 -d wordpress

Run on PC1