0
0
Dockerdevops~5 mins

Deploying services in Swarm in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to run your app across multiple computers easily, Docker Swarm helps by managing many containers as one group. Deploying services in Swarm means starting your app so it runs reliably and can handle more users by spreading work across machines.
When you want your app to keep running even if one computer fails
When you need to run multiple copies of your app to handle more users
When you want to update your app without stopping it completely
When you want to manage many containers from one place
When you want to balance work evenly across several servers
Commands
This command starts Docker Swarm mode on your current machine, making it the manager that controls the cluster.
Terminal
docker swarm init
Expected OutputExpected
Swarm initialized: current node (abcdef123456) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-0abcdef1234567890abcdef1234567890abcdef1234567890abcdef123456 192.168.1.100:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
This command deploys a service named 'my-web' running the nginx web server with 3 copies (replicas). It also maps port 8080 on your machine to port 80 in the containers so you can access the web server.
Terminal
docker service create --name my-web --replicas 3 -p 8080:80 nginx:1.23
Expected OutputExpected
q1w2e3r4t5y6z7x8y9a0b1c2d3e4f5g6h7i8j9k0l1m2n3o4p5q6r7s8t9u0v1w2
--name - Sets the name of the service for easy identification
--replicas - Specifies how many copies of the service to run
-p - Maps a port on the host to a port in the container
This command lists all running services in the swarm so you can check if your service is active and how many replicas are running.
Terminal
docker service ls
Expected OutputExpected
ID NAME MODE REPLICAS IMAGE PORTS q1w2e3r4t5y6 my-web replicated 3/3 nginx:1.23 *:8080->80/tcp
This command shows detailed information about the tasks (containers) running for the 'my-web' service, including their current state and which node they run on.
Terminal
docker service ps my-web
Expected OutputExpected
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS x1y2z3a4b5c6 my-web.1 nginx:1.23 node-1 Running Running 2 minutes ago x7y8z9a0b1c2 my-web.2 nginx:1.23 node-2 Running Running 2 minutes ago x3y4z5a6b7c8 my-web.3 nginx:1.23 node-3 Running Running 2 minutes ago
Key Concept

If you remember nothing else from this pattern, remember: Docker Swarm lets you run and manage many copies of your app easily across multiple machines with simple commands.

Common Mistakes
Trying to deploy a service before running 'docker swarm init'
The swarm mode is not active, so Docker cannot manage services across nodes.
Always run 'docker swarm init' first to enable swarm mode on your machine.
Not specifying the number of replicas when creating a service
Docker will create only one replica by default, which may not provide the desired availability or load distribution.
Use the '--replicas' flag to set how many copies of the service you want.
Forgetting to map ports with '-p' when deploying a web service
Without port mapping, you cannot access the service from outside the swarm nodes.
Use '-p host_port:container_port' to expose the service ports properly.
Summary
Initialize swarm mode with 'docker swarm init' to start managing services.
Deploy services using 'docker service create' with options for replicas and port mapping.
Check running services with 'docker service ls' and inspect tasks with 'docker service ps'.