How To Install and Use Docker Compose on Ubuntu 22.04

How To Install and Use Docker Compose on Ubuntu 22.04. First of all, Docker is a hugely popular and widely used open source platform that allows developers and operation teams to build, test and deploy containers with ease. Since, applications are packaged inside containers that results in lightweight, standalone and standardized software units that include everything a container needs to run including the source code , libraries, dependencies, runtime, system tools and settings.

However, deploying applications with multiple containers is quite a challenge. For example, consider a full featured web application that requires an Apache web server and MySQL database. Building, deploying and connecting the containers from separate Docker files is time consuming. And this is where Docker Compose comes to the rescue.

Shall we start with how to Install and Use Docker Compose on Ubuntu 22.04?

What is Docker Compose?

Evidently, Docker Compose is a free and open source platform for running multi container Docker applications. With Docker compose, you run multiple containers by defining various application services inside a single YAML file. Using a single command you create, start restart and stop all your containers from the command line.

Benefits of Docker Compose

Simple configuration of services

Portability and CI/CD support

Optimal resource utlization

Secure internal communication

Please follow this article to learn how to install Docker Compose on Ubuntu 22.04.

Install and Use Docker Compose on Ubuntu 22.04

Step 1: Install Docker on Ubuntu 22.04

As a prerequisite, you need to install Docker before installing Docker compose. Firstly, log into your Ubuntu server and update the local package index as follows.

				
					$ sudo apt update
				
			

Some additional dependencies are needed for the installation of Docker to proceed smoothly. Proceed and install them as follows.

				
					$ sudo apt install software-properties-common apt-transport-https ca-certificates lsb-release -y
				
			

We are installing Docker from the official Docker repository, which provides the latest version. But first, import the Docker GPG Key which allows you to securely connect 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 the GPG Key has been added, run the following command to add the Docker repository to the sources list.

				
					$ 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
				
			

Next, update the local packages to notify the system of the newly added Docker repository.

				
					$ sudo apt update
				
			

Then install Docker Community Edition  ( Docker-CE) which is freely available using the APT package manager.

				
					$ sudo apt install docker-ce -y
				
			

The command installs Docker alongside package such as docker CLI,  container, docker plugins and other additional packages. Henceforth, to confirm that Docker has been installed, run the command:

				
					$ sudo docker version
				
			

After that, verify that the Docker daemon or service is running, run the command:

				
					$ sudo systemctl status docker
				
			

From the following output, you see that Docker is up and running.

With Docker installed and running, let us now proceed and install Docker Compose.

Step 2: Install Docker Compose on Ubuntu 22.04

There are two ways to install Docker Compose. First, install from Ubuntu repository using the APT package manager as follows.

				
					$ sudo apt install docker-compose
				
			

However, installing from Ubuntu repository does not always provide the latest installation of Docker Compose. 

The most recommended way of installing Docker Compose is to download it from its official GitHub release page. At the time of writing this guide, the current stable version is 2.16.0.  Please download the latest version of Docker compose, run the command:

				
					sudo curl -L https://github.com/docker/compose/releases/download/v2.16.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
				
			

Once the download is complete, make the Docker compose file executable as follows.

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

Confirm that Docker compose has been installed by running the command:

				
					$ docker compose version
				
			

If all went well, you should get the following output.

Step 3: Create a Docker Compose yml file

Importantly, Docker compose comes in handy, when running multi container applications. Suppose your application consists a web server and a database running in separate containers. Building and running these containers from separate containers using a docker file is a time consuming task. But docker compose comes to the rescue with the use of a YAML file, where instructions that define how all the containers are built and run are defined. YAML file specifies as many containers as you want and how they should be built, launched and interact with each other. Once you have specified the YAML file, all you need is to run a single command to build, run, and configure all the containers.

Consider building and running  WordPress CMS using Docker compose. This requires running two containers: a container running WordPress and another one running a MySQL database that stores all the data used by WordPress.

To make this possible in a convenient and reliable way, create a Docker Compose file with all the specifications of the containers defined. We create a separate directory and navigate into it. 

				
					$ mkdir my_app
$ cd my_app
				
			

Next, we create a docker-compose YAML file.

				
					$ sudo vim docker-compose.yml
				
			

Here is the complete Docker Compose YAML file that specifies the two services that are needed to run the application along with the images, volumes and environment.

Docker Compose YAML File

				
					version: '3'

services:
   database:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - database
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: database:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
    db_data:
				
			

Let us break down the Docker compose file:

  • version: '3'  Specifies the version of Docker Compose file version.
  •  services: Defines the containers that are created. Here, two containers will be created: the database container running MySQL and WordPress container.
  • database: Defines the database service.
  • image: This Instructs Docker Compose on what image to pull in order to create the container.
  • environment: This specifies the environment variables that are used to store database credentials such as database name, user and password.
  • volumes: This defines the directories in which data is stored.
  • depends_on: Specifies the dependency between containers. In this case, the keyword instructs the ‘WordPress’ container to start right after the ‘database’ container running  MySQL has started.
  • ports: Specifies port mappings. In this case, port 80 on the container is being exposed to port 8000 on the host machine which WordPress is to be accessed.

Step 4: Build a multi container App with Docker Compose

With the Docker compose file in place, run the web application using the following command:

				
					$ sudo docker-compose up -d

				
			

The command builds and starts the WordPress and the database containers. The -d flag starts the containers in detached mode or runs them in the background.

You get the following out as a result of running the command:

Furthermore, to verify that the containers are running, by executing this command:

				
					$ sudo docker ps

				
			

Get the following output.

From the output, you see that you have WordPress and MySQL containers running on your system. Likewise, to access your newly installed WordPress application, navigate to the following address on your web browser.

				
					http://server-ip:8000/wordpress
				
			

This displays the first step of the installation of WordPress as shown. From here you proceed with the installation right to the very end.

Docker Compose Basic Commands

Above all, Docker compose offers a variety of commands. Here are some of the basic commands to use:

Start all services, run the following command. The -d flag runs the containers in detached mode.

				
					docker-compose up -d
				
			

Check out other related commands here.

Stop and remove all services:

				
					docker-compose down
				
			

To recreate services, if containers are already running:

				
					
docker-compose up -d --no-recreate
				
			

View all the images that the Docker compose has created:

				
					docker-compose images
				
			

List all the running containers:

				
					docker-compose ps
				
			

Check out other related commands here.

To list running processes:

				
					docker-compose top
				
			

View logs:

				
					docker-compose logs
				
			

Thank you for reading How To Install and Use Docker Compose on Ubuntu 22.04. We shall conclude this article. 

How To Install and Use Docker Compose on Ubuntu 22.04 Conclusion

In this guide, we have demonstrated how to install Docker compose on Ubuntu 22.04. We went further and deployed a multi container web application using a Docker compose YAML file and checked out a few docker compose commands. 

Do explore our Docker content in our blog over here

Avatar for James Kiarie
James Kiarie

Hello everyone! My name is James, a certified Linux Administrator, and a tech enthusiast with over 5 years of experience in penning down high-quality guides on Linux and Cloud technologies. Outside work hours I enjoy working out, swimming, listening to music, and reading fiction novels.

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