Docker Explained – An Introductory Guide To Docker

Tram Ho

Docker has gained immense popularity in this rapidly evolving IT world. Organizations are continuing to adopt it in their production environments.

Take this opportunity to explain Docker in the simplest way. In this blog, the following concepts will be covered:

  • History Before containerization
  • Reasons to use containers
  • What is Docker?
  • Dockerfile, Images & Containers
  • Docker Compose & Docker Swarm
  • Hands-On

1. History Before containerization

Before containerization came along, the first way to isolate and organize applications and their dependencies was to place each application in its own virtual machine. These machines run multiple applications on the same physical hardware, and the process is nothing more than virtualization .

However, virtualization has some disadvantages such as bloated virtual machines, running multiple virtual machines leading to unstable performance, the boot process often takes a long time and the virtual machine will not solve the problems issues such as portability, software updates, or continuous integration and ever-changing.

These limitations have led to the emergence of a new technique called Containerization . Now let me tell you about Containerization .

Containerization

Containerization is a type of Virtualization that brings virtualization to the operating system level. Although Virtualization brings abstraction to the hardware, Containerization brings abstraction to the operating system. For details on containers refer to this Tutorial blog.

2. Reasons to use containers

Here are the reasons for using containers:

  • The container does not have a guest operating system and uses the server operating system. Therefore, they share relevant libraries and resources when needed.
  • Application processing and execution is very fast because the application-specific binaries and container libraries run on the host kernel.
  • Starting the container takes only a fraction of a second, and containers are also lighter and faster than virtualization.

Now that you understand what a container is and why to use it, it’s time to understand our main concept here.

3. What is Docker?

Docker is a platform that bundles an application and all of its dependencies together as containers. This aspect ensures that the application works in all environments.

As you can see in the diagram, each and every application runs on separate containers and has its own set of dependencies & libraries. This ensures that each application is independent of others, giving developers the certainty that they can build applications that do not interfere with each other.

So a developer can build a container with different applications installed on it and provide it to the QA team. The QA team will then simply run the container to recreate the developer’s environment.

If you want to learn more about Docker, then you can click here .

For now, let me show you some more basic concepts about Docker, such as Dockerfile, images, and containers.

4. Dockerfile, Images & Containers

Dockerfile, Docker Images & Docker Containers are three important terms that you need to understand when using Docker.

As you can see in the diagram above when Dockerfile is built, it becomes a Docker Image and when we run Docker Image it eventually becomes the Docker Container.

Refer below to understand all three terms.

Dockerfile : Dockerfile is a text document containing all the commands a user can call on the command line to assemble an Image. So Docker can generate Images automatically by reading instructions from Dockerfile. You can use docker build to create an automated build that executes several command line instructions in succession.

Docker Image : In layman’s terms, Docker Image can be compared to a template used to create Docker Containers. So these read-only templates are the building blocks of Containers. You can use docker run to run Image and create containers.

Docker Images are stored in the Docker Registry. It can be a user’s local repository or a public repository like Docker Hub that allows multiple users to collaborate on building apps.

Docker Container : It is a running version of Docker Image because they contain all the packages needed to run the application. So these are basically ready applications created from Docker Images, which are the last Docker utility.

Now that you know the basics, if you want to learn about the architecture of this technology then you can click here .

5. Docker Compose & Docker Swarm

Docker Compose is a YAML file that contains detailed information about the services, networks, and volumes for setting up the app. So you can use Docker Compose to create separate containers, store them, and help them communicate with each other. Each container will display a port to communicate with other containers.

Docker Swarm is a technique for creating and maintaining a cluster of Docker Engines . Docker Engines can be stored on different nodes and these nodes in remote locations, forming a Cluster when connected in Swarm mode.

In the Practice section, I’ll show you the basic Docker commands and show you how to create a Dockerfile, Images & Docker Container.

6. Hands-On

Follow the steps below to create Dockerfile, Image & Container.

Step 1 : First you have to install Docker. To learn how to install it, you can click here .

Step 2 : After the installation is complete, use the command below to check the version.

docker -v

Step 3 : Now create a folder in which you can create DockerFile and change the current working directory to that.

mkdir images

cd images

Step 4.1 : Now create Dockerfile using the editor. In this case, I used the nano editor.

nano Dockerfile

Step 4.2 : After opening Dockerfile, you have to write it as follows.

  • FROM: Specifies the image to be downloaded
  • MAINTAINER: The image owner’s metadata
  • RUN: Specifies commands to be executed
  • ENTRYPOINT: Specifies the command to be executed first
  • EXPOSE: Specifies the port in contact with the container

Step 4.3 : When you’re done, just save the file.

Step 5 : Build Dockerfile with the command below.

docker build.

** “.” used to build Dockerfile in current directory **

Step 6 : After the above command has been executed, the corresponding docker image will be created. To check if the Docker Image has been created, use the following command.

docker images

Step 7 : Now to create a container based on this image, you must run the following command:

docker run -it -p port_number -d image_id

Where -it is to ensure the container is interactive, -p is for port forwarding, and -d is to run the daemon in the background.

Step 8 : Now you can inspect the created container using the following command:

docker ps

With this we end this blog. I hope you’ve enjoyed this article.

Source: Edureka

Share the news now

Source : Viblo