Set Up Docker on Ubuntu 18.04/19/20.04

In this tutorial, we'll show you how to install Docker and Docker-Compose latest release on an Ubuntu 18.04, 19.04, 19.10 or 20.04.

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.

Prerequisites

To follow this tutorial along, you'll need a (physical or virtual) machine installed with Ubuntu having a non-root user sudo privileges.

Install Docker

Log in to your Ubuntu machine using a non-root user and type below command to install docker:
sudo apt -y install docker.io

Install Docker-Compose

Once docker installation finished, type below command to install docker-compose:
sudo apt -y install docker-compose

You can check which docker-compose version is running on your Ubuntu using the below command:
sudo docker-compose --version

Check Docker Images

Type below command to check available docker images:
sudo docker images

You will see output similar to the below:
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        3 months ago        1.84kB

Create a Docker File:

You can run docker images using a YAML file like below:
sudo nano docker-compose.yml

Add the following contents into the file:
ubuntu-test:
 image: hello-world

Save and close the file.

Next, run the ubuntu-test docker image using the below command:
sudo docker-compose up

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

This will create a container, attache and run the hello program under docker using ubuntu-test image you created, which confirms that the docker installation appears to be working:

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

You will see an empty output like below:
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

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

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

Wrapping up

Now that you have installed docker and docker-compose on your Ubuntu, start deploying your applications in a completely isolated environment.

No comments:

Powered by Blogger.