Tech News

How To Install Docker and Docker-Compose on Ubuntu 19.04

Docker Compose


Docker complements kernel namespacing with a high-level API which operates at the process level. It runs unix processes with strong guarantees of isolation and repeatability across servers. Docker is a great building block for automating distributed systems: large-scale web deployments, database clusters, continuous deployment systems, private PaaS, service-oriented architectures, etc.

docker-compose is a service management software built on top of docker. Define your services and their relationships in a simple YAML file, and let compose handle the rest.

In this tutorial, we'll show you how to install the latest version of Docker and Docker-Compose to help you manage multi-container applications on a Ubuntu 19.04 server.

Prerequisites
To follow this guide you will need a Ubuntu 19.04 server and a non-root user with sudo privileges.

Installing Docker

sudo apt -y install docker.io

Output
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  bridge-utils cgroupfs-mount containerd dns-root-data dnsmasq-base pigz runc ubuntu-fan
Suggested packages:
  ifupdown aufs-tools debootstrap docker-doc rinse zfs-fuse | zfsutils
The following NEW packages will be installed:
  bridge-utils cgroupfs-mount containerd dns-root-data dnsmasq-base docker.io pigz runc ubuntu-fan
0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
Need to get 52.5 MB of archives.
After this operation, 258 MB of additional disk space will be used.
Get:1 http://pk.archive.ubuntu.com/ubuntu disco/universe amd64 pigz amd64 2.4-1 [57.4 kB]
Get:2 http://pk.archive.ubuntu.com/ubuntu disco/main amd64 bridge-utils amd64 1.6-2ubuntu1 [30.5 kB]
Get:3 http://pk.archive.ubuntu.com/ubuntu disco/universe amd64 cgroupfs-mount all 1.4 [6,320 B]
Get:4 http://pk.archive.ubuntu.com/ubuntu disco/universe amd64 runc amd64 1.0.0~rc7+git20190403.029124da-0ubun                              tu1 [1,904 kB]
Get:5 http://pk.archive.ubuntu.com/ubuntu disco/universe amd64 containerd amd64 1.2.6-0ubuntu1 [19.4 MB]
Get:6 http://pk.archive.ubuntu.com/ubuntu disco/main amd64 dns-root-data all 2018091102 [5,472 B]
Get:7 http://pk.archive.ubuntu.com/ubuntu disco/main amd64 dnsmasq-base amd64 2.80-1ubuntu1 [314 kB]
Get:8 http://pk.archive.ubuntu.com/ubuntu disco/universe amd64 docker.io amd64 18.09.5-0ubuntu1 [30.7 MB]
Get:9 http://pk.archive.ubuntu.com/ubuntu disco/main amd64 ubuntu-fan all 0.12.12 [34.6 kB]
Fetched 52.5 MB in 52s (1,001 kB/s)
Preconfiguring packages ...
Selecting previously unselected package pigz.
(Reading database ... 97275 files and directories currently installed.)
Preparing to unpack .../0-pigz_2.4-1_amd64.deb ...
Unpacking pigz (2.4-1) ...
Selecting previously unselected package bridge-utils.
Preparing to unpack .../1-bridge-utils_1.6-2ubuntu1_amd64.deb ...
Unpacking bridge-utils (1.6-2ubuntu1) ...
Selecting previously unselected package cgroupfs-mount.
Preparing to unpack .../2-cgroupfs-mount_1.4_all.deb ...
Unpacking cgroupfs-mount (1.4) ...
Selecting previously unselected package runc.
Preparing to unpack .../3-runc_1.0.0~rc7+git20190403.029124da-0ubuntu1_amd64.deb ...
Unpacking runc (1.0.0~rc7+git20190403.029124da-0ubuntu1) ...
Selecting previously unselected package containerd.
Preparing to unpack .../4-containerd_1.2.6-0ubuntu1_amd64.deb ...
Unpacking containerd (1.2.6-0ubuntu1) ...
Selecting previously unselected package dns-root-data.
Preparing to unpack .../5-dns-root-data_2018091102_all.deb ...
Unpacking dns-root-data (2018091102) ...
Selecting previously unselected package dnsmasq-base.
Preparing to unpack .../6-dnsmasq-base_2.80-1ubuntu1_amd64.deb ...
Unpacking dnsmasq-base (2.80-1ubuntu1) ...
Selecting previously unselected package docker.io.
Preparing to unpack .../7-docker.io_18.09.5-0ubuntu1_amd64.deb ...
Unpacking docker.io (18.09.5-0ubuntu1) ...
Selecting previously unselected package ubuntu-fan.
Preparing to unpack .../8-ubuntu-fan_0.12.12_all.deb ...
Unpacking ubuntu-fan (0.12.12) ...
Setting up dnsmasq-base (2.80-1ubuntu1) ...
Setting up runc (1.0.0~rc7+git20190403.029124da-0ubuntu1) ...
Setting up dns-root-data (2018091102) ...
Setting up bridge-utils (1.6-2ubuntu1) ...
Setting up pigz (2.4-1) ...
Setting up cgroupfs-mount (1.4) ...
Setting up containerd (1.2.6-0ubuntu1) ...
Setting up ubuntu-fan (0.12.12) ...
Setting up docker.io (18.09.5-0ubuntu1) ...
Processing triggers for dbus (1.12.12-1ubuntu1) ...
Processing triggers for systemd (240-6ubuntu5) ...
Processing triggers for man-db (2.8.5-2) ...

Installing Docker-Compose

sudo apt -y install docker-compose

Output
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  golang-docker-credential-helpers libsecret-1-0 libsecret-common python3-cached-property python3-distutils python3-docker
  python3-dockerpty python3-dockerpycreds python3-docopt python3-lib2to3 python3-texttable python3-websocket
The following NEW packages will be installed:
  docker-compose golang-docker-credential-helpers libsecret-1-0 libsecret-common python3-cached-property python3-distutils python3-docker
  python3-dockerpty python3-dockerpycreds python3-docopt python3-lib2to3 python3-texttable python3-websocket
0 upgraded, 13 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,045 kB of archives.
After this operation, 5,620 kB of additional disk space will be used.
Get:1 http://pk.archive.ubuntu.com/ubuntu disco/universe amd64 python3-cached-property all 1.5.1-2 [10.8 kB]
Get:2 http://pk.archive.ubuntu.com/ubuntu disco/main amd64 libsecret-common all 0.18.8-1 [3,936 B]
Get:3 http://pk.archive.ubuntu.com/ubuntu disco/main amd64 libsecret-1-0 amd64 0.18.8-1 [94.7 kB]
Get:4 http://pk.archive.ubuntu.com/ubuntu disco/universe amd64 golang-docker-credential-helpers amd64 0.6.1-1 [478 kB]
Get:5 http://pk.archive.ubuntu.com/ubuntu disco/main amd64 python3-lib2to3 all 3.7.3-1ubuntu1 [75.4 kB]
Get:6 http://pk.archive.ubuntu.com/ubuntu disco/main amd64 python3-distutils all 3.7.3-1ubuntu1 [140 kB]
Get:7 http://pk.archive.ubuntu.com/ubuntu disco/universe amd64 python3-dockerpycreds all 0.3.0-1 [5,252 B]
Get:8 http://pk.archive.ubuntu.com/ubuntu disco/universe amd64 python3-websocket all 0.53.0-1 [32.2 kB]
Get:9 http://pk.archive.ubuntu.com/ubuntu disco/universe amd64 python3-docker all 3.4.1-4 [77.0 kB]
Get:10 http://pk.archive.ubuntu.com/ubuntu disco/universe amd64 python3-dockerpty all 0.4.1-1 [10.8 kB]
Get:11 http://pk.archive.ubuntu.com/ubuntu disco/universe amd64 python3-docopt all 0.6.2-2 [19.4 kB]
Get:12 http://pk.archive.ubuntu.com/ubuntu disco/universe amd64 python3-texttable all 1.6.0-1 [10.9 kB]
Get:13 http://pk.archive.ubuntu.com/ubuntu disco/universe amd64 docker-compose all 1.21.0-3 [85.6 kB]
Fetched 1,045 kB in 2s (447 kB/s)
Selecting previously unselected package python3-cached-property.
(Reading database ... 97600 files and directories currently installed.)
Preparing to unpack .../00-python3-cached-property_1.5.1-2_all.deb ...
Unpacking python3-cached-property (1.5.1-2) ...
Selecting previously unselected package libsecret-common.
Preparing to unpack .../01-libsecret-common_0.18.8-1_all.deb ...
Unpacking libsecret-common (0.18.8-1) ...
Selecting previously unselected package libsecret-1-0:amd64.
Preparing to unpack .../02-libsecret-1-0_0.18.8-1_amd64.deb ...
Unpacking libsecret-1-0:amd64 (0.18.8-1) ...
Selecting previously unselected package golang-docker-credential-helpers.
Preparing to unpack .../03-golang-docker-credential-helpers_0.6.1-1_amd64.deb ...
Unpacking golang-docker-credential-helpers (0.6.1-1) ...
Selecting previously unselected package python3-lib2to3.
Preparing to unpack .../04-python3-lib2to3_3.7.3-1ubuntu1_all.deb ...
Unpacking python3-lib2to3 (3.7.3-1ubuntu1) ...
Selecting previously unselected package python3-distutils.
Preparing to unpack .../05-python3-distutils_3.7.3-1ubuntu1_all.deb ...
Unpacking python3-distutils (3.7.3-1ubuntu1) ...
Selecting previously unselected package python3-dockerpycreds.
Preparing to unpack .../06-python3-dockerpycreds_0.3.0-1_all.deb ...
Unpacking python3-dockerpycreds (0.3.0-1) ...
Selecting previously unselected package python3-websocket.
Preparing to unpack .../07-python3-websocket_0.53.0-1_all.deb ...
Unpacking python3-websocket (0.53.0-1) ...
Selecting previously unselected package python3-docker.
Preparing to unpack .../08-python3-docker_3.4.1-4_all.deb ...
Unpacking python3-docker (3.4.1-4) ...
Selecting previously unselected package python3-dockerpty.
Preparing to unpack .../09-python3-dockerpty_0.4.1-1_all.deb ...
Unpacking python3-dockerpty (0.4.1-1) ...
Selecting previously unselected package python3-docopt.
Preparing to unpack .../10-python3-docopt_0.6.2-2_all.deb ...
Unpacking python3-docopt (0.6.2-2) ...
Selecting previously unselected package python3-texttable.
Preparing to unpack .../11-python3-texttable_1.6.0-1_all.deb ...
Unpacking python3-texttable (1.6.0-1) ...
Selecting previously unselected package docker-compose.
Preparing to unpack .../12-docker-compose_1.21.0-3_all.deb ...
Unpacking docker-compose (1.21.0-3) ...
Setting up python3-cached-property (1.5.1-2) ...
Setting up python3-texttable (1.6.0-1) ...
Setting up python3-docopt (0.6.2-2) ...
Setting up python3-lib2to3 (3.7.3-1ubuntu1) ...
Setting up python3-websocket (0.53.0-1) ...
update-alternatives: using /usr/bin/python3-wsdump to provide /usr/bin/wsdump (wsdump) in auto mode
Setting up libsecret-common (0.18.8-1) ...
Setting up python3-dockerpty (0.4.1-1) ...
Setting up python3-distutils (3.7.3-1ubuntu1) ...
Setting up libsecret-1-0:amd64 (0.18.8-1) ...
Setting up golang-docker-credential-helpers (0.6.1-1) ...
Setting up python3-dockerpycreds (0.3.0-1) ...
Setting up python3-docker (3.4.1-4) ...
Setting up docker-compose (1.21.0-3) ...
Processing triggers for man-db (2.8.5-2) ...
Processing triggers for libc-bin (2.29-0ubuntu2) ...

Verifying Docker Compose Version

sudo docker-compose --version

Output
docker-compose version 1.21.0, build unknown

Check Available Docker Images

sudo docker images

Output
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        3 months ago        1.84kB

sudo nano docker-compose.yml

Put the following contents into the file:

ubuntu1904-test:
 image: hello-world

Save the file and close text editor.

Now, execute the following command:

sudo docker-compose up

docker-compose creates a container, attaches, and runs the hello program, which confirms that the installation appears to be working:

Output
Creating muhammad_ubuntu1904-test_1 ... done
Attaching to muhammad_ubuntu1904-test_1
ubuntu1904-test_1  |
ubuntu1904-test_1  | Hello from Docker!
ubuntu1904-test_1  | This message shows that your installation appears to be working correctly.
ubuntu1904-test_1  |
ubuntu1904-test_1  | To generate this message, Docker took the following steps:
ubuntu1904-test_1  |  1. The Docker client contacted the Docker daemon.
ubuntu1904-test_1  |  2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
ubuntu1904-test_1  |     (amd64)
ubuntu1904-test_1  |  3. The Docker daemon created a new container from that image which runs the
ubuntu1904-test_1  |     executable that produces the output you are currently reading.
ubuntu1904-test_1  |  4. The Docker daemon streamed that output to the Docker client, which sent it
ubuntu1904-test_1  |     to your terminal.
ubuntu1904-test_1  |
ubuntu1904-test_1  | To try something more ambitious, you can run an Ubuntu container with:
ubuntu1904-test_1  |  $ docker run -it ubuntu bash
ubuntu1904-test_1  |
ubuntu1904-test_1  | Share images, automate workflows, and more with a free Docker ID:
ubuntu1904-test_1  |  https://hub.docker.com/
ubuntu1904-test_1  |
ubuntu1904-test_1  | For more examples and ideas, visit:
ubuntu1904-test_1  |  https://docs.docker.com/get-started/
ubuntu1904-test_1  |
muhammad_ubuntu1904-test_1 exited with code 0

Docker containers only run as long as the command is active, so once hello finished running, the container stopped. Consequently, when we look at active processes, the column headers will appear, but the hello-world container won't be listed because it's not running:

sudo docker ps -a

Output
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

We can see the container information, by using the -a flag. This shows all containers, not just active ones:

sudo docker ps -a

Output
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
a6ba72d8a265        hello-world         "/hello"            10 seconds ago      Exited (0) 9 seconds ago                       muhammad_ubuntu1904-test_1


Wrapping up
We've now installed Docker and Docker-Compose, tested our installation by running a Hello World example.

No comments

Comments with links will not be published.