Introduction
Docker Compose is yet another useful Docker tool. It allows users to launch, execute, communicate, and close containers with a single coordinated command. Essentially, Docker Compose is used for defining and running multi-container Docker applications.
This tutorial shows you how to install Docker Compose on Ubuntu 20.04 and how to run a container.
Prerequisites
- A system running Ubuntu 20.04
- A user account with sudo privileges
- Docker installed on Ubuntu 20.04
- A command line/terminal window (Ctrl-Alt-T)
Note: If you are new to Docker, check out the Docker Commands Cheat Sheet.
Install Docker Compose on Ubuntu
Follow the steps below to install the latest Docker Compose version from its official GitHub repository. At the time of writing, Docker Compose 1.29.2 is the latest stable release.
You can also install Docker Compose from the Ubuntu repository by running sudo apt install docker-compose
. However, this option doesn’t guarantee you are downloading the latest version.
Step 1: Upgrade and Update
Start by updating the default repository to ensure you download the latest Docker Compose:
sudo apt update
Then, upgrade the system to ensure all local packages and programs are up to date:
sudo apt upgrade
Step 2: Install curl
To download the binary package using an URL, you need curl. You can check whether you have this command-line utility by typing in the terminal window:
curl
If the output displays “try 'curl --help' or 'curl --manual' for more information
“, move on to the next step. This message means curl is installed.
However, if the terminal says “command not found
“, you need to install curl with:
sudo apt install curl
Note: For some development purposes, you might need to make curl ignore SSL errors. To learn how to do this, please refer to our guide How To Make Curl Ignore Certificate Errors.
Step 3: Download the Latest Docker Version
With the system updated and curl available for use, you can download and install Docker Compose. There is no installation script once the package is locally available, it is automatically installed.
Use the following command:
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
The command instructs the system to save the file in the /usr/local/bin/ directory, under the name docker-compose.
Wait for the download to complete.
Note: The command used above includes the release number of the software. To check whether there are new stable versions, refer to the official list of releases on GitHub. Accordingly, modify the command if needed.
Step 4: Change File Permission
Make the downloaded file executable by changing the file permission with:
sudo chmod +x /usr/local/bin/docker-compose
Step 5: Check Docker Compose Version
To verify the installation, check the Docker Compose version by running:
sudo docker–compose --version
If the output shows the release downloaded in Step 3, you have successfully installed the package.
Uninstall Docker Compose on Ubuntu
Uninstalling Docker Compose from your Ubuntu system is a simple 3-step process.
Step 1: Delete the Binary
First, delete the binary with the command:
sudo rm /usr/local/bin/docker-compose
Step 2: Uninstall the Package
Then, use the apt remove
command to uninstall the software:
sudo apt remove docker-compose
Step 3: Remove Software Dependencies
Finally, remove the unwanted software dependencies by running:
sudo apt autoremove
Run a Sample Container
Check whether the installation is working correctly by running a sample container with Docker Compose. Use Docker’s hello-world container to do so.
1. Create a new directory for the sample container:
mkdir hello–world
2. Move into the previously created directory:
cd hello–world
3. Create a YAML configuration file:
sudo nano docker-compose.yml
4. Next, instruct Docker to create a container under the name hello-world based on the latest Hello World image on Docker Hub. To do this, add the following content to the YAML file:
version: '2'
services:
hello-world:
image:
hello-world:latest
5. Save the changes and exit the text editor (Ctrl+X > Y > Enter).
6. Launch the container while within the hello-world directory with the command:
sudo docker–compose up
This prompts Docker to launch the hello-world container. The output displays a script with a message that the installation is working correctly. Once it shows the script, it exists out of the container.
Note: Users new to Docker may find it difficult to use as they often encounter an issue right after installing it. Check out our guide on how to resolve the “Cannot Connect To The Docker Daemon” error that commonly occurs.
Conclusion
After reading this article, you should have successfully set up Docker Compose on Ubuntu.
Once you get in the practice of launching containers with pre-made Docker images, you may want to start making your own. If you are reading to do so, take a look at How To Create Docker Image With Dockerfile.
原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/tech/dev/223805.html