Dockerfile Teardown
Published: 2020-11-11 16:07:59

So your app is ready, and now you want to containerize it using docker.If yes,then read on. 

> Docker file is a file which contains all commands that are required to generate an image.

#### Simple Docker file
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/euwnob5b9k17etqy0ifg.png)

#### Creating a Docker file.

In your project directory create a new file with the exact name Dockerfile.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nbsjawodmb1dj9p13hdu.png)

Inside this file only we will have all the commands listed.Lets see what are those commands.

#### 1. FROM
```
FROM <image>:<tag>
```
Dockerfile must start with FROM. This basically sets the base image.

####### Example
```
FROM node:13-alpine
```
**Instructed to get the base image of node with version 13.**

#### 2. LABEL
```
LABEL <key><value> <key2><value2>
```
Used to set metadata for the image.It is a key-value pair

####### Example
```
LABEL companyname="xyz organization"
```
**Sets companyname label.You can access it using docker inspect <container_id>**  

#### 3. RUN
```
RUN <command>
```
Executes command inside the new image and commits the results.

####### Example 
```
RUN mkdir -p /home/app
```
**This will create a new directory inside the image** 

#### 3. CMD
```
CMD [<command>,<command>]
```
The first commands to be executed after container is created. 

####### Example 
```
CMD ["npm", "start"]
```
**This will execute `npm start` when container is created.** 

#### 4. EXPOSE 
```
EXPOSE <port>
```
Informs Docker that the container listens on the specified network ports.This information is used to interconnect containers using links and to set up port redirection on the host system

####### Example 
```
EXPOSE 3000
```
**This will expose port inside the container as well as for host machine.** 

#### 5. ENV 
```
ENV <key><value>
```
Sets environment variables <key> to the value <value>.This is passed to all `RUN` , `CMD` instructions

####### Example 
```
ENV MONGO_DB_USERNAME=admin
```
**Here MONGO_DB_USERNAME env is set to admin** 

#### 6. COPY 
```
COPY <source> <destination>
```
Copies files from source i.e the host into image.All new files & directories are created with mode 0755.

####### Example 
```
COPY ./package.json /home/app
```
**Here package.json file is being copied from current directory to /home/app in image.** 

#### 7. ADD 
```
ADD <source> <destination>
```
Same as copy but some slight change.
* The ADD instruction can copy and extract TAR files from the Docker host to the Docker image.

####### Example 
```
ADD unzipthis.tar /home/app
```
**This will extract files inside the image to the specified directory.** 

#### 8. VOLUME 
```
VOLUME <path> 
```
Used for persistence inside docker images.This instruction  can create a directory inside the docker image, which can later be mounted to a directory

####### Example 
```
VOLUME /data 
```

#### 9. WORKDIR 
```
WORKDIR <path> 
```
Instruction specifies what the working directory should be inside the Docker image

####### Example 
```
WORKDIR /home/app
```
**This will the directory where the RUN or other instruction will be executed.** 


#### *Now try Reading this dockerfile again*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/euwnob5b9k17etqy0ifg.png)

Hope this will make writing Dockerfile easier for you.