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.
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.
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.
% 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.
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.
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.
Docker Compose Demo
This is a Docker Compose Demo.
This content is being served by an Nginx container.
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.
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.
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 [> ] 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->80/tcp,:::8000->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.
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.
Information Security professional with 4+ years of experience. I am interested in learning about new technologies and loves working with all kinds of infrastructures.
00votes
Article Rating
Subscribe
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.
DisagreeAgree
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.