How to Install Docker Compose on Ubuntu?
Docker Compose is a tool that makes it easier to deploy and manage multi-container applications in Docker environments. It enables programmers to define, set up, and run several linked containers as a single application stack. Docker Compose simplifies the orchestration of complicated applications by utilizing a YAML file to define the services, networks, and volumes needed. It makes tasks simpler to duplicate and share consistent development and production environments. Collaboration is facilitated, deployment problems are decreased, and application portability between systems is improved.
In this tutorial, you will learn how to install Docker Compose on Ubuntu 22.04 system.
Prerequisites
Before you proceed with the the guide on how to install Docker Compose on Ubuntu 22.04, ensure you meet the following requirements:
- Access to Ubuntu 22.04: You should either have root privileges or sudo privileges if you're a non-root user.
- Docker: Docker needs to be installed on your system. If it's not already, don't worry; we'll guide you through its installation as well.
- Firewall Considerations: If you use Docker to expose container ports, note that firewall management tools like ufw or Firewalld might block them. Ensure your firewall settings allow the necessary ports.
Step-by-Step Guide: How to Install Docker Compose on Ubuntu 22.04?
You can install Docker Compose on Ubuntu 22.04 using two different methods:
- Install Docker Compose from the Ubuntu apt repository.
- Install Docker Compose using the official GitHub repository.
Before starting the Docker Compose Ubuntu installation, ensure that you have installed Docker on your system. But if you don’t have Docker on your system, worry not. We will cover that as well. Let’s get started!
Install Docker Compose Ubuntu Using Apt Repository
The most convenient method is to install Docker Compose using the Ubuntu apt repository. To do this, you will need to add the Docker repository on the Ubuntu system.
Step 1: Install Required Docker Compose Dependencies
First, open the terminal window using the keyboard shortcut ‘Ctrl + Alt + t’. Use the ‘apt-get update’ command to update the Ubuntu system’s apt repositories:
$ sudo apt-get update
After installing updates, install the required dependencies for installing Docker and Docker Compose on your Ubuntu system:
$ sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common
The above command will install all necessary packages with names ‘lsb-release,’ ‘ca-certificates,’ ‘apt-transport-https,’ and ‘software-properties-common’ on Ubuntu 22.04 machine.
Step 2: Add Docker Repository to Ubuntu Source List
To add the Docker repository on the Ubuntu system, you need to import the Docker GPG key required for connecting to the Docker repository:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Once you import the Docker GPG key, add the Docker repository to your system sources list using the following command:
$ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Step 3: Install Docker Compose Ubuntu 22.04
After adding the Docker repository, update all Ubuntu apt repositories.
$ sudo apt update
Here, you will notice the Docker repository will display in your source repositories list.
Now, you can install Docker Compose on Ubuntu 22.04 using the following command:
$ sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
Verify the running service status of Docker using the below-given command:
$ sudo systemctl status docker
Now, check the Docker compose version:
$ docker compose version
Also, check the Docker version:
$ docker --version
Install Docker Compose Ununtu Using the Official Github Repository
Before installing Docker Compose, first, check the latest available version of Docker Compose from the releases page.
The latest ‘Docker compose’ release is v2.20.3. However, we will install the Docker Compose stable version v2.3.3 on our system. Here's a step-by-step guide to install Docker Compose on Ubuntu via the official GitHub repository.
Step 1: Set Up The Directory
First, create a directory using the ‘mkdir’ command:
$ mkdir -p ~/.docker/cli-plugins/
Step 2: Download Docker Compose
Now, download the Docker compose from the official Github repository using the ‘curl’ or ‘wget’ command:
$ curl -SL https://github.com/docker/compose/releases/download/v2.3.3/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
This command will procure the v2.3.3 release for your system. If you aim to install a different version, simply adjust the version number in the URL.
Step 3: Make The File Executable
Make the Docker-compose file executable using the ‘chmod’ command:
$ chmod +x ~/.docker/cli-plugins/docker-compose
Step 4: Verify Your Installation
Verify the above installation by displaying the Docker Compose version using the below command:
$ docker compose --version
With the above steps, you're not limited to personal computers – you can also seamlessly install Docker Compose on BlueVPS servers.
How to Use Docker Compose on Ubuntu 22.04?
After you install Docker Compose Ubuntu, you will need to set up a docker-compose.yml file to work with the Docker Compose Ubuntu system. Start by creating a directory in your system’s Home folder:
$ mkdir ~/compose-mongodemo
$ cd ~/compose-mongodemo
How to Set Up ‘docker-compose.yml’ File?
In this tutorial, we will set up a docker-compose.yml file for the ‘Mongo Express’ image. Create and edit the file:
$ nano demo-compose.yml`
[Here, you should provide the YAML content for Mongo Express]
You can also take the above configuration code from the docker hub. Save the changes and exit from this file in nano using ‘Ctrl+x.’
To deploy your services using the provided docker-compose.yml configuration, execute the following command:
$ docker compose up -d
This command will pull the necessary images, including Mongo Express, and start the containers.
Verify the running-up service status:
$ sudo docker compose up
You can also access the Monogo Express interface via:
http://localhost:8081 or http://your-server-ip:8081
The following output will display in your browser:
Let’s discuss some more Docker Compose commands that will help you run a multi-container environment.
If you want to check logs generated by your Mongo container, use the ‘logs’ command:
$ sudo docker compose logs
To pause and unpause the running container environment, use the below commands:
$ docker compose pause
$ docker compose unpause
You can also remove the containers, volumes, networks, and associated container-based environment using the ‘down’ command:
$ docker compose down
Conclusion
In this guide, you’ve seen how to install Docker Compose Ubuntu using two different methods: using an apt repository and via GitHub. We have also demonstrated how to set up a containerized environment using a Mongo Express database image. Finally, you have learned how to manage a containerized environment Docker Compose, using different commands.
If you want to learn more about Docker and Docker Compose, check the official Docker documentation.