Docker

How to Install Docker on Ubuntu 20.04

What is Docker?

Docker is a tool for delivering your software in packages called a container. A Container is an OS-level virtualization that allows you to run your application in an isolated environment with its libraries and configuration files.

A container is like the virtual machine, but more fast, portable, lightweight. It takes less disk space but more dependent on the host operating system.

Docker is a tool for managing a container. Allows you to do both basic and advanced container management, including build, export, import, manage networking, manage volume, etc.

Why using Docker?

Docker is a tool for delivering your software in packages called a container. A Container is an OS-level virtualization that allows you to run your application in an isolated environment with its libraries and configuration files.

A container is like the virtual machine, but more fast, portable, lightweight. It takes less disk space but more dependent on the host operating system.

Docker is a tool for managing a container. Allows you to do both basic and advanced container management, including build, export, import, manage networking, manage volume, etc.

In this tutorial, you will learn some basic of Docker as below:

  • Installing Docker on Ubuntu 20.04
  • Searching and download for Docker images
  • Running the container
  • Edit the container and create a custom image
  • Push images to DockerHub

Before going any further, run the sudo command below to get the root privileges.

sudo su

Step 1 – Install Docker on Ubuntu 20.04

First, we will install the latest version of Docker CE (Community Edition) to the Ubuntu system.

By default, the ubuntu provides docker packages on its default repository. But, we will keep using the latest version Docker, and we will use the Docker version provided by the official Docker repository.

Update ubuntu repositories and install packages dependencies for Docker.

sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common

Add the GPG key of Docker to the system.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Add the official Docker repository.

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Add Docker Image and Repository for ubuntu 20.04

Now the latest version Docker using the apt command below.

sudo apt install docker-ce docker-ce-cli containerd.io

Once the installation is complete, start the Docker service and add it to the system boot.

systemctl start docker
systemctl enable docker

Check the Docker service and make sure it’s up and running.

systemctl status docker

Start and Check Docker Service on Ubuntu 20.04

After that, check the Docker version.

docker version

Show Docker Version on Ubuntu 20.04

As can a result, you’ve installed Docker on the Ubuntu 20.04.

Step 2 – Docker for Non-root user on Ubuntu 20.04

By default, the Docker only allows the root user to run containers. And for non-root users, you will need to add your user to the Docker group.

Add a non-root user “yujin” to the “docker” group using the command below.

usermod -a -G docker yujin

Now log in as a user “yujin“, check all group ID for that user.

su – yujin
id -nG

As a result, you’ve added a non-root user to the “docker” group.

Set up Docker for Non Root User

Next, run the new container as a non-root user docker “hello-world” using the following command.

docker run hello-world

Now you will get the “Hello World” message from the Docker.

Docker Hello World on Ubuntu 20.04

As a result, you’ve successfully run a Docker container as a non-root user.

Step 3 – Docker Basic Usage

For this section, you will learn how to use the Docker for managing container.

You will learn how to search and download Docker images, run the container, edit the docker container and create a new custom Docker image, and push the image to the DockerHub.

– Basic Help Command

Use the following command to display all available commands on Docker.

docker --help

If you want to display options for a specific command, you can use the following format.

docker COMMAND --help

As a result, you will see some options of your specific docker command.

– Search Docker Images

By default, the Docker provides an image repository called “DockerHub” that allows you to search, download, and upload Docker images.

To search for a docker image, you can use the search command following by the pattern that you want to search.

docker search nginx

Now you will see top Docker images for the pattern “nginx”.

Docker Search Images

– Download Docker Images

To download the Docker image, you can use the pull command as below.

docker pull nginx

Now you’ve downloaded the “nginx” Docker image, and you can use the ‘images’ command to get a list of downloaded docker images on your local system.

docker images

As can be seen, you will get details about “nginx” Docker image that you just downloaded.

Docker Pull Download Image

– Run Docker Container

To run the Docker container, you must specify the Docker image that you want to use.

If the image is available on your local system, the container will immediately run after the command invoked.

When the image is not available on your system, the Docker will automatically search and download an image from the “DockerHub” and run the container.

To run the Docker container, use the ‘run’ command as below.

docker run --name nginx01 -P -d nginx
  • Specify your container name using the “–name” option.
  • To expose ports inside the docker container, use the “-P” option. By default, it will expose port between 32768 to 61000.
  • The “-d” option allows you to run the container on the background.

Now check the container using the “ps” command.

docker ps

As a result, you will get the “nginx01” container is up and running, exposing the port “32768” on the host system.

Run Docker Container

Next, open your web browser, type the server IP address with the port ‘32768’.

http://10.5.5.35:32768/

As can be seen, you will get the default Nginx index page from the container.

Nginx Docker Container

– Edit the Container

To edit the container on the fly, you need to access the docker container itself through the shell.

The Nginx container has provided the bash shell by default.

Run the “exec command to execute the bash shell on the “nginx01″ container.

docker exec -it nginx01 /bin/bash

Once you’ve logged in to the container, go to the “/usr/local/nginx/html” directory and create a new custom index.html file.

cd /usr/share/nginx/html
echo "<h1><center>This is custom index.html yujin/nginx</center></h1>" > index.html

Now you’ve edited the index.html file of the ‘nginx01‘ container.

Edit Docker Container

Type “exit’ to log out from the container.

– Create Custom Docker Images

After editing the container, we will create a new custom docker image called “yujin/nginx” based on “nginx01″ docker container.

Run the “commit command below to create the custom docker image based on docker container “nginx01.

docker commit -m "Change custom index.html" -a "yujin" nginx01 yujin/nginx

After that, check all available docker images on your local system.

docker images

You will get a new docker image called “yujin/nginx“.

Next, run a new container named “nginx02″, based on an original docker image “yujin/nginx“.

docker run --name nginx02 -P -d yujin/nginx

Now check the container.

docker ps

As can be seen, you’ve run a new container “nginx02″ based on the custom docker image “yujin/nginx“.

Create Custom Docker Image from Running Docker Container

Next, open your web browser and type the server IP address following by the port that uses by the ‘nginx02‘ container.

http://10.5.5.35:32769/

As can be seen, you will get the custom index.html file that created on top.

Nginx Container based on Custom Docker Image

– Push Image to DockerHub

The DockerHub is a cloud-based repository provided by Docker that allows you to download, create, and push docker images.

To push custom images to the DockerHub, you will need to register to the https://hub.docker.com.

Once you’ve registered, back to your terminal server, log in to the DockerHub using the following docker command.

docker login

Type your username and password, and you will get the result as below.

Docker Login to DockerHub

As can be seen, you’ve logged in to the DockerHub.

Next, before pushing an image to the DockerHub, change the container tag with your username.

docker tag oldname/nginx username/nginx

After that, push the image to the DockerHub using the “push command below.

docker push username/nginx

Docker push Image to DockerHub

Now back to your browser and rechecks the DockerHub. And you will get the result as below.

Show Image on DockerHub

As can be seen, you’ve pushed a custom docker image to the DockerHub.

Write A Comment