What is Dockerfile Syntax?
A Dockerfile is a script that uses the Docker platform to generate containers automatically. It is essentially a text document that contains all the instructions that a user may use to create an image from the command line. The Docker platform is a Linux-based platform that allows developers to create and execute containers, self-contained programs, and systems that are independent of the underlying infrastructure. Docker, which is based on the Linux kernel’s resource isolation capabilities, allows developers and system administrators to transfer programs across multiple systems and machines by executing them within containers.
Docker containers may operate on any Linux host thanks to Dockerfiles. Docker images are used to construct container environments for applications, and they may be produced manually or automatically using Dockerfiles. Docker containers can execute Linux and Windows apps. Developers may use Dockerfiles to construct an automated container build that steps through a series of command-line instructions. Docker containerization is essentially virtualization at the operating system level. Without the startup overhead of virtual machines, several independent containers can run within a single Linux instance.
Dockerfiles provide business applications with more flexibility and mobility. Dockerfiles are used by IT companies to bundle programs and their dependencies in a virtual container that may operate on bare metal, in public or private clouds, or on-premises. Numerous apps, worker tasks, and other activities can operate independently on a single physical computer or across multiple virtual machines using containers. Kubernetes is an open-source solution for automating the management and orchestration of Dockerfile-based containerized applications.
In this article, we’ll be discussing the syntax for writing a Dockerfile step by step.
A FROM statement 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 the Dockerfile produces more than one image.
FROM java: 8
This statement is a kind of documentation, which defines the author who is creating this Dockerfile or who should you contact if it has bugs.
MAINTAINER Firstname Lastname <firstname.lastname@example.org>
The RUN statement defines running a command through the shell, waiting for it to finish, and saving the result. It tells what process will be running inside the container at the run time.
- RUN unzip install.zip /opt/install
- RUN echo hello
If we define to add some files, ADD statement is used. It basically gives instructions to copy new files, directories, or remote file URLs and then adds them to the filesystem of the image.
To sum up it can add local files, contents of tar archives as well as URLs.
1. Local Files: ADD run.sh /run.sh
2. Tar Archives: ADD project.tar.gz /install/
3. URLs: ADD https://project.example-gfg.com/downloads/1.0/testingproject.rpm/test
ENV statement sets the environment variables both during the build and when running the result. It can be used in the Dockerfile and any scripts it calls. It can be used in the Dockerfile as well as any scripts that the Dockerfile calls. These are also persistent with the container and can be referred to at any moment.
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.
CMD specifies the whole command to run. We can say CMD is the default argument passed into the ENTRYPOINT. The main purpose of the CMD command is to launch the software required in a container.
- CMD [“program-foreground”]
- CMD [“executable”, “program1”, “program2”]
Note: If you have both ENVIRONMENT and CMD, they are combined together.
EXPOSE statement maps a port into the container. The ports can be TCP or UDP but by default, it is TCP.
The VOLUME statement defines shared volumes or ephemeral volumes depending upon whether you have one or two arguments.
1. If you have two arguments, it maps a host path into acontainer path.
VOLUME [“/host/path” “/container/path/”]
2. If you have one arguments, it creates a volume that can be inherited by the later containers.
As the name suggests, WORKDIR sets the directory that the container starts in. Its main purpose is to set the working directory for all future Dockerfile commands.
It sets which user’s container will run as. This can be useful if you have shared network directories involved that assume a fixed username or a fixed user number.
USER geeksforgeeks USER 4000
Please Login to comment...