What is Dockerfile?
- A Dockerfile is just a text file that contains all the commands that must be executed to create a Docker image. The Docker images can then be uploaded to places like DockerHub for either public usage or private usage.
- It is used to automate the process of creating a Docker image from a source code or pre-compiled binary.
- Dockerfile is similar to the Makefile which is used in C that contains all the commands which need to run to run the application.
A simple example of a Dockerfile could be this:
FROM registry.aws.site.com:443
RUN microdnf install wget
RUN wget -c project-with-dependencies.jar
COPY ./package.json
RUN npm install
RUN npm run build
This dockerfile will execute all the steps mentioned in the file and will eventually run the npm application. To better understand the file, we should know the Dockerfile commands i.e., FROM, RUN, CMD, COPY, etc.
Explaining them with respect to this example:
- The first line usually uses the FROM command. It is basically used to form the first layer for the container which means it uses that as the base image for the container to run something.
- The RUN command is basically running some Linux command – whatever comes after “RUN” is the Linux command that needs to run on the container. This is mostly used to install some packages and use it. In this case, it is used to install wget and then run a wget command to download the jar in the container. And then it is also used to run the npm commands.
- COPY command as the name suggests copies the folders and files to the container so that it can be accessed from the container directly. Here we copy the package.json file from the current directory to the container.
How To Use Dockerfile Best Practices for Efficient Image Building?
Docker is an open-source platform that enables developers to build, distribute, operate, update, and manage containers.