Building a Dockerfile – All you need to know!

Filed Under: Random
DOCKERFILE

Hello, readers! In this article, we will be focusing on Building a Dockerfile in detail. So, let us begin!! 馃檪


What is a Dockerfile?

Dockerfile can be considered as an executed file that contains all the details to build up an image according to the requirements. That is, it contains all the sets of commands that are necessary to build a docker image on top of it either locally or through a public platform.

By building a Dockerfile, we create an image through the instructions/commands mentioned in the file and the image becomes an executable piece that can be further utilized in the deployments to get the applications up through the commands in the image.

Let us now have a look at the various and mostly used components of a Dockerfile.


Components of a Dockerfile

A Dockerfile consists of various components that make up the entire file. These components trigger various actions that make up the execution of the commands to build an image.


1. CMD

The CMD instruction enables us to execute any command mentioned ahead of it at runtime within a container. With CMD, we can specify the command that we want to be executed upon building the image. Once we run the image through a container, the CMD instruction triggers the execution of the command pointed by the instruction.

We can have multiple CMD instructions within a single Dockerfile. In this case, it applies and executes the commands from the last CMD instruction.

Syntax:

CMD command parameter
  • parameter: The value/parameter entered by the command.
  • command: The command to be triggered by the CMD instruction.

2. ENTRYPOINT

The ENTRYPOINT is similar to CMD instruction but with a slight change in the execution. That is, it is also used to run the commands within the container, but it is used when we want to execute a specific set of commands at runtime. Unlike CMD where in the case of multiple instructions, it executes the last one, with ENTRYPOINT, it executes only the specific instruction at the runtime.

Syntax:

ENTRYPOINT command parameter 

3. FROM

The FROM instruction defines the choice of the base image to build the Dockerfile. We choose the base image over which we want our layers of the Dockerfile to be written and patched.


4. WORKDIR

The WORKDIR instruction sets a working directory within the running container. In case the directory does not already exist, it then creates a new directory as mentioned in the WORKDIR instruction.

WORKDIR directory-name

5. COPY

The COPY instruction enables us to copy the files from our local repositories into the path within the container. Thus, we make the local files available to use within the dedicated path in the container.

Syntax:

COPY file path

6. ENV

The ENV instruction adds environment variables to the container environment. That is, it is used to set environment variables in the container.

Syntax:

ENV key value

7. RUN

The RUN instruction executes the commands specified next to it. With RUN instruction, we can execute specific language-related commands and instructions at ease during the build of the Dockerfile.


Example: Dockerfile for a demo Python application

FROM alpine 
WORKDIR /usr/src/application
ADD gc.json /usr/src/application
ADD demo.py /usr/src/application
ENV gckey "123456"
RUN apt-get update  
CMD [python,鈥漝emo.py鈥漖 

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question. For more such posts related to Docker & Kubernetes, Stay tuned with us.

Till then, Happy Learning!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content