Skip to content

Instantly share code, notes, and snippets.

@nishanttotla
Last active May 24, 2019 11:57
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nishanttotla/e4b623ce47e944086ebc to your computer and use it in GitHub Desktop.
Save nishanttotla/e4b623ce47e944086ebc to your computer and use it in GitHub Desktop.
A bash script to set up a simple Docker Swarm cluster using Docker Machine
################################## INSTRUCTIONS ##################################
# 1. Make sure docker-machine is installed on your machine #
# 2. Download the file #
# 3. Run using $ . swarm-with-docker-machine.sh so that DOCKER_HOST is exported #
##################################################################################
# Remove any existing machines
docker-machine rm -y manager agent1 agent2
# Create machines
docker-machine create -d virtualbox manager
docker-machine create -d virtualbox agent1
docker-machine create -d virtualbox agent2
# Get Swarm token
eval $(docker-machine env manager)
docker pull swarm
TOKEN="$(docker run --rm swarm create)"
# Run Swarm manager
docker run -d -p 3376:3376 -t -v /var/lib/boot2docker:/certs:ro swarm manage -H 0.0.0.0:3376 --tlsverify --tlscacert=/certs/ca.pem --tlscert=/certs/server.pem --tlskey=/certs/server-key.pem token://$TOKEN
# Join Swarm agents
eval $(docker-machine env agent1)
docker run -d swarm join --addr=$(docker-machine ip agent1):2376 token://$TOKEN
eval $(docker-machine env agent2)
docker run -d swarm join --addr=$(docker-machine ip agent2):2376 token://$TOKEN
# Manage the Swarm
export DOCKER_HOST=$(docker-machine ip manager):3376
# Now run Docker commands to check if Swarm was set up correctly
docker info
## you should see two healthy nodes after this
docker run hello-world
docker ps -a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment