# Mastering Docker: A Comprehensive Containerization Guide

**What is Docker?**

Docker is a **<mark>containerization</mark>** tool that helps us to **<mark>package</mark>** the application and its **dependencies** in the container. Or Docker **packages** an **application** and all its **dependencies** in a container.

**\-&gt; Now what does it mean?**

Here I will explain to you with a simple example, Suppose we have created an application using Angular.

Now to run this Application we will be required -

1. source code should be there
    
2. node should be installed
    
3. npm will be installed
    
4. npm run build command will be run
    

Let us take one more example, Suppose we have a Python application and to run this application we will be required -

1. Source code
    
2. Python should be installed
    

\-&gt; Here **Source code** is the **application source code** and **node, npm** and **python** are the **dependencies** that need to run the application.

\-&gt; Now with the help of **Docker** we create a **package(file)** that contains the **source code** and it's all **dependencies** which is required to **run the application**. And this file is called a **Dockerfile**.

\-&gt;After that we have to **build** this **Dockerfile** which is called a **docker image**.

### To Install the Docker on the Ubuntu Machine -

`sudo apt update`

`sudo apt install` [`docker.io`](http://docker.io) `-y`

`sudo docker version`

### Dockerfile

**\-&gt;** Dockerfile is the Set of Instructions that is required to build the **Docker image.**

**Formate of Dockerfile -** Here is the format of the `Dockerfile`**:**

**INSTRUCTION** arguments

The **instruction** is **not** case-sensitive. However, the convention is for them to be **UPPERCASE** to distinguish them from **arguments** more easily.

\-&gt; A `Dockerfile` **must begin with a** `FROM` instruction.

\-&gt; The `FROM` instruction specifies the ***Base Image*** from which we are building a new image.

**Instructions in Dockerfile are -**

**FROM** - It is used to set the base image from which we create a new image.

Ex - FROM ubuntu

**RUN** - It is used to run the given instructions

Ex- RUN apt update && apt install nginx

**WORKDIR** - It is used to set the working directory.

Ex - WORKDIR &lt;dirname&gt;

**COPY** - It is used to copy the content from local.

Ex - COPY &lt;source&gt; &lt;destination&gt;

**ADD** - It is also used to copy the content from local but it can **also** copy from the internet or using url.

Ex - ADD &lt;source&gt; &lt;url&gt;

**EXPOSE** - It is used to expose the application on the port so that we can access

the application on the exposed port.

Ex - EXPOSE &lt;port number&gt;

**ENV** - It is used to set the environment variable.

Ex - ENV &lt;key&gt;=&lt;value&gt;

**CMD** - It is used as a **Start Command**. But its values can be changed during running the docker command.

Ex - CMD \["echo","Hello World"\]

**ENTRYPOINT** - It is also used as a **Start Command**. But its values can not be changed during running the docker command. But its value can be overridden using **\--entrypoint** during running the docker command\*\*.\*\*

Ex - ENTRYPOINT \["echo","Hello World"\]

**Suppose we have to write a Dockerfile for the Python Application then -**

[**python.py**](http://python.py)

`print("Hello Mastan")`

**Dockerfile**

`FROM python:3`

`WORKDIR /app`

`COPY . .`

`CMD ["python3","`[`python.py`](http://python.py)`"]`

Refer my [Github](https://github.com/Mastangithub123/Docker/tree/master)

\-&gt; Here we can see that <mark>Dockerfile</mark> and <mark>python.py</mark> are in same folder python

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690607054511/caac1705-1b10-41a6-8297-5221c7529925.png?auto=compress,format&format=webp align="left")

\-&gt; Now we are building the docker image and image name is python-image and **'.'** indicates that Dockerfile is in the same directory where docker build command is running.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690607183108/19f642b2-9cd2-4fe9-b257-152d00df5114.png?auto=compress,format&format=webp align="left")

\-&gt; Using docker images command we can see that 'python-image' has been created.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690607353409/df6b454a-e5e3-4a31-b84e-31111c6372fa.png?auto=compress,format&format=webp align="left")

\-&gt; Now we can run the docker image and can see the output of our Python Application. Command to run the image is **"docker run &lt;Image Id&gt; or &lt;Image name&gt;"**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690607449421/af144287-6a68-49a1-9bd8-bafa833cc87a.png?auto=compress,format&format=webp align="left")

### **Docker image -**

Docker Image is an executable package of software that includes everything needed to run the application like source code and its dependencies.

### **Docker Container -**

It is running form of a **docker image.** I.e. when we run the docker image then it is called a **container**.

## Docker Architecture -

\-&gt; Docker architecture contains mainly **three** components -

1. **Docker Client**
    
2. **Docker Daemon**
    
3. **Docker Registry**
    

### **1) Docker Client -**

It is a way through which we interact with **docker daemon**. **Or** it is a **workspace** where we run our **Docker command** to interact with **docker daemon** like - docker run, docker pull etc.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690607974680/e4cd42e2-8256-4ee5-9f7f-f24c2ffe928b.png?auto=compress,format&format=webp align="left")

\-&gt; Here we are running the **docker run command in docker client** area **i.e**. we are sending **request to docker daemon** to run the python-image.

### **2) Docker Daemon -**

It is used to fulfill the **requirement or request** given by the **docker client**

Example - **docker run python-image** command will run the image.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690608413914/60c34637-1107-479b-aadf-30658d730575.png?auto=compress,format&format=webp align="left")

\-&gt; Here **docker daemon** has processed the request given by **docker client** and **run the docker image**

### 3) Docker Registry -

It is a space where we **store the docker image** and **pull the docker image**.

Example - **dockerhub**

### Conclusion

**Docker** is used to **package our application and its dependencies in a container.** After that, we create an **image** of it and **store** this image in the **container registry**. As per requirement we can run that **image** and **create a container**.

Keep your learning journey alive! If you encounter any challenges, please don't hesitate to reach out to me on [**LinkedIn**](https://www.linkedin.com/in/mastan-vali-907151242/).

Thank You!

Best regards,

Mastan 🙌
