Let’s learn the commands to install Docker Compose on Ubuntu 22.04 LTS Jammy JellyFish to automate the process of setting up containers with the help of various Docker Images.
What is Docker Compose?
Docker-Compose is a CLI tool that is installed with Docker. Similar to images, there is a file in which the individual steps are listed. However, the syntax is slightly different from the individual calls directly in the Docker CLI. Particularly charming is the fact that it automatically ensures that the containers can see each other on the net.
Why do we use Docker Compose?
At the heart of Docker Compose is a YAML file called docker-compose.yml. In this file, we define the containers we want to start and their relationships with each other. We can share directories with the host system, share ports and links, and much more. This all saves us from typing all the long commands manually again and again. Simply put all your requirements from a container in the docker-compose YAML file and run it on any Docker installed server whenever you require to immediately build, up, and run the required similar Docker apps.
Steps to install Docker Compose on Ubuntu 22.04 LTS
The steps given here can also be used for other versions of Ubuntu or Debian such as 20.04/18.04, Debian 11 including Linux Mint, and more…
1. Add the Docker’s Public GPG key
Let’s add the GPG key issued by the developers of the Docker to verify the packages we receive to install the latest version of Docker Compose, Engine, or any other package related to it.
Install the commonly required packages:
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
After that add the key:
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
2. Add the repository of Docker
Next, add the Docker repository on Ubuntu Jammy using the below-given command that automatically detects the system’s version and points it to the repo URL and the GPG key we have configured in the previous step.
Note: Well, we don’t need to add any extra repository to your system because the packages to install Docker compose are already available to install using the default system repository. However, the version for this CLI tool available through it will be the older one. Therefore, to get the latest version we need to add the repository given below:
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
3. Update Ubuntu 22.04 system
Finally, run the system update command that will rebuild the APT package index cache, so that it could recognize the latest added repository of Docker and the packages available through it.
sudo apt update
4. Install Docker Compose on Ubuntu 22.04
To install and use Docker compose on our system it must have docker-engine whereas its plugin requires Docker CLI.
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
Note: The above command will also install the Docker Engine and other required tools on your system.
After running the above command you will have all the essential things we need to use Docker compose.
To check the version:
docker compose version
5. Create your first Docker Compose file
At the heart of Docker Compose is a YAML file called docker-compose.yml. In this file, we define the containers we want to start and their relationships with each other. We can share directories with the host system, share ports and links, and much more.
In the first level of the file, we write the names of our individual containers. These are important because we can use these names to reference other containers. In the sub-level of each container, the individual properties are then given. A simple example of this is here:
Let’s create a directory that holds our Compose file:
Create a YAML file:
Copy-paste the given lines:
version: '1.1' services: hello-world: image: hello-world:latest
Save the file- Ctrl+O, press the Enter key and then exit the editor using Ctrl+X.
Now, inside the test folder, use the Docker Compose command to execute the code given in the file, which simply says to pull the image hello-world and create a container with that.
sudo docker compose up
Output for the above command: