How to run Docker Swarm localy

Suppose there is an application which uses docker-compose to run and you want to try it in swarm mode.

First of all you need to initialize swarm nodes.

docker swarm init
SWARM_TOKEN=$(docker swarm join-token -q worker)
SWARM_MASTER=$(docker info | grep -w 'Node Address' | awk '{print $3}')

for i in $(seq 3); do
  docker run -d --privileged --name worker-${i} --hostname=worker-${i} -p ${i}2375:2375 docker:dind
  docker --host=localhost:${i}2375 swarm join --token ${SWARM_TOKEN} ${SWARM_MASTER}:2377
done

Run visualizer docker run -it -d -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock dockersamples/visualizer to see the nodes and the services on them.

Then deploy the application.

docker stack deploy --compose-file docker-compose.yml yourapp

If you have private docker repository add a --with-registry-auth option.

Now you can play with deploy options in the docker-compose file.

To remove the application from the swarm use docker stack rm yourapp.

comments powered by Disqus