Unlock the Dockerfile: Best Practices for Dockerfile Creation
Docker is a popular tool used for containerization, which allows developers to package their applications and dependencies into a single, portable unit that can be easily deployed across different environments.
A Dockerfile is a script used to build a Docker image, which is a template for creating containers. In this blog post, we will discuss what a Dockerfile is, its components, and how to build it.
If docker is not installed then download it from the official website.
💎What is a Dockerfile?
A Dockerfile is a plain text file that contains a set of instructions that are used by Docker to build a Docker image. These instructions are executed in order to create a Docker image, which can be used to create Docker containers.
The Dockerfile is a key component of the Docker ecosystem, and it is used to automate the process of building Docker images.
💎General Format of Dockerfile
# Comment
INSTRUCTION arguments
💎Components of a Dockerfile
A Dockerfile consists of a series of instructions that tell Docker how to build an image. The following are the main components of a Dockerfile:
✨Base Image
The first line of a Dockerfile typically specifies the base image, which is the starting point for building the Docker image. A base image is a pre-built image that contains a minimal operating system, such as Ubuntu or Alpine Linux, and any necessary dependencies. Docker images are built on top of base images, which can be publicly available or privately hosted.
✨Instructions
The Dockerfile contains a series of instructions that are used to build the Docker image. These instructions are executed in the order that they are specified in the file. Some common instructions include:
FROM
- specifies the base imageRUN
- runs a command inside the Docker imageCOPY
- copies files from the host machine into the Docker imageADD
- adds files from a URL or archive to the Docker imageCMD
- specifies the default command to run when a container is startedENTRYPOINT
- specifies the command to run when a container is started, and any arguments that should be passed to itEXPOSE
- specifies the port(s) that the container will listen on
✨Comments
Dockerfiles can contain comments, which start with the #
character. Comments are used to document the Dockerfile and provide context for the instructions.
💎Building a Dockerfile
To build a Dockerfile, you need to run the docker build
command, followed by the path to the directory that contains the Dockerfile. For example:
docker build .
This command will build a Docker image using the Dockerfile in the current directory (.
). You can specify a different directory by replacing .
with the path to the directory.
When you run the docker build
command, Docker will read the instructions in the Dockerfile and execute them in order to create the Docker image. The resulting image will be tagged with the name specified in the Dockerfile, or with a randomly generated name if no name is specified.
💎Dockerfile Example
Let’s take a look at an example Dockerfile that installs the Node.js runtime environment and runs a simple “Hello, World!” script:
# Use the official Node.js 16 image as the base image
FROM node:16
# Create a directory for the application
WORKDIR /usr/src/app
# Copy the package.json and package-lock.json files
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the application files
COPY . .
# Expose port 3000
EXPOSE 3000
# Start the application
CMD [ "npm", "start" ]
In this example, the Dockerfile specifies that the official Node.js 16 image should be used as the base image. It then creates a directory for the application, copies the package.json
and package-lock.json
files and installs the dependencies using the npm install
command.
Next, the Dockerfile copies the application files to the Docker image using the COPY
command. It then exposes port 3000 using the EXPOSE
instruction, which allows the container to receive incoming traffic on that port.
Finally, the Dockerfile specifies the default command to run when the container is started using the CMD
instruction. In this case, it runs the npm start
command, which starts the application.
To build this Dockerfile, save it to a file called Dockerfile
, and run the following command:
docker build -t my-node-app .
This command will build the Docker image using the Dockerfile in the current directory and tag it with the name my-node-app
. Once the image is built, you can run a container using the following command:
docker run -p 3000:3000 my-node-app
This command starts a container using the my-node-app
image and maps port 3000 on the host machine to port 3000 on the container. Once the container is running, you can access the application by navigating to http://localhost:3000
in your web browser.
💎Best Practices for Building a Dockerfile
👉Use a minimal base image — When building a Docker image, it’s important to start with a minimal base image. This reduces the size of the image and ensures that only the necessary dependencies are included. For example, instead of using a general-purpose operating system like Ubuntu, you can use a smaller image like Alpine Linux.
FROM alpine:latest
👉Minimize the number of layers — Each instruction in a Dockerfile creates a new layer in the resulting Docker image. Layers increase the size of the image and can make it more difficult to manage. To minimize the number of layers, you can combine multiple instructions into a single RUN
command. For example, instead of running multiple RUN
commands to install packages, you can run a single RUN
command that installs all the packages at once.
RUN apk add --no-cache \
package1 \
package2 \
package3
👉Use caching to speed up builds — Docker uses caching to speed up builds by reusing layers from previous builds. To take advantage of caching, you should order your Dockerfile instructions so that the ones that change frequently are placed towards the end of the file. For example, if you’re copying files into the image, you should do that at the end of the Dockerfile.
COPY . /app
👉Remove unnecessary files — To keep the size of your Docker image small, you should remove any unnecessary files after you’ve finished installing dependencies. For example, you can remove the package cache and any temporary files created during the build process.
RUN apk add --no-cache \
package1 \
package2 \
package3 && \
rm -rf /var/cache/apk/* /tmp/*
👉Use environment variables — Using environment variables in your Dockerfile can make it easier to configure your application when you run it in a container. For example, you can use an environment variable to specify the port that your application listens on.
ENV PORT=3000
EXPOSE $PORT
👉Use a non-root user — Running containers as the root user can be a security risk, as it gives the container access to the host system. Instead, you should use a non-root user to run your application.
RUN adduser -D myuser
USER myuser
👉Use ENTRYPOINT
and CMD
correctly — The ENTRYPOINT
and CMD
instructions specify the command that should be run when the container starts. It's important to use these instructions correctly to ensure that your container behaves as expected. The ENTRYPOINT
instruction specifies the command that should be run, while the CMD
instruction provides default arguments to the ENTRYPOINT
command.
ENTRYPOINT [ "python" ]
CMD [ "app.py" ]
💎Conclusion
A Dockerfile is a script used to build a Docker image, which is a template for creating containers. It contains a series of instructions that tell Docker how to build the image, including the base image, commands to run, and files to copy.
By automating the process of building Docker images, Dockerfiles make it easy to create portable, reproducible containers that can be deployed across different environments.
Hope you find this article insightful 😉 💚
✨ Follow me on -
YouTube — https://www.youtube.com/@theritikchoure/
LinkedIn — https://www.linkedin.com/in/ritikchourasiya/
Twitter — https://twitter.com/theritikchoure