Getting Started with Docker Swarm
A quick definition:
swarmis a simple tool which controls a cluster of Docker hosts and exposes it as a single “virtual” host.
For me, Docker is a bit of a shift in thinking compared to a regular virtualbox. Docker Swarm takes the container applies concept to multi-node networked instances.
For this article, I will be working with Docker Swarm within a single virtualbox, just to keep things simple.
- Virtualbox installed
- vagrant installed
- Download this Vagrantfile
With everything in the requirements section completed, in the folder containing the Vagrantfile, run this command in the terminal:
The rest of the article will work within the manager virtualbox.
Showing Docker Swarm Services
In Docker Swarm, every running container(s) is a service. Instead of Docker managing them directly, Docker Swarm uses the Docker API to create containers from images.
To see Docker Swarm services, run:
docker swarm ls
|ID||the container ID|
|NAME||the unique name for the service|
|MODE||which mode the service is in: replicated or global|
|REPLICAS||the number of replicas the service is running in|
|IMAGE||the Docker image used for the service|
|PORTS||which ports connect the host to the service|
Contrast this with
So, different information for different use cases.
Start nginx Service
Now that we know how to see what Docker Swarm services are running, let’s start one up!
I’m going to use a simple & well-known image: nginx. I would like to focus on the other parts of the Docker Swarm commands before thinking about how to create an image for the service.
To create a Docker Swarm service with nginx, run command:
service create <image name>
Oooh, that loks a bit different. There’s a nice progress bar and a
message stating the service status.
nginx Service Status
Let’s check out what the status of the service using
Oooh, that’s cool. The service:
replicated and it’s running on 1/1 replicas.
Let’s checkout what
docker ps shows:
Oooh, that’s interesting. The names match: objective_hermann, but
docker ps command lists and additional ending:
.1.6agqbzmx8qiazhwwncdegknwa, so the container and service are not
1:1. The container ID is different than the service ID.
Docker Service ps
Docker Swarm has its own
ps command, which gives different
|NAME||Similar to the
|IMAGE||This lists the image used for the service|
|NODE||Nodes this service running on|
|DESIRED STATE||The desired state of this service: running or shutdown|
|CURRENT STATE||Current state of the service|
|ERROR||Error meessage from creating the service|
|PORTS||Ports connecting the host to the service|
Stopping Docker Service
Now that we have a Docker Service running in Swarm, how can we stop it?
By using the command:
docker service rm <service name>, so to remove
the objective_hermann service:
That was easy!
nginx that is not accessible through any ports is not fun. Let’s
create the service again with ports mapped using the option:
--publish <host port number>:<service port number>
Let’s check by using a quick
curl to see what nginx returns:
More Formal Format
Another, more formal and explicit version of the same command:
Updating Service Port
If the service is already running, say you forgot to specify ports
when creating the service, use the
docker service update command
publish-add to update the service:
It’s basically the same as if you manually stopped the service and restarted with the right fields, but in update case, the service ID does not change.
This time, I went over how in a single node Docker Swarm to:
- list services
- create a new Docker Swarm from the nginx image
- make the service accessible by specifying or updating its ports
- delete services that are in a single node Docker Swarm.
Docker Swarm builds on the concepts of Docker, and although commands
are similar, like
ps is in both docker and docker service, it’s
intended usage is different.
I chose to focus on using a known Docker image, nginx, to learn about Docker Swarm.
Next time, I will focus on how to create a service in Docker Swarm from your Docker image.