Hello, readers! In this article, we will be focusing on Building a Dockerfile in detail. So, let us begin!! 🙂
Table of Contents
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.
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.
CMD command parameter
- parameter: The value/parameter entered by the command.
- command: The command to be triggered by the CMD instruction.
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.
ENTRYPOINT command parameter
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.
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.
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.
COPY file path
The ENV instruction adds environment variables to the container environment. That is, it is used to set environment variables in the container.
ENV key value
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,”demo.py”]
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!! 🙂