How to Install and Use Docker Compose on Ubuntu 20.04 Tutorial

How to Install and Use Docker Compose on Ubuntu 20.04 (Step by Step Tutorial). Docker Compose is an awesome tool that allows you to orchestrate multiple containerized applications at the same time, on a single host, in a very easy way.

With Docker Compose, you define a multi container application in a single file, then spin your application up in a single command which will create and start all the required services.

This is a step by step tutorial on how you can install and use Docker Compose on Ubuntu 20.04.

What is Docker Compose

Docker Compose is an open source tool for defining and running multi container Docker applications. It is a companion to Docker, offering a way to combine multiple containers into a single application. Docker is a Platform as a Service (PaaS) that uses virtualizations to deliver software in the form of containers. Docker CE (Community Edition) is the basic version of Docker EE (Enterprise Edition).

Docker Compose allows you to define your application as a set of related containers and then compose them into a fully functional application that can be configured once and run anywhere on any compatible container host.

You can use Docker Compose to manage applications that are made up of multiple containers. It offers an alternative way of managing app services and running them in isolation from each other.

Using Docker Compose, you can create and structure your application with different services like databases or web servers, etc., each having its containers but with shared dependencies such as database connections, cache stores, and routing rules.

The goal is to provide a way for developers to share common configurations across many containers running on the same host machine.

It’s especially useful when you need to orchestrate more complex applications with multiple services. And it results in significant time savings when building and maintaining applications.

The Benefits of Docker Compose

The benefits of Docker Compose are as follows:

  • Docker Compose allows you to define multiple containers in a single file.
  • Each service is independent of the others.
  • There is no requirement to start each service.
  • It takes the guesswork out of managing application dependencies, and it’s easy to scale your application horizontally or vertically.
  • All can be found in a single YAML file and launched at once.
  • It’s a very useful tool for developing applications locally.
  • It makes it easier to test an application locally before pushing it live.
  • While docker-compose maintains many applications, docker manages a single container.
  • Developers do not have to micromanage each component of their applications and can focus on deployment and testing.
  • Docker Compose is a strong tool for application deployment and setup.
  • It brings flexibility for developers and saves precious development time.

Follow this post to learn how to install and use Docker Compose on Ubuntu 20.04.

How to Install and Use Docker Compose on Ubuntu 20.04

Prerequisites

You will need:

  • Ubuntu 20.04 system.
  • A non root user with sudo privileges.
  • Docker installation on your system. If you do not have it installed, follow our guide how to setup Docker on Ubuntu.

Install Docker Compose

Update Your System

Before everything, update your system to ensure all packages are updated.

				
					sudo apt update
				
			

Download Docker repository

Run the following curl command to download the Docker Compose repo in the /usr/local/bin/docker-compose directory. Or you could download Docker Compose from its official Github repository.

				
					sudo curl -L "https://github.com/docker/compose/releases/download/v2.0.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
				
			

Output:

				
					 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 12.1M  100 12.1M    0     0  1987k      0  0:00:06  0:00:06 --:--:-- 2811k

				
			

Change File Permissions

Once the Docker Compose is downloaded, run the following command to grant permission and make the downloaded file executable.

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

Check Docker Compose Version

Next, verify the installation by checking the docker compose version.

				
					docker-compose --version
				
			

Output:

				
					docker-compose version 2.0.1, build 07737305
				
			

This output demonstrates the successful installation of Docker Compose.

How to use Docker Compose

You need to generate a docker-compose.yml file to utilize docker-compose. The containers that make up the stack of your application are described here.

You can define the networks, environment variables, port bindings, and volumes that your containers will utilize.

Containers specified in the same docker-compose.yml file join the same stack automatically. They’re connected by a Docker network and can interact with each other by utilizing their docker-compose.yml service names as hostnames.

Setup the docker-compose.yml File

To illustrate how to construct a docker-compose.yml file and use Docker Compose, you will use the official Nginx image from the Docker Hub to establish a sample web server environment.

First of all, make and navigate to the compose-demo directory using the following commands.

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

				
			

Inside this directory, create an application folder to act as the document root for your Nginx environment:

				
					mkdir app
				
			

Next, create an index file within the app folder using the following command.

				
					nano app/index.html

				
			

Copy the following code into the index file.

				
					


    
    <p>Docker Compose Demo</p>
        



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




				
			

Save the file and exit the nano editor.

Finally, create the docker-compose.yml file using the following command.

				
					nano docker-compose.yml
				
			

Copy the following code and post it in the docker-compose.yml file.

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

				
			

The docker-compose.yml file defines containers under the services node. Each node declares its image. You can specify the environment or port here. Volume specifies a shared volume between the host and the container.

From the above file, you can see the web container will use the nginx:alpine image, run on port 8000:80 and will be using the shared volume ./app:/usr/share/nginx/html.

Save and exit the file.

Deploy Application

Run the following command to launch the application.

				
					sudo docker-compose up -d
				
			

Output:

				
					Creating network "compose-demo_default" with the default driver
Pulling web (nginx:alpine)...
alpine: Pulling from library/nginx
df9b9388f04a: Pulling fs layer
df9b9388f04a: Downloading [&gt;                                                  ]   29.4kB/2.815MB0351ea626c: Pulling fs layer
…

				
			

This will launch a web container and download the Nginx library.

You can verify the successful deployment with the following command.

				
					sudo docker-compose ps
				
			

Output:

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


				
			

You can navigate to your sample application through localhost:8000.

You can check logs produced by the Nginx container by using the following command.

				
					sudo docker-compose logs
				
			

When you are done with the container, you can conveniently stop or take the deployment down with the following command.

				
					sudo docker-compose stop
sudo docker-compose down

				
			

We have successfully finished reading  about how to Install and Use Docker Compose on Ubuntu 20.04. Let’s conclude.

How to Install and Use Docker Compose on Ubuntu 20.04 Conclusion

In this guide, we have shown you how to install and use Docker Compose on Ubuntu 20.04.

Docker Compose is a tool that allows you to define and run multiple Docker containers from one command line.

Docker Compose is a way to orchestrate multiple containers to work together. Docker compose use cases are for front end web site but also services using it as a supporting function (Redis server). With microservices setup for your app development Docker Compose  can be used to start multiple independently running services. 

To summarize Docker Compose is software that helps to automate the deployment of your code. It’s especially useful when you need to orchestrate more complex applications with multiple services.

Explore our other content on Docker to learn more.

Avatar for Sobia Arshad
Sobia Arshad

Information Security professional with 4+ years of experience. I am interested in learning about new technologies and loves working with all kinds of infrastructures.

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