Steps to Install & Configure Docker Compose on Ubuntu 20.04 LTS

Hello, here we are discussing about for Docker Compose. Docker compose is a docker tool. It is used for deploying the docker images & easier for users to manage the processes of docker containers, including starting up, shutting down, and setting up intra-container linking and volumes.

There are steps to setup docker compose on Ubuntu:

Prerequisite:

  • Ubuntu 20.04 with sudo privileges.

Step 1: Update the system.

apt-get update

Step 2: Install the docker.io.

apt install docker.io

  • Here is the command output.

root@ip-172-31-25-214:/home/ubuntu/project# apt install docker.io
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
bridge-utils containerd dns-root-data dnsmasq-base libidn11 pigz runc ubuntu-fan
Suggested packages:
ifupdown aufs-tools cgroupfs-mount | cgroup-lite debootstrap docker-doc rinse zfs-fuse | zfsutils
The following NEW packages will be installed:
bridge-utils containerd dns-root-data dnsmasq-base docker.io libidn11 pigz runc ubuntu-fan
0 upgraded, 9 newly installed, 0 to remove and 32 not upgraded.
Need to get 74.5 MB of archives.
After this operation, 361 MB of additional disk space will be used.
Do you want to continue? [Y/n]  y

Step 3: Download the Docker Compose.

apt install curl
curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

  • Here is the command output.

root@ip-172-31-25-214:/home/ubuntu# 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
% Total % Received % Xferd  Average Speed  Time Time  Time   Current
Dload Upload Total  Spent  Left   Speed
100  664 100  664 0   0   3405      0 --:--:-- --:--:-- --:--:-- 3387
100 12.1M 100 12.1M  0  0  8872k    0 0:00:01 0:00:01 --:--:-- 18.0M

  • Provide the permission.

chmod +x /usr/local/bin/docker-compose

  • Check Docker compose version.

docker-compose --version

  • Here is the command output.

root@ip-172-31-25-214:/home/ubuntu# docker-compose --version
docker-compose version 1.29.2, build 5becea4c

Step 3: Run a container with docker compose.

  • Create a directory & move into it.

mkdir project
&&
cd project

  • Create a yaml configuration file.Supported filenames are: docker-compose.yml, docker-compose.yaml, compose.yml, compose.yaml

vim docker-compose.yaml

  • Add the following lines:

example:
image: hello-world

  • Then run the following command.

docker-compose up

  • Here is the command output.

root@ip-172-31-25-214:/home/ubuntu/project# docker-compose up
Pulling example (hello-world:)...
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:975f4b14f326b05db86e16de00144f9c12257553bba9484fed41f9b6f2257800
Status: Downloaded newer image for hello-world:latest
Creating project_example_1 ... done
Attaching to project_example_1
example_1 |
example_1 | Hello from Docker!
example_1 | This message shows that your installation appears to be working correctly.
example_1 |
example_1 | To generate this message, Docker took the following steps:
example_1 | 1. The Docker client contacted the Docker daemon.
example_1 | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
example_1 | (amd64)
example_1 | 3. The Docker daemon created a new container from that image which runs the
example_1 | executable that produces the output you are currently reading.
example_1 | 4. The Docker daemon streamed that output to the Docker client, which sent it
example_1 | to your terminal.
example_1 |
example_1 | To try something more ambitious, you can run an Ubuntu container with:
example_1 | $ docker run -it ubuntu bash
example_1 |
example_1 | Share images, automate workflows, and more with a free Docker ID:
example_1 | https://hub.docker.com/
example_1 |
example_1 | For more examples and ideas, visit:
example_1 | https://docs.docker.com/get-started/
example_1 |
project_example_1 exited with code 0

  • Check docker image.

docker images

  • Here is the command output.

root@ip-172-31-25-214:/home/ubuntu/project# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 3 months ago 13.3kB

  • Check the status of docker container.

docker ps -a

  • Here is the command output.

root@ip-172-31-25-214:/home/ubuntu/project# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
93b641f2f1a3 hello-world "/hello" About a minute ago Exited (0) About a minute ago project_example_1

Step 4: Examples of Docker-compose.

Example 1: Using Docker-hub image

  • Add the following lines in docker-compose yaml file.

version: "3.9" # optional since v1.27.0
services:
web:
ports:
- "80:80"
image: httpd

  • Run docker-compose up command with -d option (in the background).

docker-compose up -d

  • Here is the command output.

root@ip-172-31-25-214:/home/ubuntu/project# docker-compose up -d
Creating network "project_default" with the default driver
Pulling web (httpd:)...
latest: Pulling from library/httpd
a2abf6c4d29d: Pull complete
dcc4698797c8: Pull complete
41c22baa66ec: Pull complete
67283bbdd4a0: Pull complete
d982c879c57e: Pull complete
Digest: sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
Status: Downloaded newer image for httpd:latest
Creating project_web_1 ... done

  • Check the running container in the background.

docker-compose ps

  • Here is the command output.

root@ip-172-31-25-214:/home/ubuntu/project# docker-compose ps
Name             Command     State        Ports
---------------------------------------------------------------------------
project_web_1 httpd-foreground Up 0.0.0.0:80->80/tcp,:::80->80/tcp

  • Check the container id.

docker ps -a

  • Here is the command output.

root@ip-172-31-25-214:/home/ubuntu/project# docker ps -a
CONTAINER ID IMAGE   COMMAND CREATED STATUS           PORTS        NAMES
316f49488718 httpd "httpd-foreground" 39 seconds ago Up 38 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp project_web_1

Open web server using the following URL.

http://server-ip

  • Here is the output.

  • Check the logs.

docker-compose logs

  • Here is the command output.

root@ip-172-31-25-214:/home/ubuntu/project# docker-compose logs
Attaching to project_web_1
web_1 | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.20.0.2. Set the 'ServerName' directive globally to suppress this message
web_1 | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.20.0.2. Set the 'ServerName' directive globally to suppress this message
web_1 | [Sat Jan 15 15:15:21.476472 2022] [mpm_event:notice] [pid 1:tid 140439572049216] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
web_1 | [Sat Jan 15 15:15:21.476841 2022] [core:notice] [pid 1:tid 140439572049216] AH00094: Command line: 'httpd -D FOREGROUND'
web_1 | 132.154.69.105 - - [15/Jan/2022:15:15:51 +0000] "GET / HTTP/1.1" 200 45

  • To pause & unpause (resume) the environment execution without changing the state of the containers.

docker-compose pause
&&
docker-compose unpause

  • Here is the command output.

root@ip-172-31-25-214:/home/ubuntu/project# docker-compose pause
Pausing project_web_1 ... done
root@ip-172-31-25-214:/home/ubuntu/project# docker-compose unpause
Unpausing project_web_1 ... done

  • To stop the containers without destroying data.

docker-compose stop

  • To destroy the containers, networks, and volumes associated with the container.

docker-compose down

  • Here is the command output.

root@ip-172-31-25-214:/home/ubuntu/project# docker-compose stop
Stopping project_web_1 ... done
root@ip-172-31-25-214:/home/ubuntu/project# docker-compose down
Removing project_web_1 ... done
Removing network project_default

Example 2: Using Own docker image.

  • Add the following line in docker-compose yaml file.

version: "3.9" # optional since v1.27.0
services:
web:
ports:
- "80:80"
image: docker-image-url

  • Run the docker-compose yaml file.

docker-compose up -d

  • Here is the command output.

root@ip-172-31-25-214:/home/ubuntu/project# docker-compose up -d
Pulling web (1234566.dkr.ecr.sa-east-1.amazonaws.com/test:latest)...
latest: Pulling from test
284055322776: Pull complete
b44765a9561c: Pull complete
e922853d05fa: Pull complete
415cedfe0b25: Pull complete
de8422af7f26: Pull complete
d45fbad1d598: Pull complete
1b164000d479: Pull complete
Digest: sha256:649d47b9024b4a6d5ba52d5554f242655b73e0c17ab1baf227cce21c112fa1aa
Status: Downloaded newer image for 12345667.dkr.ecr.sa-east-1.amazonaws.com/test:latest
Creating project_web_1 ... done

  • Check the container.

docker-compose ps

  • Here is the command output.

root@ip-172-31-25-214:/home/ubuntu/project# docker-compose ps
Name                Command                State   Ports
------------------------------------------------------------------------------------------------------------
project_web_1 /bin/sh -c apache2ctl -D F ... Up 3306/tcp, 443/tcp, 0.0.0.0:80->80/tcp,:::80->80/tcp

Open Web page using URL

http://server-ip

  • Here is the output.

Step 5: To delete the docker container & images.

docker rmi container-id
&&
docker rm image-name

Leave a Reply