Introduction
Docker is a robust, open platform that is designed to help developers and system administrators build, distribute, and deploy container-based applications in software containers. To experience Docker’s full potential, each application’s component must be implemented in its unique container.
Now, if you are running multi-faceted applications with multiple components, it can be daunting to enable all containers to cooperate, start, or even stop.
However, there is a tool built to help you seamlessly orchestrate containers with ease. The tool is called the Docker Compose. Docker Compose is a great tool that makes it easy to define and run complex container-based applications, using a YAML file. The tool allows you to run a single command to create and initiate all the Docker services from YAML configuration.
This guide will help you install and set up Docker Compose on your Ubuntu 22.04 server.
Ready? Let’s get started!
Prerequisites
For the installation to run flawlessly, you need the following:
- Ubuntu 22.04 VPS or dedicated server hosting
- Installed Docker
- A non-root user with Sudo permissions
Step 1 – Installing Docker Compose
In this tutorial, we’ll install the Docker Compose from the official Docker’s GitHub repository and not the one from Ubuntu repositories. This is because the Docker Compose application from GitHub repository is the latest release and is ideally suited for Ubuntu 22.04.
Run the command below to install the current release of Docker Compose:
$ sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
The command above includes the -o flag which stipulates the output file first instead of redirecting the output. This syntax prevents permission issues when running the command using sudo.
Now, execute the command below to adjust the permissions:
$ sudo chmod +x /usr/local/bin/docker-compose
Next, run the command below to check the version:
$ sudo docker-compose --version
This will give you the output below:
The output confirms that the installation process was successful.
Step 2 – Deploying A Container Via Docker Compose
To run a container, the Hello World image available for testing and demonstration purposes from the Docker Hub; a public Docker registry. The image will help us illustrate the YAML configuration as required to deploy a Container using the Docker Compose.
First, run the command below to create a new directory for the file, then move into it:
$ sudo mkdir hello-world
$ cd hello-world
Next, execute the command below to fashion the YAML file:
$ sudo nano docker-compose.yml
This will open the docker-compose.yml file. Add the content below into this file:
my-test:
image: hello-world
The first part of the above content stipulates the container name, whereas the second part specifies the image to be used to create a container. If you don’t want to use this image, you can run the command below to check the images available in your system manually:
$ sudo docker images
This should show you a list of the available images. However, if there are no images you will get the output below:
Now, execute the command below, while in the ~/hello-world file:
$ sudo docker-compose up
This command will search for a local image called hello-world. If this image is not available, then Docker Composer will source it from Docker Hub. In this case, you will get the output below:
Once the image is found, Docker Compose will generate a container, link, and deploy the hello world program.
The Docker client communicated with the Docker daemon.
The daemon then pulled a “hello-world” image from the Docker Hub.
The daemon also generated a new container from the image which implements the executable which delivers the output we are currently reading.
The daemon streamed the output to the Docker client, which relayed it to the terminal.
Once the hello completed running, the Docker container stopped. This is because Docker containers run for as long as the command remains active. As a result, if you inspect the active process, you will not see the hello world container. However, the output will show the column header:
$ sudo docker ps
This will give you the output below:
However, this does not bar us from viewing the container information. Run the command below to see the details of the container:
$ sudo docker ps -a
This will give you the output below:
The details displayed in the output will be helpful when trying to do away with the container.
Step 3 – Removing the Local Image
This an optional step and it’s useful when in need of extra disk space. Here, we’ll use the docker rm command to remove any container referencing the image. First, execute the command below to remove all containers referencing the image:
$ sudo docker rm fcf034029971
Next, run the command below to remove the image:
$ sudo docker rmi hello-world
Conclusion
That’s it! You have successfully installed and configured Docker Compose on your Ubuntu 22.04 system. You’ve also learned how to test its installation, and flawlessly removed the test containers as well as the image. Hope, you enjoyed the whole process!
Check out these top 3 VPS services:
- Check the recommendations for the best VPS and get a suitable one.