How to Install Docker Compose with Docker on Ubuntu 20.04. Docker Compose is a simple tool that provides a way to orchestrate multiple containers to work together which makes deployment using a yaml
file.
In this guide you are going to learn how to install Docker compose and create a new application using docker compose on Ubuntu 20.04.
Prerequisites
- Running Compute Engine, see the Setting up Compute Engine Instance.
- Docker installed, see how to install Docker on Ubuntu 20.04 LTS.
Install Docker Compose
Once you have Docker installed you can proceed to install Docker Compose.
Here we shall install latest version of Docker Compose from the official GitHub repository.
sudo curl -L https://github.com/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
Set up correct permissions to the downloaded file.
sudo chmod +x /usr/local/bin/docker-compose
Verify the installation using the following command.
docker-compose --version
Output
docker-compose version 1.25.5, build 8a1c60f6
Now Docker Compose is installed successfully and you can start running containers.
Use Docker Compose
Docker Compose allows you to use YAML file to define multiple container applications. With the YAML file you can run, build and configure all containers.
Create a project directory and navigate inside that directory.
mkdir docker-project cd docker-project
Create a YAML file. This is a basic example of the yaml file for hello world .
sudo nano docker-compose.yml
Paste the following contents and save the file.
new-test: image: hello-world
Hit Ctrl + X
followed by Y
and Enter
to save the file and exit.
Now you can execute the following command to pull the hello word image from Docker Hub.
docker-compose up
You will receive an output similar to this.
Output
Pulling new-test (hello-world:)…
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:6a65f928fb91fcfbc963f7aa6d57c8eeb426ad9a20c7ee045538ef34847f44f1
Status: Downloaded newer image for hello-world:latest
Creating docker-project_new-test_1 … done
Attaching to docker-project_new-test_1
new-test_1 |
new-test_1 | Hello from Docker!
new-test_1 | This message shows that your installation appears to be working correctly.
new-test_1 |
new-test_1 | To generate this message, Docker took the following steps:
new-test_1 | 1. The Docker client contacted the Docker daemon.
new-test_1 | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
new-test_1 | (amd64)
new-test_1 | 3. The Docker daemon created a new container from that image which runs the
new-test_1 | executable that produces the output you are currently reading.
new-test_1 | 4. The Docker daemon streamed that output to the Docker client, which sent it
new-test_1 | to your terminal.
new-test_1 |
new-test_1 | To try something more ambitious, you can run an Ubuntu container with:
new-test_1 | $ docker run -it ubuntu bash
new-test_1 |
new-test_1 | Share images, automate workflows, and more with a free Docker ID:
new-test_1 | https://hub.docker.com/
new-test_1 |
new-test_1 | For more examples and ideas, visit:
new-test_1 | https://docs.docker.com/get-started/
new-test_1 |
docker-project_new-test_1 exited with code 0
Now the hello-world image is pulled from Docker Hub and docker-compose creates a container, attaches and runs the program.
You can see all the containers using the following command.
docker ps -a
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0a84984cf2a9 hello-world "/hello" 38 seconds ago Exited (0) 35 seconds ago docker-project_new-test_1
Now you have made a deployment using Docker Compose.
Conclusion
Now you have learned how to install and use Docker Compose with Docker on Ubuntu 20.04.
Thanks for your time. If you face any problem or any feedback, please leave a comment below.