Skip to main content

Setting Up a Docker Swarm

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:30:00

Difficulty

Intermediate

For the last six months, the Acme Anvil Corporation has been migrating some of their bare metal infrastructure to Docker containers. A schism has developed between the members of your team on whether to use Docker Swarm or Kubernetes. Your manager has decided to settle the dispute by creating two competing demos for Docker Swarm and Kubernetes. You have been tasked with helping to create the Docker Swarm demo. Create a swarm with three nodes and then create a service using the weather-app image.

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 a Docker Swarm

Introduction

For the last six months, the Acme Anvil Corporation has been migrating some of their bare metal infrastructure to Docker containers. A schism has developed between the members of your team on whether to use Docker Swarm or Kubernetes. Your manager has decided to settle the dispute by creating two competing demos for Docker Swarm and Kubernetes. You have been tasked with helping to create the Docker Swarm demo. Create a swarm with three nodes and then create a service using the weather-app image.

Instructions

In this lab, you are tasked with setting up a Docker swarm.

  1. Log in to Swarm Server 1. This will be your swarm master.
  2. Initialize a new Docker swarm.
  3. Add two worker nodes to the swarm.
  4. Create a new swarm service using the weather-app image.
  5. Name the service weather-app.
  6. Publish port 80 and map it to port 3000 on the container.
  7. Create 3 replicas.

Solution

Begin by logging in to Swarm Server 1 using the credentials provided on the hands-on lab page:

ssh cloud_user@PUBLIC_IP_ADDRESS

Become the root user:

sudo su -

Repeat these steps for Swarm Server 2 and Swarm Server 3 in new tabs.

Initialize the Docker swarm

On Swarm Server 1:

  1. Initialize the Docker swarm.

    docker swarm init

    Copy the docker swarm join command that is displayed for the next step.

Add additional nodes to the swarm

Add your worker nodes to the swarm.

  1. Run the following command on Swarm Server 2 and Swarm Server 3:

    docker swarm join --token TOKEN IP_ADDRESS:2377

    > Note: This is the command that was copied from the previous step.

Create a swarm service

  1. From the master node (Swarm Server 1), create a service to test your swarm configuration.

    docker service create --name weather-app --publish published=80,target=3000 --replicas=3 weather-app
  2. Verify that this completed successfully:

    docker service ls

    We can also paste one of the public IP addresses of our worker nodes in a browser to view the weather app.

Conclusion

Congratulations, you've completed this hands-on lab!