How to Install Docker-Compose on Ubuntu 22.04?
Docker as a whole is a resource-friendly utility where you manage the applications and their dependencies in a container. To make it more lightweight, application resources are kept in multiple containers. Thus, to run an application, all the dependent containers must be synchronized, i.e., start, stop, and load, multiple containers at a time. Here Docker Compose comes in.
Docker Compose manages multiple containers at a time. The overall working and functionality of the Docker Compose depend on the “YAML” file, where a set of configurations are written for Docker Compose. The purpose of the “YAML” in docker-compose is similar to that of the docker file in Docker.
Keeping all these facts in view, this post will address the installation of the Docker-Compose on Ubuntu 22.04.
- Difference Between docker-compose and docker compose
- Tip: Install Docker to Get docker-compose
- How to Install Docker Compose on Ubuntu 22.04?
- Getting Started With Docker Compose
- How to Completely Remove Docker Compose From Ubuntu 22.04?
- Bottom Line
Difference Between docker-compose and docker compose
You might come across various notations of compose, i.e., docker compose and docker-compose. These are basically two different variants of compose. Let’s differentiate both:
- docker-compose: This tool is relatively old and its development/update is deprecated. It is Python-based and needs a dedicated installation (irrespective of the presence of Docker).
- docker compose: This is a Go-based utility. It comes by default with the docker CLI support. Initially, “docker compose” was available from the Docker Desktop. However, now it’s been included in the CLI of Ubuntu. Interestingly, the “docker-compose” development has been shifted to “docker compose”.
Tip: Install Docker to get “docker compose”
Docker Compose is the extended functionality of Docker. Before installing docker-compose, ensure that your system has docker installed and that its daemon (service managing docker) is running actively on the system. Get In Here to install docker on Ubuntu 22.04. With the installation of Docker, you will be able to access the docker compose too, by default.
How to Install Docker-Compose on Ubuntu 22.04?
The docker-compose’s official support is available through the GitHub repository. Moreover, you can get limited support of docker-compose from the libraries of Ubuntu 22.04, as well. Let’s get into these methods:
Install Docker-Compose Executable Directly | GitHub Repository
The Docker Compose executable is placed in the GitHub repository. The executable is fetched as per your system information (kernel and other hardware components). Let’s do it:
Step 1: Download the docker-compose From GitHub
Navigate to GitHub’s Docker-Compose Release Page and get the latest release available using the curl command. The command downloads the compose executable and places it at “/usr/local/bin/” with the “docker-compose” name. The executables placed in the “/usr/local/bin/” directory are recognized by the system globally.
sudo curl -L "https://github.com/docker/compose/releases/download/v2.23.3/docker-compose-linux-x86_64" -o /usr/local/bin/docker-compose
Note: Use the “arch” command to check the system architecture and then get the link to the relevant release. If you do not know your system architecture, replace the “Linux-x86_64” with “$(uname -s)-$(uname -m). The command in such case would be:
sudo curl -L "https://github.com/docker/compose/releases/download/v2.23.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Step 2: Grant Executable Permissions to the Downloaded File
Make the “docker-compose” executable for all the users via the command:
sudo chmod +x /usr/local/bin/docker-compose
Once all is done, restart the shell environment, and check the docker-compose version:
docker-compose -v
The docker-compose is installed.
The docker-compose works as an alias to the docker compose command for “compose V2”. As the docker-compose V1 update support is deprecated.
Install Docker-Compose Using the Source Code
The source code support is also available on the same GitHub repository. Each version has the source code available alongside other installation mediums. Let’s build the compose’s source code and install it:
Prerequisites: Install Build Essential
Building/installing a package from the source code requires a few prerequisites, i.e., make (in this specific case). To ensure this, install the build-essential toolkit which has all the utilities required to install a package from the source code:
sudo apt install build-essential
Step 1: Download and Extract the Source Code File
Go to the Release Page and get the source code (.tar.gz) file of the latest available version:
sudo wget https://github.com/docker/compose/archive/refs/tags/v2.23.3.tar.gz
Extract the downloaded .tar.gz file as:
tar -xzf v2.23.3.tar.gz
This will extract the content in the directory named “compose-2.23.3”.
Step 2: Install Compose
The “tar.gz” source file of the docker-compose is already compiled and its makefile is also available. So, you just have to use the “make install” command to read the instructions in “Make File” and install compose accordingly.
To do so, navigate to it using the “cd” command and install the compose as per the instructions written in the “Make File”:
make install
After the installation, create a symbolic link for docker-compose (at /usr/local/bin/docker-compose) to make it recognizable by the system:
sudo ln -s ~/compose-2.23.3/bin/build/docker-compose /usr/local/bin/docker-compose
Verify the installation of the docker-compose by running any docker-compose command:
docker-compose up -d
Or you can just run “docker-compose” to verify.
Install Docker Compose From Default Repositories of Ubuntu 22.04
Docker-Compose is available on the default repositories of Ubuntu 22.04. Here, you will get the docker-compose version 1.29.2. Let’s see how it can be installed:
Note: You must be aware that Docker has stopped the updates on version 1 as of July 2023. If you are installing it, you won’t get updates for this version.
Update the core libraries of your Ubuntu 22.04:
sudo apt update
Now, use the below command to install docker-compose:
sudo apt install docker-compose
Use the “docker-compose” command for verification:
docker-compose -v
Note: It can be seen that version 1.29.2 is recognized as “docker-compose”.
Getting Started With Docker-Compose
The functionality of the docker-compose depends on the configuration/set of rules stated in the “docker-compose.yml” file. The “docker-compose.yml” file is not available on Ubuntu 22.04 by default. Let’s understand how to create the “.yml” file and how to use it:
Step 1: Create docker-compose.yml File
Use the “touch” command to create the “docker-compose.yml” file (It can be created anywhere on the system):
touch docker-compose.yml
Step 2: Define the Set of Rules for Your Docker Compose
Open the “.yml” file in any editor. As an example, we will use “Apache” as our web service and “Redis” as the database service:
nano docker-compose.yml
Two services are defined, one is the web (httpd-apache) and the other is a database (Redis). The Apache image is named “httpd” and the Redis image is “redis” on the Docker Hub.
Note: Visit the Docker Hub to get Docker Images.
Step 3: Validate the .yml File
Use the “docker-compose config” to check/validate the syntax of the “.yml” file:
docker-compose config
If the syntax is valid/correct, you will get the details of the “.yml” file in the output as above. Otherwise, you will get the following error: (which states the syntax problem at a specific line):
Run the Docker Compose
The “-d” option will run the services in the background:
docker-compose up -d
The docker-compose will fetch the images from the Docker Hub (if not available locally).
Verify the Status
Once the docker-compose has run the containers, use the “docker ps” command to check/verify the containers’ status:
docker-compose ps
Pull Down the Containers
Both the images are fetched and the containers are running. If you want to pull down the containers, you can use the command:
$ docker-compose down
Here’s how you can manage the docker-compose.yml file.
Basic Docker-Compose Commands
Until now, we have learned how to install the docker-compose and a very basic understanding of a “.yml” file. Now, let’s go through some basic commands that are used to manage the docker-compose:
Start the Containers of the Service
docker-compose start
Stop the Containers of the Services
docker-compose stop
Pause the Running Containers
docker-compose pause
Resume/Unpause the Containers
docker-compose unpause
These were some of the basics of docker-compose.
How to Completely Remove Docker-Compose From Ubuntu 22.04?
The removal of the docker-compose depends on the installation method. As discussed, you can install the docker-compose by placing its executable in the “/usr/local/bin/docker-compose” or get the docker-compose cli plugin. Let’s discuss how these installations are removed.
Remove the Version Installed From Default Repositories
If you have installed the compose from Ubuntu-owned repositories, then use the following command to remove it and its dependencies as well:
sudo apt autoremove docker-compose
Remove the Executable of the Docker Compose
The following command removes the docker-compose executable from the system:
sudo rm /usr/local/bin/docker-compose
Verify the removal by using the “docker-compose” command:
Alternative | Rename/Backup the docker-compose Executable
You can rename the docker-compose executable with some other name, which will help you to get back to docker-compose whenever you need:
sudo mv /usr/local/bin/docker-compose /usr/local/bin/docker-compose.bkp
Note: If you want to get back the docker-compose support, just rename the file to “docker-compose” and then place it in “/usr/local/bin/docker-compose”. The command below does the same:
sudo mv <Path-of-The-Backup-File> /usr/local/bin/docker-compose
Note: Replace <Path-of-The-Backup-File> with the path where you have backed up your docker-compose plugin.
Bottom Line
Docker-Compose has been in the computing world for about 10 years now. It has revolutionized the overall containerization technology. You keep different services (application dependencies) of an application in different containers and these containers are managed by the docker-compose.
To get the docker-compose support on Ubuntu 22.04, either download its executable from GitHub or install the docker-compose-plugin.
Here, in this post, you have learned the installation, configuration/setting up, and removal of docker-compose on Ubuntu 22.04.