Docker Services

Notes
April 12, 2018

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

  1. a service for storing data in the database
  2. a service for video transcoding in the background after user uploads something and
  3. 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 –

  1. It’s not fussy about where it’s stored on the machine. We can keep this file wherever we want.
  2. Mind the indentation while writing it. It’s more of a YAML thing, but misplaced indentations will create problems.
  3. 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 –

  1. docker swarm init
    1. 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.”
  2. docker stack deploy -c /path/to/docker-compose.yml service_name
    1. 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.
    2. -c tag is simply “path to the compose file” followed by the complete path to YAML file.

Look at it! Isn’t it beautiful😍?

Try running 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.

It’s super easy to scale our application as required!

Now deleting a service is also a 2 step procedure –

  1. docker stack rm service_name – delete the service
  2. docker swarm leave –force – it will have to be done forcefully😔

That’s about it!

Docker docs to the Rescue😊