How to Install and Use Docker Compose on Debian 11 Tutorial

How to install and use Docker Compose on Debian 11  Tutorial. An application can be made up of many containers that run various services. Manually building, running and connecting the containers from distinct Docker files can be tedious and time consuming. Not to mention it will impair the overall productivity.

That’s why Docker introduced Docker Compose, which is a tool for defining and running multi container Docker applications. It can be used to create, manage  and compose multi container Docker applications. Compose allows you to define containers as well as their relationships in a YAML (yet another mark up language) file.

In this article, you will learn about Docker Compose, its features, and its benefits. You will also learn how to install and use Docker Compose on a Debian 11 system.

What is Docker Compose

Well  Docker Compose is a tool for defining and running multi-container Docker applications. It’s used by developers to build and orchestrate distributed applications.

Designed to be easy to use and provides a declarative language for specifying and configuring complex, multi-container Docker applications with minimal effort

Also Compose helps you manage application configuration, so you don’t need to manually copy over the files between containers.

Additionally it makes it easier for developers and operations staff to work together on an application’s build and deployment process. Docker Compose also allows you to spin up new applications from the same code base as fast as possible.

Worth adding that it is a more powerful alternative to Docker Images, which are generally one offs that contain everything you need for an application. Moreover Compose also allows you to manage multiple versions of the same application or even multiple versions of the same image.

What else is particularly beneficial is that it is great tool for orchestrating more complicated applications with various services. Also it saves a substantial amount of time while developing and maintaining apps.

Features of Docker Compose

Some of prominent features of Compose are as follows:

  • With Docker Compose you can host multiple isolated environments on a single host.
  • Ability to customize containers for different environments or users by adding environment variables in the docker compose file.
  • Moreover Docker Compose preserves data that the services use.
  • You can reuse the existing containers.
  • This Docker Compose   puts all App Layer configurations in one location.
  • Can start and stop your application services in a single shot.
  • Logs in Docker Compose  can be utilized to monitor your app services.
  • Manages the app’s lifecycle events in a single action.
  • What is more with Compose can scale apps or specialized services together.
  • Can also get information about all the containers that are operating across the whole app.
  • Can run commands for executing containers in the app.
  • You can pull/push service images together.

Advantages of Docker Compose

Here are some of the primary advantages of utilizing Docker Compose:

  • Quick and easy configuration: You can simply set up or alter application services using YAML scripts and environment variables.
  •  Secure internal communication: Compose establishes a network for all services to share. Because the services cannot be accessed from outside the app, this adds an extra degree of protection.
  • Support for CI/CD and portability: Developers may simply access and share the whole setup because docker compose file defines all services. They may launch the environment in minutes by downloading the YAML file and source code. This helps to build up and enable an efficient CI/CD process.
  • Efficient resource utilization: Docker Compose enables the hosting of several separate environments on a single server. Running everything on a single piece of hardware allows you to save a significant amount of resources. 

Follow this post to learn how to install and use Docker Compose on a Debian 11 server.

How to Install and Use Docker Compose on Debian 11

Prerequisites

You will need:

  • Debian 11 system.
  • A user with sudo privileges.
  • Docker installation on your system. If you do not have it installed, follow our guide on how to set up Docker on Debian 11.

Install Docker Compose

Update Your System

Before you begin, update your system to check that all packages are up to date.

				
					sudo apt update
				
			

Get the Docker Compose repository

Use curl to download the Docker Compose repo to the /usr/local/bin/docker-compose directory. You may also get Docker Compose from its official Github repository.

				
					sudo curl -L "https://github.com/docker/compose/releases/download/v2.0.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
				
			

Output:

				
					  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

				
			

Modify File Permissions

After downloading Docker Compose, run the following command to give permission and make the downloaded file executable.

				
					sudo chmod +x /usr/local/bin/docker-compose
				
			

Check Docker Version

Next, check the docker-compose version to ensure that the installation was successful.

				
					docker-compose --version
				
			

Output:

				
					Docker Compose version v2.0.1
				
			

This result shows that Docker Compose was successfully installed.

How to Use Docker Compose

To use docker compose, you must first create a docker-compose.yml file. The containers that comprise your application’s stack are detailed here.

You can specify which networks, environment variables, port bindings, and volumes of your containers.

Containers provided in the same docker-compose.yml file immediately join the same stack. They are in a Docker network and communicates with one another by using their docker-compose.yml service names as hostnames.

Configure the docker-compose.yml file

To demonstrate how to create a docker-compose.yml file and utilize Docker Compose, you will create an example web server setup using the official Nginx image from Docker Hub.

Firstly, use the following commands to create and go to the compose-demo directory.

				
					mkdir ~/compose-demo
cd ~/compose-demo
				
			

Create an application folder inside this directory to serve as the document root for your Nginx environment:

				
					mkdir app
				
			

Then, under the app folder, run the following command to generate an index file.

				
					nano app/index.html
				
			

In the index file, paste the following code.

				
					<p>Docker Compose Demo</p>
<h2>This is a Docker Compose Demo.</h2>
<p>This content is being served by an Nginx container.</p>
				
			

Exit the nano editor after saving the file.

Finally, run the following command to generate the docker-compose.yml file.

				
					nano docker-compose.yml
				
			

Copy and paste the following code into the docker-compose.yml file.

				
					version: '3.7'
services:
  web:
    image: nginx:alpine
    ports:
      - "8000:80"
    volumes:
      - ./app:/usr/share/nginx/html

				
			

Finally, run the following command to generate the docker-compose.yml file.

Also with containers which are defined on the services node using the docker-compose.yml file. Every node defines its image. Here you can define the environment or port. A volume denotes a shared volume shared by the host and the container.

The web container will utilize the nginx:alpine image, run on port 8000:80, and use the shared volume./app:/usr/share/nginx/html, as shown in the preceding file.

Save and close the file.

Next in how to install and use Docker Compose on Debian 1 Docker Compose Demo is to deploy application with docker. 

Deploy the application with Docker

To start the application, use the following command.

				
					sudo docker-compose up -d
				
			

Output:

				
					[+] Running 7/7
 ⠿ web Pulled                                                                       11.6s
   ⠿ 530afca65e2e Pull complete                                                      4.0s
   ⠿ 323a7915bc04 Pull complete                                                      5.8s
   ⠿ b5b558620e40 Pull complete                                                      6.0s
   ⠿ b37be0d2bf3c Pull complete                                                      6.3s
   ⠿ ba036c7f95ec Pull complete                                                      6.5s
   ⠿ a46fd6a16a7c Pull complete                                                      6.6s
[+] Running 2/2
 ⠿ Network compose-demo_default  Created                                             0.3s
 ⠿ Container compose-demo-web-1  Started                                             3.5s
				
			

This will start a web container as well as download the Nginx library.

Use the following command to confirm that the deployment was successful.

				
					sudo docker-compose ps
				
			

Output:

				
					NAME                 COMMAND                  SERVICE             STATUS              PORTS

compose-demo-web-1   "/docker-entrypoint.…"   web                 running             0.0.0.0:8000-&gt;80/tcp, :::8000-&gt;80/tcp

				
			

You can access your sample application through localhost:8000.

Run the following commands to inspect logs generated by the Nginx container.

				
					sudo docker-compose logs
				
			

When you are done, you can stop the Docker Compose with the following commands.

				
					sudo docker-compose stop
sudo docker-compose down

				
			

That’s it for the Docker Compose installation and utilization on Debian 11. 

How to Install and Use Docker Compose on Debian 11 Conclusion

Now that you have learned how to install and use Docker Compose on Debian 11, you can speed up your development and use it to deploy your applications.

To summarize Docker Compose is a tool that enables you to define your entire application in a single file, which can then execute in multiple containers. It helps in creating, managing, and composing multi-container Docker applications.

Explore our Docker section to learn more.

Avatar for Sobia Arshad
Sobia Arshad

Information Security professional with 4+ years of experience. I am interested in learning about new technologies and loves working with all kinds of infrastructures.

5 3 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x