How to Install and Use Docker Compose on Ubuntu 20.04 | 22.04

install docker compose ubuntu

What is Docker Compose?

Docker Compose is a command-line tool for defining and running multi-container applications. With Docker compose, you can run multiple containers as a single service. The containers are still isolated, but they can interact with each other. With Docker compose, you get the benefits of single-host deployment, great security, ease of setup and configuration which leads to really high productivity and efficiency.

For example, if you have an application that requires an Apache web server and MariaDB database, you can create a docker-compose.yaml file that can run both the containers as a service without the need to start each one separately.

What is Compose File?

Docker Compose uses a YAML (docker-compose.yaml) file for defining configured application services, networks and volumes. The default path for a Compose file is ./docker-compose.yaml. It contains a service definition which configures each container started for that service. With a single command, you can create and start all the services from your YAML configuration file.

In this tutorial, we will show you how to install and use Docker Compose on Ubuntu 20.04 | 22.04.

If you want to host Docker on Cloud and don’t know how to choose the best Cloud hosting provider. You can read my guide on The 8 Best Docker Hosting Platforms in 2022.

Prerequisites

  • A server running Ubuntu 20.04 with Docker installed.
  • A root password is configured on your server.

Note: Please refer to my guide on How to Install and Use Docker for a more detailed reference on Docker.

Install Docker Compose Ubuntu

Docker Compose available in a single binary file. So you don’t need to install it from the Ubuntu repository. At the time of writing this article, the latest version of Docker Compose is 1.28.0. Before downloading the Docker Compose, I would recommend visiting the Docker Compose download page for the latest available version.

First, install the curl command-line tool with the following command:

apt-get install curl -y

Next, pick the latest version of Docker Compose from their download page and run the following command:

curl -L https://github.com/docker/compose/releases/download/1.28.0/docker-compose-Linux-x86_64 -o /usr/bin/docker-compose

After the successful download, set the execution permission to the downloaded binary with the following command:

chmod +x /usr/bin/docker-compose

Next, verify the installed version of Docker Compose with the following command:

docker-compose --version

Output:

docker-compose version 1.28.0, build d02a7b1a

Create a docker-compose.yml File

In this section, we will create a docker-compose.yaml to demonstrate how to use Docker Compose. We will then create a web server environment using the Nginx image.

First, create a project directory inside your home directory:

mkdir ~/project

Next, create a webapp directory for Nginx document root:

mkdir ~/project/webapp

Next, create an index.html file inside the webapp directory:

nano ~/project/webapp/index.html

Add the following codes:

<!doctype html>
<head>
    <title>Docker Compose Example</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/[email protected]/dist/light.min.css">
</head>
<body>

    <h1>This is a Docker Compose Example Page.</h1>
    <p>This content is being served by an Nginx container.</p>

</body>

</body>
</html>

Save and close the file then change the directory to your project and create a docker-compose.yaml file:

cd ~/project
nano docker-compose.yaml

Add the following lines:

version: '3.8'
services:
  webapp:
    image: nginx:alpine
    ports:
      - "8080:80"
    volumes:
      - ./webapp:/usr/share/nginx/html

Save and close the file when you are finished.

Run Docker Compose

At this point, docker-compose.yaml is ready for use. Now, you can use “docker-compose up” command to pull the necessary Docker images, create a container for the web service, and run the containerized environment in background mode:

docker-compose up -d

You should see the following output:

Creating network "project_default" with the default driver
Pulling webapp (nginx:alpine)...
alpine: Pulling from library/nginx
801bfaa63ef2: Pull complete
b1242e25d284: Pull complete
7453d3e6b909: Pull complete
07ce7418c4f8: Pull complete
e295e0624aa3: Pull complete
Digest: sha256:c2ce58e024275728b00a554ac25628af25c54782865b3487b11c21cafb7fabda
Status: Downloaded newer image for nginx:alpine
Creating project_webapp_1 ... done

You can now verify all running container with the following command:

docker-compose ps

You should see all information about your running container in the following output:

      Name                    Command               State          Ports        
--------------------------------------------------------------------------------
project_webapp_1   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:8080->80/tcp

As you can see, your web application is running and listening on port 8080. You can access it using the URL https://your-server-ip:8080. You should see your application page in the following screen:

Docker Compose Basic Commands

In this section, we will show you some most commonly used Docker Compose Commands to manage and interact with your containerized environment.

To start all services, run the following command:

docker-compose up -d

To stop all services, run the following command:

docker-compose down

To pause and unpause all services, run the following command:

docker-compose pause
docker-compose unpause

To check the version of Docker Compose, run the following command:

docker-compose --version

To scale a service, run the following command:

docker-compose up -d -scale

To check the logs produced by containers, run the following command:

docker-compose logs

Conclusion

In the above post, you learned how to install and use Docker Compose on Ubuntu 20.04/Ubuntu 22.04. I hope you can now deploy and scale your application easily with Docker Compose.

Recommended Reading

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva