Install and Use Docker Compose on Ubuntu 20.04

In this article, we want to teach you how to Install and Use Docker Compose on Ubuntu 20.04.

Docker Compose is used to run multiple containers as a single service. For example, imagine you had an application that required NGNIX and MySQL, you could create one file which would start both the containers as a service without the need to start each one separately.

Here we show you how to get started with Docker Compose.

Install and Use Docker Compose on Ubuntu 20.04

Before you start to install and use Docker Compose, you need some requirements first.

Requirements

You need to log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this you can check our article about the Initial Server Setup with Ubuntu 20.04.

Also, a Docker is installed on your server. you can follow our article about How to Install and Use Docker on Ubuntu 20.04.

When you are done with these requirements, you can start to install Docker Compose on Ubuntu 20.04.

Install Docker Compose on Ubuntu 20.04

To install Docker compose you need to confirm the latest version of Docker Compose on the releases page.

Now run the following command to download the 1.29.2 release:

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

Then, you need to set the correct permissions with the following command:

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

To see if your installation was successful run the following command:

docker-compose --version

In your output you will see:

Output
docker-compose version 1.29.2, build 5becea4c

Now you have successfully installed Docker Compose on Ubuntu 20.04.

Let’s see how to set up the YAML file, and the minimal configuration required to run a container using Docker Compose.

Set Up a YAML file

To set up a YAML file, you need to create a web server environment using the official Nginx image from DockerHub. This containerized environment will serve a single static HTML file.

First, create a new directory in your home folder, then move into it with the following command:

mkdir ~/compose-demo
cd ~/compose-demo

Then, create a folder to serve as the document root for your Nginx environment with the following command:

mkdir app

Here you need to create a new index.html file with your favorite text editor, here we use vi:

vi app/index.html

Paste the following content into your file:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Docker Compose Demo</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
</head>
<body>

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

</body>
</html>

When you are finished, save and close the file.

Now you can create the YAML file with the following command:

vi docker-compose.yml

Then, paste the following content into the file:

version: '3.7'
services:
web:
image: nginx:alpine
ports:
- "8000:80"
volumes:
- ./app:/usr/share/nginx/html

The docker-compose.yml file typically starts off with the version definition. This will tell Docker Compose which configuration version we’re using.

Save and close the file, when you are done.

Let’s see how to use Docker Compose on Ubuntu 20.04.

Use Docker Compose to Run a Container

At this step, you can execute Docker Compose to bring our environment up.

The following command will download the necessary Docker images, create a container for the web service, and run the containerized environment in background mode:

docker-compose up -d

In your output you will see:

Output
Creating network "compose-demo_default" with the default driver
Pulling web (nginx:alpine)...
alpine: Pulling from library/nginx
a0d0a0d46f8b: Pull complete
4dd4efe90939: Pull complete
c1368e94e1ec: Pull complete
3e72c40d0ff4: Pull complete
969825a5ca61: Pull complete
61074acc7dd2: Pull complete
Digest: sha256:686aac2769fd6e7bab67663fd38750c135b72d993d0bb0a942ab02ef647fc9c3
Status: Downloaded newer image for nginx:alpine
Creating compose-demo_web_1 ... done

Your container is now up and running.

To see that your container is active, run the following command:

docker-compose ps

Your output should similar to this:

Output
Name                 Command                State   Ports
--------------------------------------------------------------------------------
compose-demo_web_1  /docker-entrypoint.sh   Up      0.0.0.0:8000->80/tcp,:::
                    ngin ...                        8000->80/tcp

Now you can access the Docker Compose demo page on Ubuntu 20.04 by typing your server’s IP address in your web browser followed by “:8000”:

http://your-IP-address:8000

You will see a page like this:

Docker Compose Demo page on Ubuntu 20.04

Here we want to show you how to manage Docker Compose commands on Ubuntu 20.04.

Manage Docker Compose Commands

To check the logs produced by your Nginx container, you can use the following command:

docker-compose logs

Your output should similar to this:

Output
Attaching to compose-demo_web_1
web_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
web_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
web_1 | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
web_1 | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
web_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
...

If you want to pause the container without changing the current state of your containers, you can use the following command:

docker-compose pause
Output
Pausing compose-demo_web_1 ... done

You can unpause the container with the following command:

docker-compose unpause
Output
Unpausing compose-demo_web_1 ... done

To stop a container you can use the Docker Compose stop command on Ubuntu 20.04:

docker-compose stop
Output
stopping compose-demo_web_1 ... done

If you want to remove the containers, networks, and volumes associated with this containerized environment, you can use the following command:

docker-compose down
Output
Removing compose-demo_web_1 ... done
Removing network compose-demo_default

This command will not remove the base image, you can bring it up again with the docker-compose up command.

To remove the base image from your system, you can use the following command:

docker image rm nginx:alpine
Output
Untagged: nginx:alpine
Untagged: nginx@sha256:686aac2769fd6e7bab67663fd38750c135b72d993d0bb0a942ab02ef647fc9c3
Deleted: sha256:513f9a9d8748b25cdb0ec6f16b4523af7bba216a6bf0f43f70af75b4cf7cb780
Deleted: sha256:5da2ba1075ada2783aada4fa30ec8cdd56a072759ea7c283de1c505b56ed0e70
Deleted: sha256:198f9b11827076fddb1d6abac11ec4d87d0a52dabea2df751ab998920af715b8
Deleted: sha256:580b12eeafe4e1d9b8a9d7207a1fb54f3def230b51262d0aa1d6ae38a3d6c808
Deleted: sha256:0b316c7704153abae2097fbf7f15d87b9208d175a6112af8ce9ce6757b08dab1
Deleted: sha256:2f2b56622dca526989a9376c2e74fcbc900a9515efb6f804c1462c121f45411d
Deleted: sha256:e2eb06d8af8218cfec8210147357a68b7e13f7c485b991c288c2d01dc228bb68

Conclusion of Install and Use Docker Compose

At this point, you learn how to install and use Docker Compose on Ubuntu 20.04 and set up a containerized environment based on an Nginx web server image. Also, you learn how to manage this environment using Compose commands.

Hope you enjoy this part of the Docker Tutorials on the orcacore website.

May this article about Install and Use Docker Compose on Debian 11 be useful for you.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!