In this article, we want to teach you Install and Use Docker Compose on Debian 11.
Docker is an open-source platform that is set up based on a Linux operation system. It is a tool that can ease the creation, implementation, and performance processes with the containers.
Docker Compose is a tool you can use to centrally manage the deployments of many different Docker containers. It’s an important tool for any application that needs multiple microservices, as it allows each service to easily be in a separately managed container.
Install and Use Docker Compose on Debian 11
To complete this article, you need to log in to your server as a non-root user with sudo privileges. to do this, you can check our article about the Initial server setup with Debian 11.
Also, you need to install Docker on your server. For this, you can visit our article about How to install and use Docker on Debian 11.
When you are done with these requirements, you can start to install Docker Compose on Debian 11.
Install Docker Compose on Debian 11
You can install Docker Compose from the official Debian repositories. but in this article, you will install Docker Compose from Docker’s GitHub repository.
Check the latest version from the GitHub Docker Compose releases page and run the following command with the current release:
sudo curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
Now you need to set the permissions with the following command:
sudo chmod +x /usr/local/bin/docker-compose
Verify the installation by checking the Docker Compose version:
Output docker-compose version 1.29.2, build 5becea4c
At this point, when you have installed Docker Compose on Debian 11, let’s see how to use Docker Compose.
Use Docker Compose to Run a Container
The Docker Hub includes a Hello World image for presentation and testing. It shows the minimal configuration required to run a container using Docker Compose, a YAML file that calls a single image.
Now create the minimal configuration to run the hello-world container.
First, you need to create a directory for the YAML file and switch to it with the following command:
mkdir hello-world cd hello-world
Then, create the YAML file with your favorite text editor, here we use vi:
Add the following content to your file:
my-test: image: hello-world
The first line is used as a part of the container name. and the second line specifies which image to use to create the container.
When you are done, save and close the file.
Look at the images on your system with the following command:
When there are no local images, in your output you will see:
Output REPOSITORY TAG IMAGE ID CREATED SIZE
Now run the Docker Compose up command on your Debian 11 that you are still in the ~/hello-world directory:
When you run this command, if the hello-world image doesn’t exist, Docker Compose will pull it from the docker hub first, then it will create the container:
Output Pulling my-test (hello-world:)... latest: Pulling from library/hello-world 2db29710123e: Pull complete Digest: sha256:37a0b92b08d4919615c3ee023f7ddb068d12b8387475d64c622ac30f45c29c51 Status: Downloaded newer image for hello-world:latest Creating hello-world_my-test_1 ... done Attaching to hello-world_my-test_1 my-test_1 | my-test_1 | Hello from Docker! my-test_1 | This message shows that your installation appears to be working correctly. my-test_1 | my-test_1 | To generate this message, Docker took the following steps: my-test_1 | 1. The Docker client contacted the Docker daemon. my-test_1 | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. my-test_1 | (amd64) my-test_1 | 3. The Docker daemon created a new container from that image which runs the my-test_1 | executable that produces the output you are currently reading. my-test_1 | 4. The Docker daemon streamed that output to the Docker client, which sent it my-test_1 | to your terminal.
Note: Docker containers only run when the command is active.
Because of that when you check the active containers, you won’t see the hello-world container:
Output CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Run the following command to see all the containers both active and inactive, you will see the hello-world container:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 59d1b50a0b40 hello-world "/hello" 25 minutes ago Exited (0) 25 minutes ago hello-world_my-test_1
At this point, you learn to install and use Docker compose. Now you can remove the container when you are done with it.
You can remove the local images to avoid using unnecessary disk space. To do this, you need to remove all containers that reference the image.
Run the following command to remove the container:
docker rm 59d1b50a0b40
Note: remember to replace the container ID with your own.
Now you can remove the image with the following command:
docker rmi hello-world
At this point, you learn to install Docker Compose and you learn to use it to run a container and remove it.
Hope you enjoy this article about Install and Use Docker Compose on Debian 11.