Home DevOPS Introduction to Containerization and Docker for Beginners

Introduction to Containerization and Docker for Beginners

by Sandhiya Annamalai

In this article, we will focus on the below topics:

  • What is Virtualization?
  • What is Containerization?
  • Virtualization vs Containerization
  • Introduction to Docker
  • Docker Installation
  • What is Docker Hub?
  • Docker Commands

What is Virtualization?

Virtualization is a process of creating multiple virtual machines by dividing the hardware elements (CPU,memory,storage, etc) of a single physical computer.

Advantages of Virtualization:

  • Multiple operating systems can run on the same host machine(Infrastructure).
  • Total cost of ownership is also reduced

Disadvantages of Virtualization:

  • Running multiple Virtual Machines leads to unstable performance.
  • Boot up process is long and takes time.
  • Every virtual machine(vm) requires a separate operating system(os).
  • Each VM occupies more memory and storage.

To overcome these disadvantages we can use a new technique called Containerization. Now we can discuss about Containerization.

What is Containerization?

Containerization is more efficient than virtualization where applications run in separate user spaces, called containers, while using the same shared operating system (OS). Containerization is also a type of Virtualization.

Advantages of Containerization:

  • Containerization is more efficient because there is no guest OS here and utilizes only a host’s operating system.
  • Boot-up process is short and takes a fraction of seconds.
  • Containers are lightweight and faster than Virtual Machines.
  • Containers only contain application specific libraries which are separate for each container and they are faster and do not waste any resources(memories, storage, etc).

Virtualization vs Containerization:

Introduction to Docker:

  • Docker is a containerization  platform, where we can simplify the process of creating, running, maintaining and sharing of applications.
  • Each application will run on a separate container and will have its own set of libraries.
  • Docker would virtualize the operating system of the host on which it is installed and running, rather than virtualizing the hardware components.

Advantages of Docker:

  • multiple containers can reside on a single host
  • Flexible resource sharing.
  • Ease of moving and maintaining your applications.
  • The memory usage of docker containers is very low and unused memory can be allocated to other containers.
  • Docker is highly portable.

Docker Installation on Ubuntu:

Step 1: Update the apt package index and install packages to allow apt to use a repository over HTTPS

$ sudo apt-get update

$ sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Step 2: Add Docker’s official GPG key

$ sudo mkdir -p /etc/apt/keyrings

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo 
gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Step 3: Use the following command to set up the repository

$ echo \

  "deb [arch=$(dpkg --print-architecture) 
signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \

  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 4: To install docker engine

$ sudo apt-get update

$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

Step 5: To  install a specific version

$ sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io docker-compose-plugin

Step 6: Verify that Docker Engine is installed correctly by running the hello-world image

$ sudo docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints a message and exits.

Step 7: To verify docker version

$  docker --version

What is Docker Hub?

  • Docker Hub is Docker’s official registry, it is the default registry when you install Docker. 
  • It contains over thousands of images including official images for Gcc, nginx, Python, Ubuntu, alpine, MySQL etc.
  • Docker Hub is a repository/registry where you can create, test, store, and deploy Docker container images
  • It provides access to public open source image repositories and also lets you create your own private repositories.
  • It allows us to pull and push docker images to and from Docker Hub.

How to create a Docker Hub account?

  • Go to the Docker Hub page.
  • Enter a username that will become your Docker ID.
  • Fill in a valid email address.
  • Sign up after entering a password.
  • Docker will send a confirmation email to the email address you provided.
  • To finish the registration process, confirm your email address.

After creating your docker hub account the screen will shown a below:

Docker Commands:

There are several docker commands you must know when working with Docker. Change into the root user before running docker commands (by using sudo su command).

Following are the basic commands:

  • docker pull
  • docker images
  • docker run
  • docker attach
  • docker ps
  • docker ps -a
  • docker stop
  • docker start
  • docker login
  • docker cp
  • docker rm
  • docker rmi
  • docker commit
  • docker tag
  • docker push
  • docker build

1.docker pull

This command is used to pull images from the docker repository(hub.docker.com).

$ docker pull <image name>:<tag/version>

2.docker images

This command lists all the downloaded images in docker engine.

We can view image details such as repository(image) name, tag(version) name, image ID, creation time, and image size here.

$ docker images

3.docker run

This command is used to create a container from an image.

The docker create command work same as docker run command.

Run command to be used in two ways.

  • Interactive mode(-it)
  • Detached mode(-dt)

Interactive mode:

In this mode we are creating a container and it is directly attached to the created container. Once we enter the exit command the container will stop.

$ docker run -it --name <container name> <image name:tag/version>

Detached mode:

Detached mode(-dt)means that a Docker container runs in the background of your terminal. It does not receive input or display output.

$ docker run -dt --name <container name> <image name:tag/version>

4.docker attach

This command is used to switch into a container which is in detached mode.

$ docker attach <container name>    
(We can also use container ID instead of container name)

5.docker ps

This command is used to list the running containers.

$ docker ps

6.docker ps -a

This command is used to list all the running and exited containers.

$ docker ps -a

7.docker stop

This command stops a running container.

$ docker stop <container name>

8.docker start

This command is used to start and run the stopped container.

$ docker start <container name>

9.docker login

This command is used to login to the docker hub repository from docker.

$ docker login

10.docker cp

We first create a file in a virtual machine using the vi file editor before using the copy(cp) command.

We have created a file in vm, now let’s see how to copy this file into a docker container.

$ docker cp <filename> <container name:destintion path>

11.docker rm

This command is used to delete a container that has been stopped. If the container is still running, use the command rm -f to forcefully delete it.

$ docker rm <container name>
$ docker rm -f <container name>

12.docker rmi

This command is used to delete an image from the docker engine.

$ docker rmi <image name>

13.docker commit

This command is used to create a new image from a running container.

$ docker commit <container name>

How to create a repository in Docker Hub?

Docker Hub repositories allow you to share container images with your team members and customers.

A single Docker Hub repository can hold many Docker images.

Step 1:In the docker hub page click on the repository option on top.

Step 2:click on the create Repository.

Step 3:In the create repository page give the repository name and select either public or private option as per requirement.

Click the create option and the page will shown as below.

14.docker tag

A Docker tag command provides a unique identity to your created Docker image. The same images with different versions identified by tags in a Docker repository.

$ docker tag <image id> <username/repo name:new tag name>

15.docker push

The docker push command is used to share your created docker images to the Docker Hub registry.

You can add multiple images to your repository by adding a specific tag.

$ docker push <username/repo name:image tag name>

Check your docker hub repository after using the push command. You can see that the created docker image was successfully pushed, as shown below.

Dockerfile:

  • A Dockerfile is a text file which contains a set of commands to create a new image from a base image.
  • These commands are executed in the order in which they are written.
  • Execution of these instructions takes place on a base image.

How to create a DockerFile?

Dockerfile is a simple text file, so we can create the Dockerfile by using vi command. In Dockerfile the commands or instructions to be written based on the requirement of the new image which we are going to create.

$ vi Dockerfile

In editor terminal write the code as below to create gcc image with base image ubuntu

FROM ubuntu:20.04                         //base image
RUN apt-get update                        //to update packages
RUN apt-get install gcc -y            //to install gcc 
COPY sample.c .                            //to copy "c" file
RUN gcc -o sample sample.c    //to compile "c" file
CMD ["./sample"]                        //to run "c" file 

16.docker build

  • The docker build command can be used to create a docker image, however the build command must be executed in the same directory as the Dockerfile.
  • The commands in the Dockerfile are executed when an image is built.
$ docker build -t <username/repo name:new image name> dir path
  • -t − is to mention a tag to the image.
  • The directory path is the address of the directory in which we want to install our container.
  • The current directory is specified by the “.” operator.

You may also like