Skip to main content

Setting Up Docker Swarm

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:00:00

Difficulty

Intermediate

After years of running containers on a single Docker host, you’ve decided to migrate over to using Docker Swarm. Using Swarm will allow your clients to scale the number of containers up, as demand increases, and then down as demand dies off. Before you can do this, you first need to set up a Swarm cluster consisting of a manager and a worker node. Once setup is complete, create an Nginx service to test the cluster.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Setting up Docker Swarm

The Scenario

After years of running containers on a single Docker host, we've decided to migrate over to using Docker Swarm. Using Swarm will allow our clients to scale the number of containers up, as demand increases, and then down as demand dies off.

Before we can do this, we first need to set up a swarm cluster consisting of a manager and a worker node. Once setup is complete, create an Nginx service to test the cluster.

Log In

Log in to the environment using the credentials provided on the lab page, either in a terminal session on your local machine or by clicking Instant Terminal. Note that there are two servers to log into, the manager and the worker.

Initialize the Swarm

On the manager node, initialize the swarm:

[cloud_user@manager]$ docker swarm init 
--advertise-addr [MANAGER_PRIVATE_IP]

After this runs, we'll get a docker swarm join command that we can then go run from the worker. Copy it.

Add the Worker to the Cluster

Now we can paste the docker swarm join command in a terminal on the worker:

[cloud_user@worker]$ docker swarm join --token [TOKEN] 
[MANAGER_PRIVATE_IP]:2377

On the manager, we can run docker node ls and see both of them, if all went well.

Create a Swarm Service

Let's create an Nginx service in the swarm. Run this command on the master node:

[cloud_user@master]$ docker service create -d 
--name nginx_service 
-p 8080:80 
--replicas 2 
nginx:latest

Now we can run (still on the master node) docker service ls and see nginx_service running, and that it's running on both nodes.

Conclusion

With this little Docker swarm set up, we're on our way to the migration. Congratulations!