What is a Dockerfile?
A Dockerfile is a text document that has all the command line instructions needed to assemble an image. With the help of a Dockerfile, users can create an automated build that executes several command-line instructions in succession.
Docker builds images automatically by reading instructions from a Dockerfile, which is a text file that contains all commands needed to build a given image in the correct order. A Dockerfile follows a predefined format and set of instructions.
A Docker image is made up of read-only layers, each representing a Dockerfile instruction. The layers are stacked, and each one is a delta of the previous layer’s changes. Below is the diagrammatic representation of the dockerfile.
Docker builds images by reading instructions in dockerfiles. A docker instruction has two components: instruction and argument.
A docker instruction can be written as :
RUN npm install
“RUN” in the instruction and “npm install” is the argument. There are many docker instructions but below are some of the docker instructions
Dockerfile Instructions
Docker file Instruction | Explanation |
FROM | We use “FROM” to specify the base image we want to start from. |
RUN | RUN is used to run commands during the image build process. |
ENV | Sets environment variables within the image, making them accessible both during the build process and while the container is running. If you only need to define build-time variables, you should utilize the ARG instruction. |
COPY | The COPY command is used to copy a file or folder from the host system into the docker image. |
EXPOSE | Used to specify the port you want the docker image to listen to at runtime. |
ADD | An advanced form of COPY instruction. You can copy files from the host system into the docker image. You can also use it to copy files from a URL into a destination in the docker image. In fact, you can use it to copy a tarball from the host system and automatically have it extracted into a destination in the docker image. |
WORKDIR | It’s used to set the current working directory. |
VOLUME | It is used to create or mount the volume to the Docker container |
USER | Sets the user name and UID when running the container. You can use this instruction to set a non-root user of the container. |
LABEL | Specify metadata information of Docker image |
ARG | Defines build-time variables using key-value pairs. However, these ARG variables will not be accessible when the container is running. To maintain a variable within a running container, use ENV instruction instead. |
CMD | Executes a command within a running container. Only one CMD instruction is allowed, and if multiple are present, only the last one takes effect. |
ENTRYPOINT | Specifies the commands that will execute when the Docker container starts. If you don’t specify any ENTRYPOINT, it defaults to “/bin/sh -c”. |
Benefits of using a Dockerfile
Dockerfiles enable faster and more efficient deployment of applications. Once a Docker image has been built, it can be easily deployed to any environment that supports Docker.
Dockerfiles can be used in automation testing to build and run test environments for different applications and services. Using a Dockerfile, you can create an image of a specific test environment that can be easily and consistently recreated, without needing to manually set up and configure the environment on each test run.
You can easily integrate your Dockerfile with a continuous integration and continuous deployment (CI/CD) pipeline, which enables you to automatically build, test and deploy your application with every code change. This helps to reduce the risk of errors and ensures that your application is always up-to-date.