What is Dockerfile Syntax?
Overview
Every Docker image starts from a Dockerfile. To create an image of an application or script, simply create a file called Dockerfile, which is essentially a text document that contains all the instructions that a user may use to create an image from the command line. In this article, we are going to focus on understanding the docker file syntax and best practices to keep in mind while writing a docker file. Let's start.
Introduction
In this article, we will focus on understanding the syntax and best practices for writing a docker file. We will also go over how to create an image of an application or script using a Dockerfile.
Best Practices
Some best practices to help you write Dockerfile include, but are not limited to -
Separation of Concern
Keep each Dockerfile focused on one primary task to make it easier to reuse in different applications.
Avoid Unnecessary Installations
This will reduce complexity, making the image and container more compact.
Reuse Already-Built Images
There are several built and versioned images on Docker Hub; thus, instead of implementing an already existing image, it's highly advisable to reuse it by importing it.
Have a Limited Number of Layers
A smaller number of layers allows for a more compact or smaller build. Memory is an important factor to consider when building images and containers, as this also affects those who use the image.
Now, let's take a look at the docker file commands and dockerfile syntaxes.
FROM
The FROM statement in a Dockerfile defines which image to download and start from. It must be the first command in your Dockerfile. A Dockerfile can have multiple FROM statements, which means that the Dockerfile produces more than one image.
Dockerfile Syntax:
E.g.
MAINTAINER
This document defines the author of the Dockerfile and how to contact them in case of any bugs.
Dockerfile Syntax:
E.g.
RUN
The RUN statement defines what process will be running inside the container at runtime by specifying a command to run through the shell and waiting for it to finish. This is useful for things like telling the container which scripts to run to start your application when it is launched.
Dockerfile Syntax:
E.g.
ADD
The ADD statement is used to copy new files, directories, or remote file URLs into the filesystem of the image. This can be used to add new content to the image, such as new code or configuration files.
Dockerfile Syntax:
E.g.
ENV
The ENV statement sets environment variables both during the build and when running the docker image.
Dockerfile Syntax:
E.g.
ENTRYPOINT
It specifies the starting of the expression to use when starting your container. Simply ENTRYPOINT specifies the start of the command to run. If your container acts as a command-line program, you can use ENTRYPOINT.
Dockerfile Syntax:
E.g.
CMD
CMD defines the entire command to be executed. It's essentially the default argument passed into the ENTRYPOINT. The main purpose of CMD is to launch the software required in a container.
Dockerfile Syntax:
E.g.
EXPOSE
EXPOSE statement maps a port into the container. The ports can be TCP or UDP, but by default, it is TCP. For example, if you're running a web server inside a container, you might want to EXPOSE port 80 so that people can access your web server from the outside.
Dockerfile Syntax:
E.g.
VOLUME
The VOLUME statement defines shared or ephemeral volumes depending on whether you have one or two arguments.
Dockerfile Syntax & E.g. :
WORKDIR
WORKDIR sets the directory that the container starts in for all future Dockerfile commands, as the name suggests. Its main purpose is to set the working directory so that everything is run smoothly.
Dockerfile Syntax:
E.g.
USER
It sets which user the container will run as.
Dockerfile Syntax:
E.g.
Conclusion
In this article, we have successfully understood the dockerfile syntaxes of various docker file commands, along with their example. Their understanding is crucial to be able to write docker files in the future and subsequently build the docker image. With more practice, you'll become more clear on the concepts.