Container technology is not a new phenomenon and has been a part of core features of Linux from long back. Most notable among them are Solaris container, OpenVZ and LXC.

Docker came into existence in the year 2013. Initially Docker used LXC libraries as their execution environment but later they have developed their own libraries (libcontainer) and due to its simplicity and ease of use it became so popular.

The containerization is the way of packaging applications along with dependencies so that it can be run in an isolated environment and is a lightweight alternative to full machine virtualization.

What is Docker?

Docker is a tool to easily create, deploy and run applications by using containers. While container allows developers to encapsulate and package their application as a single unit along with necessary libraries and dependencies that can be deployed anywhere.

In other words, Docker is a containerization platform where packaged applications in the form of container can be deployed and run securely and quickly.

What is Docker Container?

Containers are the standard unit of software that packages up code and all its dependencies so that applications can be run faster and reliably in different environments. To spin a container, you need a docker image which includes code, run-time, system tools, libraries and settings to run an application.

A docker image becomes a container when it is running in a docker engine. In other words docker image is a template and a container is the copy of that template during run time. You can spin as many containers as you want from same docker image.

Install Docker on Ubuntu 18.04

Docker can be installed either from official Ubuntu repository or from official Docker repository. The docker package in the official Ubuntu repository may not be the latest.

Therefore to get the latest Docker version installed in the system, we are going to install it from official Docker repository.

To start with, install few prerequisites before proceeding with installing Docker.

# apt install apt-transport-https ca-certificates curl software-properties-common

Now add the GPG key for official Docker repository in your system.

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

Next add the Docker repository to the apt database in the system.

# add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

Finally update the package database and install Docker community edition with the following command.

# apt update && apt install docker-ce

Once Docker is installed, its daemon will be started automatically.

# systemctl status docker

Note: Docker package consists of docker command line tool or docker client along with Docker daemon.

Docker Images

A Docker image is a portable file containing specifications for running an application in a container. Like virtual machine images, a docker image is a container that is saved and packaged to distribute via Docker hub. Docker hub is the official repository where users can store and distribute docker images.

Most of the images in the docker hub are labelled as official that means they are provided as an official packages by respective organization. Apart from official images, you can also find images provided by third party vendors those are further customized on top of official images.

While working with docker, one can also create docker image locally using a specification and upload it to docker hub and share with others.

Let us go through the process of running an application in a container by pulling an image from docker hub. This will help you to understand the process with more clarity.

First search for the image that you are willing to containerize, following example searches for the image apache web server i.e. httpd:

# docker search httpd
NAME          DESCRIPTION                         STARS               OFFICIAL            AUTOMATED
httpd           The Apache HTTP Server Project                  2543                [OK]

centos/httpd                                                                         23                                      [OK]

centos/httpd-24-centos7              Platform for running Apache httpd 2.4 or bui…   22
...
...

Among all the results the very first one is the official image of httpd web server and will be labelled as official.

Now pull the official image of httpd using docker pull. This will only download the image in your local system but will not run it.

# docker pull httpd

Using default tag: latest

latest: Pulling from library/httpd
f5d23c7fed46: Pull complete
b083c5fd185b: Pull complete
bf5100a89e78: Pull complete
98f47fcaa52f: Pull complete
622a9dd8cfed: Pull complete
Digest: sha256:8bd76c050761610773b484e411612a31f299dbf7273763103edbda82acd73642
Status: Downloaded newer image for httpd:latest

The above command will download the httpd image in your system. List the image using docker images command:

# docker images
REPOSITORY       TAG                 IMAGE ID            CREATED             SIZE
httpd           latest              ee39f68eb241        9 hours ago         154MB
nextcloud       latest              ba8e5fa8f005        3 months ago        597MB
...
...

Finally run the httpd server in a container using following docker runcommand:

# docker run -d --name WebServer httpd
 5d9d9138423372cf59e9c0fdd5321dd1753dc33167665a59dc2ee65ab917f8a6

The above docker command takes two arguments. The first argument -d  specifies docker to run the container in detached mode (terminal) and –name argument attach a name to the container which is “WebServer” in the above example.

Once docker successfully launched the container using the httpd image it will return a container id in the terminal.

Now verify if the container is running by using docker ps command:

# docker ps
CONTAINER ID IMAGE  COMMAND             CREATED        STATUS       PORTS  NAMES
5d9d91384233 httpd  "httpd-foreground"  6 minutes ago  Up 6 minutes 80/tcp WebServer
...
...

The status “Up 6 minutes” tells us that the container is started 6 minutes ago and is running. Also the “NAMES” column will show the name of the container which is WebServer that we have specified while running the container.

To stop the container use the docker stop command by specifying either the container id or by using the attached name of the container.

# docker stop WebServer
WebServer

The httpd container has been stopped now. You can remove the unused container from your local system using docker rm command:

# docker rm WebServer
WebServer

The above docker rm command will only remove the container not the httpd docker image that you have downloaded using docker pull command earlier.

To remove the docker image use the following docker rmi command from the terminal:

# docker rmi httpd
Untagged: httpd:latest
Untagged: [email protected]:8bd76c050761610773b484e411612a31f299dbf7273763103edbda82acd73642
Deleted: sha256:ee39f68eb241fd811887da7e21425ac657074363daa9969b9519504785f5d60d
Deleted: sha256:58196fbec21e78d5358fbc70beb15f0c88876cd90b69e7b1ddaf4704555dd740
Deleted: sha256:8f7378bcc6ba3317d1366fa79733e4a8b703d7cde181f671229a6619116477a8
Deleted: sha256:43c1dcceb2782c203ab2b2a5229f4938a2a05fbc70b8508f3de668434422c264
Deleted: sha256:912caf7e400db91ab190309fdf9baa15cf42574a3eefe1e1a4c6ab4a963742ff
Deleted: sha256:d8a33133e477d367977987129313d9072e0ec80894ed4c52c2d88186f354c29a

Docker Commands

At this moment, we have a basic idea of docker images and how to spin a container by using official images from docker hub. Once you have setup a container, what you need to know is few basic docker commands that you can apply to move forward while using docker.

Although there are hundreds of commands that you can use while working with docker but we will cover only those commands which are used frequently.

1. Docker info Command

The docker info command displays system wide information about docker:

# docker info
Containers: 4
Running: 0
Paused: 0
Stopped: 4
Images: 4
Server Version: 18.09.7
...
...

2. Docker Search Command

To search for images in the docker hub registries use the docker search command:

# docker search nginx

The above command will display all the available docker image of NGINX from official and third party provider.

3. Docker Pull Command

The docker pull command pulls images from docker hub into the local system:

# docker pull maradb

4. Docker images Command

To list all the docker images stored in the system, use the command docker images

# docker images

5. Docker run

The docker run command is used to create a container from docker image that you have pulled previously. If the image is not present in the local system then it will first pull the image then it will create the container.

# docker run -d mariadb

6. Docker Start/Stop/Restart/Kill

The docker start command starts a container if it is not running.

# docker start container_id OR container_name

To stop a container use docker stop command:

# docker stop container_id OR container_name

Restart a container using docker restart command

# docker restart container_id OR container_name

To kill a container, use docker kill command. The difference between docker stop and docker kill command is that docker stop command gives the container time to shutdown gracefully while docker kill command kills the container immediately without waiting for container to shutdown gracefully.

7. Docker PS Command

The docke ps command is used to list all the running container:

# docker ps

To list running as well as exited container use the -a option along with docker ps command:

# docker ps -a

8. Docker RM Command

To delete a stopped container use the command docker rm

# docker rm mariadb

9. Docker RMI Command

To delete a stored docker image from the system which has been pulled previously use the command docker rmi

# docker rmi nginx

10. Docker Exec Command

The docker exec command is used to execute a command inside a running container.

For example, the following command will create a text file in the container “WebServer” using touch command.

# docker exec -d WebServer touch /tmp/docker.txt

11. Docker Logs Command

To view the logs of a container, use the docker logs command by specifying container id or container name.

# docker logs WebServer

12. Docker Login Command

The docker login command is used to login to the docker hub by providing login id and password:

# docker login

Login with your Docker ID to push and pull images from Docker Hub. If you don’t have a Docker ID, head over to https://hub.docker.com to create one.

Username: BestWebHosting
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded

13. Docker Push Command

To push a docker image from the local system to docker hub use the following command:

# docker push BestWebHosting/nginx_custom

Conclusion

In this tutorial we learned about what is a Docker, Docker Images and Docker Commands. We also discussed and put together a list of useful docker commands with examples.

This tutorial is targeted for beginners who are starting to the whole new world of container technologies. Read the official documentation of docker to explore more about it.

We hope you like this tutorial, please let us know if you are interested to read more of this kind of stuff and advance docker tutorial by leaving your comments and suggestions below.


Looking for web hosting?

LEAVE A REPLY

Please enter your comment!
Please enter your name here