Back to blog
Jul 02, 2024
5 min read

Docker Compose and Stacks, orchestrating your containers

Docker Compose can define and run multi-container Docker applications; Docker Stacks take a group of containers orchestrated and scaled together.

Introduce Docker Compose and Stacks

Hi there, Today, we’re diving into the world of Docker orchestration tools. If you’ve been working with Docker for a while, you’ve probably heard of Docker Compose and Docker Stacks.

What is Docker Compose? Docker Compose is like the conductor of a small orchestra. It’s a tool that allows you to define and run multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services, networks, and volumes. Then, with a single command, you create and start all the services from your configuration.

We often use Docker Compose for:

  • Local development environments
  • Automated testing pipelines
  • Single host deployments

What is Docker Stacks? Docker Stacks are part of Docker Swarm mode and they take a group of containers orchestrated and scaled together. Stacks are defined using Docker Compose files, but they’re deployed to a swarm of Docker engines running in swarm mode.

We often use Docker Stacks for:

  • Production deployments
  • Scaling across multiple hosts
  • When you need advanced features like rolling updates and load balancing.

Let’s start to config it

Prerequisites

To prepare a Virtual Machine and keept it is running Debian system, then config a static IP for this VM. Maybe this VM runs on VitrualBox App or VMWare App. It doesn’t matter.

In my side, My host computer is using MX Linux, I installed a VirtualBox App, then running many VM on it. I have already prepared one Debian VM, and set username as eric in Debian VM system, set the static IP as 192.168.68.119 all the code, you can find at here: https://github.com/ericssonxiao/Docker-and-Kubernetes-Sample/tree/main/01-Docker-Compose-Stacks-Sample

I use scp command to transfer the files from host computer to VM. My work folder on Debian VM is ~/docker/. It isn’t a necessary step, you can directly use the file in Debian VM’s Download folder(~/Downloads/)

I have already prapared a Docker Image friendlyhello on https://hub.docker.com/ website. In the following step, I will use this docker image. img00 I will write another post to talk about on how to build your customer docker Image and push to https://hub.docker.com/ website.

Steps

  1. Prepare a docker-compose.yml file and check its configuration is ok. img01

Then copy docker-compose.yml to my Debian VM work folder, which located in dir ~/docker/

sudo scp ./docker-compose.yml [email protected]:/home/eric/Downloads

Go to Debian VM, use mv command to move file in work folder.

mv ~/Downloads/docker-compose.yml ~/docker/

img02

so after this step, in my Debian VM, under ~/docker, there are four files: Dockerfile, requirement.txt, app.py, docker-compose.yml img03

  1. run this commandline, which is used to initialize a new Docker swarm and create the first manager node.
# if you login as root user, you don't need to add "sudo" before the docker commands.
sudo docker swarm init

img04

  1. Then I use this commandline to deploy a stack of services defined in a Docker Compose file to a Docker swarm. For this part -c docker-compose.yml, I specifies the path to the Compose file that defines the services, networks, and volumes for the stack. AHAH, “friendlyhello”, This is the image name given to the stack being deployed.
# if you login as root user, you don't need to add "sudo" before the docker commands
sudo docker stack deploy -c docker-compose.yml friendlyhello

img05

  1. run these commands to see the cluster service in docker swarm.
# if you login as root user, you don't need to add "sudo" before the docker commands
sudo docker service ls
sudo docker service ps friendlyhello_web

img06

  1. this command is used to list Docker containers. It lists running containers by default.
# if you login as root user, you don't need to add "sudo" before the docker commands
sudo docker container ls

img07

  1. The command is used to list Docker containers, showing only their container IDs.
# if you login as root user, you don't need to add "sudo" before the docker commands
sudo docker container ls -q

img08

  1. This command force curl use IPv4 to make an HTTP request to a local server.
curl -4 http://localhost:4000

img09 Pay more attention at the Hostname’s value, like this one “Hostname:ed437168de36”, this is container IDs. If you use curl request many times, you will find in each HTTP request, the container ID is different. All of the container IDs will list by the command sudo docker container ls -q

  1. This command is used to remove a Docker stack named “friendlyhello” from a Docker swarm.
# if you login as root user, you don't need to add "sudo" before the docker commands
sudo docker stack rm friendlyhello

img10

  1. This command is used to forcefully remove a node from a Docker swarm.
# if you login as root user, you don't need to add "sudo" before the docker commands
sudo docker swarm leave --force

img11