DEV Community

Cover image for Dockerfile Teardown
Rishav Sinha
Rishav Sinha

Posted on

Dockerfile Teardown

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

Creating a Docker file.

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

Alt Text

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

1. FROM

FROM <image>:<tag>
Enter fullscreen mode Exit fullscreen mode

Dockerfile must start with FROM. This basically sets the base image.

# Example
FROM node:13-alpine
Enter fullscreen mode Exit fullscreen mode

Instructed to get the base image of node with version 13.

2. LABEL

LABEL <key><value> <key2><value2>
Enter fullscreen mode Exit fullscreen mode

Used to set metadata for the image.It is a key-value pair

# Example
LABEL companyname="xyz organization"
Enter fullscreen mode Exit fullscreen mode

Sets companyname label.You can access it using docker inspect

3. RUN

RUN <command>
Enter fullscreen mode Exit fullscreen mode

Executes command inside the new image and commits the results.

# Example
RUN mkdir -p /home/app
Enter fullscreen mode Exit fullscreen mode

This will create a new directory inside the image

3. CMD

CMD [<command>,<command>]
Enter fullscreen mode Exit fullscreen mode

The first commands to be executed after container is created.

# Example
CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode

This will execute npm start when container is created.

4. EXPOSE

EXPOSE <port>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This will expose port inside the container as well as for host machine.

5. ENV

ENV <key><value>
Enter fullscreen mode Exit fullscreen mode

Sets environment variables to the value .This is passed to all RUN , CMD instructions

# Example
ENV MONGO_DB_USERNAME=admin
Enter fullscreen mode Exit fullscreen mode

Here MONGO_DB_USERNAME env is set to admin

6. COPY

COPY <source> <destination>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Here package.json file is being copied from current directory to /home/app in image.

7. ADD

ADD <source> <destination>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This will extract files inside the image to the specified directory.

8. VOLUME

VOLUME <path> 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

9. WORKDIR

WORKDIR <path> 
Enter fullscreen mode Exit fullscreen mode

Instruction specifies what the working directory should be inside the Docker image

# Example
WORKDIR /home/app
Enter fullscreen mode Exit fullscreen mode

This will the directory where the RUN or other instruction will be executed.

Now try Reading this dockerfile again

Alt Text

Hope this will make writing Dockerfile easier for you.

Top comments (0)