Introduction to Docker for Beginners

Ritik SaxenaRitik Saxena
7 min read

Hey folks! ๐Ÿ˜Ž, hope you all are doing well

Docker is a very useful tool that is used by many big organizations as it provides the facility to create, manage, deploy and scale applications faster, effectively, and in a very easy way. As it is used by many organizations and developers, It is good to have this skill.

In this blog, we will be learning about some basic concepts that will help us to get started with Docker and use it in our projects. So let's get straight to the topics.

What is Docker?

Docker is an open-source tool, that is easy to use, and easy to set up, and that provides us the facility to bundle our application along with all the dependencies, libraries, and configuration files required by the project into packages called "containers".So here what do we mean by containers? Let's understood it with an example.

example: Let's say we are in a class and for a lecture to run we need students, a teacher, tables and chairs, fans, or A.C., whiteboard. So in this scenario Class is a container, the lecture is our project, and students, teachers, tables, chairs, fans, or A.C. are the dependencies on which our lecture depends.

I hope you now got some idea about what we mean by containers, so let's move ahead and see what problem is it solving.

What problem is it solving?

Before docker, One problem was famous among the developers: "It runs on my machine but not on the user's machine". So to solve this problem docker came into the picture. It provides an isolated environment for our application to run.

But wait one question arises and that is why do we need docker when we already have virtual machines to run our application? ๐Ÿค” So let's look at some differences between docker and virtual machines that will clarify our doubts about why is there a need for docker.

Difference between Virtual Machines and Docker

YKF86Ggyt.jpg

  • Type of Virtualization: VMs uses hardware virtualization whereas Docker uses operating-system virtualization

  • Every VM has its operating system but in docker, it uses the hosts operating system to run containers

  • Time to run: VM takes a longer time as compared to a docker container to run

  • Docker containers use less resources than VM machine

Bi-00XLhU.png

In the above figure, we can see that the RAM allocated to each VM is more than its usage which leads to the wastage of memory but this is not the case in Docker. Only RAM required by the container to run is allocated.

Once the memory and storage are allocated to the VM machine they will be kept reserved for the VM irrespective of whether the machine is running or not. In the case of containers, memory and storage get freed after their work is done or when they are not in use.

The disadvantage of Docker is that it is a very tedious task to manage when we have many container running. So to manage multiple containers we need to take the help of some container orchestration tools. One very famous tool is Kubernetes.

Now we have got to know about the problem it is solving, let's get straight to the next section of this blog i.e, the architecture of Docker.

Docker Architecture

layer5.png

Docker follows a client-server architecture

Docker Client

Like in operating systems users cannot talk directly to the hardware to get their work done so we write some commands on a command line tool (like CMD or Powershell for windows) that interacts by sending a request to the kernel and that kernel manages all the work.

Docker client is also similar to that command line tool with which docker users interact for creating images, containers, etc.

Docker Daemon

Like a kernel in the operating system does all the processing, a Docker daemon is responsible for running containers and images, and managing docker services.

Docker Host

It provides an environment to run Docker daemon, images, and containers. It is also called a docker engine.

Docker Registry

Docker Registry is a place where all the images can be stored.

There are two types of registries:

  • Public Registry: It contains Images that can be accessed by the public

  • Private Registry: It contains Images only accessed by an individual or an organization

How does it work?

The Docker user makes a request with the docker client which then sends that request to the docker daemon which processes the request and returns the result after the processing is done.

Docker Images

Docker Images are templates that can be used to create docker containers.

docker pull image-name # pull image from the docker regisry
docker push image-name:[tag] # push image to the registry
docker rmi image-name # remove image

here,

tag: It refers to the version of our Image

Docker containers

Containers provide an environment that includes all the dependencies to run an application

  • How to create a container from an Image.
docker create --name container-name image-name # create a container from an image
docker start container-name # run a container
docker run --name container-name -d image-name # single command to create and run container
docker rm container-name # remove container

here,

--name: It is an optional tag to give a name to the container, if not passed the docker will automatically assign some random name to the container

-d: It is a flag that runs the container in detached mode i.e. in the background

image-name: Image from which we want to create a container

Note: Docker images are transferrable but containers are not

Mapping of Volumes

Volumes are like storage inside containers for persisting data required by the containers.

Some Types of volume that are used for mapping:

  • Bi-mount volume: used for mapping the directory in the local machine with the volume inside the container
docker run --name container-name -v /folder:/app -d image-name

/folder: It is an absolute path to the local directory that needs to be mapped

/app: It is a volume inside the container that is mapped with the /folder in the local machine

we can set the permission to the mapped container -

docker run --name container-name -v /folder:/app:ro -d image-name # This will map the volume in read-only mode
docker run --name container-name -v /folder:/app:rw -d image-name # This will map the volume in read-write mode

By default volume is mapped in read-write mode i.e any changes made in container are reflected inside the local directory and vice-versa.

  • Anonymous volume: This type of volume is more detailed and tells the docker not to affect it even if the same folder present inside the local machine gets affected
docker run --name container-name -v /app/folder -d image-name

/app/folder: It is an anonymous volume inside the container

Dockerfile

Dockerfile is a file that contains a set of instructions that gets executed in a sequence to create an image, that can be shared and used for creating containers.

How to create an image from a Dockerfile

In this, we will create an image and from that image, we will be creating a container that will print "Hello World" on the console

Step1: Download Docker and install it from docker.com

Step 2: After installation start the docker engine

Step 3: Log in to docker by typing the following command in terminal. it will automatically log you in (if you are already logged in to the docker website on the browser)

docker login

Step 4: Create a folder (you can give any name to the folder), inside that folder create a file with the name Dockerfile (the file name should be the same) and paste the code from below code snippet inside the Dockerfile

From node:alpine
CMD echo "Hello World!"

your file should look like this in the image below

Capture.JPG

Step 5: Now we will create an image from the Dockerfile

docker build tag hello_world .

here,

hello_world: Name of the image that we want to create

. : dot is the path to our Dockerfile

Step 6: Create and run the container from the image that we just created

docker run --name hello_world hello_world

After this, the output should look like this in the image below

docker final output.png

Note: Docker executes commands in a layered format

let's say we are creating a container from the Node image and we already have that image or layer then docker will not pull the same image again from the docker registry rather it will use that same image or layer to create a container.

Step 7: How to get inside the container

docker exec -it container_name bash

here,

exec: It refers to execute

-it: It is a flag that needs to be passed so as to get inside the container into terminal mode

bash: it is the path or folder from where we will start working once we get inside the container

Conclusion

Now we got a basic understanding of what is Docker, its working and architecture, terminologies, and some basic commands in it. It's time to use this knowledge in your project.

0
Subscribe to my newsletter

Read articles from Ritik Saxena directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ritik Saxena
Ritik Saxena