A single service is basically a piece of application. Services are simply “containers in production”.
For a video sharing website, we might have 3 different services running to make the application work
- a service for storing data in the database
- a service for video transcoding in the background after user uploads something and
- a service for the frontend.
To deploy an image in swarm mode, we create a service. We can spin up multiple containers of a single image(only one image) with the help of service. The question is – how do we create services? Enter…. docker-compose.yml (probably must’ve spotted this file somewhere before)
Now docker-compose.yml is a YAML file – it defines how the containers should behave. There are few things one should know about it –
- It’s not fussy about where it’s stored on the machine. We can keep this file wherever we want.
- Mind the indentation while writing it. It’s more of a YAML thing, but misplaced indentations will create problems.
- Be sure to push your desired image to a registry before making use of this file.
Here’s my YAML file. I’m using this image.
Now that we have defined docker-compose.yml, we can create our service – thereby creating a load-balanced app! There are 2 steps –
docker swarm init
- Idk why we run this yet, need to read an awful lot of things before I understand what this command does, but not using it will throw an error – “this node is not a swarm manager.”
docker stack deploy -c /path/to/docker-compose.yml service_name
- Running this command will also create a network called service_name_webnet and a service called service_web running 5 containers of the image, along with the other settings defined in YAML file.
- -c tag is simply “path to the compose file” followed by the complete path to YAML file.
curl http://localhost or open localhost inthe browser (it might take a few minutes to load). Run either of these commands in multiple tabs simultaneously, you might see different hostname up to 5 tabs, and then the hostnames will start repeating in a sequence. Load balancer choices container in a round robin fashion. Just try and you’ll understand what I mean🙄.
What if we want to update the number of containers running or change something else? Well, we simply make the changes in the docker-compose.yml file and run this –
docker stack deploy -c /path/to/docker-compose.yml service_name. Voila! Our service has been updated!
Now here’s an important thing that should be taken into account – these updates do not tear the stack down or kill any containers, if the number of replicas is increased, only new containers will be created, this will not affect the existing containers. And if the number of replicas is decreased, then the extra containers will be destroyed starting from the last created container.
Now deleting a service is also a 2 step procedure –
docker stack rm service_name– delete the service
docker swarm leave –force– it will have to be done forcefully😔
That’s about it!
Docker docs to the Rescue😊