Introduction
Docker Compose is a tool for defining and running multi-container Docker applications. It allows users to launch, execute, communicate, and close containers with a single coordinated command.
This guide will show you how to install Docker Compose on Ubuntu.
Prerequisites
- A system running Ubuntu 18.04
- A user account with sudo privileges
- Docker installed on Ubuntu 18.04
- A command-line/terminal window (Ctrl-Alt-T)
Steps for Installing Docker Compose on Ubuntu
Update Software Repositories and Packages
Start by updating the software repositories and software packages. Open a terminal window, and enter the following:
sudo apt-get update
sudo apt-get upgrade
Check the curl
command by entering:
curl
The system should respond as seen in the image below:
If you see a different message, like curl: command not found
, you’ll need to install it.
To install curl, enter the following:
sudo apt install curl
Download the Latest Docker Compose Version
1. To download the latest version of Docker Compose, use the command:
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
This will download Docker Container 1.24.0. (At the time this article was written, Docker Compose 1.24.0 is the latest version.)
- The
–L
option tells the system to follow any redirects, in case the file has been moved - If you want a different version of Docker Compose, you may browse the list and substitute your preferred version for /1.24.0/
- The
–o
option changes the filename, so it’s easier to type - The file will be saved in /usr/local/bin/
2. Next, change the file permissions to allow the new software to be executed on Ubuntu:
sudo chmod +x /usr/local/bin/docker-compose
You do not need to run an installation script for Docker Compose. Once downloaded, the software is ready to use.
Note: You can also install Docker Compose from the official Ubuntu repository. Simply run sudo apt-get install docker-compose
. However, it is recommended to install the software package from Docker’s official GitHub repository. That way, you are always installing the latest version.
Verify Docker Compose Installation
To test for a successful installation, check the version using:
docker–compose –version
The output should appear similar to this:
How to Uninstall Docker Compose
To uninstall Docker Compose, simply delete the binary:
sudo rm /usr/local/bin/docker-compose
If you have installed Docker Compose using apt-get
, use the following command to uninstall the package:
sudo apt-get remove docker-compose
Next, run a command to remove unnecessary software dependencies:
sudo apt-get autoremove
Getting Started Using Docker Compose
Run a Sample Container with Docker Compose
1. Switch back to your home directory (if needed):
cd ~
2. Create and switch to a new directory:
mkdir hello–world
cd hello–world
3. Create and edit a new YAML configuration file:
sudo nano docker-compose.yml
YAML is a type of configuration file. This file will create a container named test-file based on the Hello World image on Docker Hub.
4. Enter the following text into the editor:
version: '2'
services:
hello-world:
image:
hello-world:latest
5. Press Ctrl-X to exit > Y to save the file > Enter.
If you have existing images on your system, you can display a list with the command:
sudo docker images
Running this command now will generate an empty list. Docker will automatically download the image based on this configuration file.
Enter:
sudo docker–compose up
Docker will look for a local hello-world image. If it can’t find one, it will download one. Then, Docker will create a container, launch it, then run the hello-world script. You should see the output on your screen as below:
Docker-compose now creates a container and runs the hello program. This action confirms that the installation is operating.
It also displays an explanation of the actions completed:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
Once this operation finishes, Docker closes the container. You should see a command prompt at the end of the output.
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.
List Docker Images and Containers
To display a list of all Docker images stored locally:
sudo docker images ls –a
The –a
option shows all docker images. You should see a listing with the hello-world image.
To see a list all containers use:
sudo docker ps -a
To view all currently running containers:
sudo docker ps
Note that this list is empty. This shows you that once the hello-world image completes, it closes out automatically.
Remove a Docker Image and Container
To remove a Docker image, you need to know its IMAGE ID. Use the command for listing images from the passage above to copy that information.
Once you have the ID, run the following syntax to erase that image:
docker image rm [IMAGE_ID]
You can also use this command to remove multiple Docker images:
docker image rm [IMAGE_ID1] [IMAGE_ID2] [IMAGE_ID3]
To remove a Docker container use:
docker rm [CONTAINER_ID]
Replace [CONTAINER_ID] with the actual container ID.
Since this is a test, we don’t need to tie up disk space with old images. That’s why we will erase the example image. You won’t be able to remove an image until you remove all the containers linked to it.
First, remove the container:
docker rm [CONTAINER_ID]
As you only used the one container, you should be able to remove the image:
docker rmi hello-world
Conclusion
Now you know how to install and use Docker Compose on Ubuntu 18.04. Get started creating and managing new containers.
原创文章,作者:kirin,如若转载,请注明出处:https://blog.ytso.com/226142.html