Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

What is Dockerfile Syntax?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Pre-requsites: Docker,Dockerfile

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.

The syntax for writing a Dockerfile and Format


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 <>

3. RUN

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 /opt/install
RUN echo hello 

4. ADD

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.


 Local Files: ADD /
 Tar Archives: ADD project.tar.gz /install/
 URLs: ADD   

5. ENV

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.



7. CMD

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 a container path.
        VOLUME ["/host/path" "/container/path/"]
2. If you have one arguments, it creates a volume that can be inherited by the later containers.
          VOLUME ["/shared-data"]


 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.


WORKDIR /directory-name

11. USER

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

12. ARG 

A variable that can be provided at build time is defined by an ARG Instruction. Once it has been specified in the Dockerfile, you can specify it using the –build-arg switch when creating the image. The Dockerfile supports multiple ARG instructions. The only instruction in the Dockerfile that can come before the FROM instruction is ARG.

After the image is created, ARG values are not accessible. An ARG variable value won’t be accessible to a running container.


ARG image_name=latest
FROM centos:$image_name
docker build -t <image-name>:<tag> --build-arg image_name=centos8 .

Best practices for writing Dockerfiles

  1. As a base image, use official images. And whenever it is possible, use Alpine images as a base image.
  2. Don’t copy unnecessary files and folders or install/use unnecessary packages of software.
  3. Running a container process as root is not recommended. As a non-root user, launch the application container process.
  4. Reduce the number of image layers as much as you can.
  5. Wherever possible, try to use multi-stage Docker files to reduce the size of the image.

Creating a New Dockerfile for Maven

#Using offical maven image as a parent image
FROM maven:3.5-jdk-8-alpine as build

#Setting the working directory to /app

#Copy the current directory contents into the container at current directory
COPY . .

#Install the mvn command for maven 
RUN mvn install

By using the above Dockerfile we can build an image of Maven we are setting a Maven alpine image as a base image with the help of FROM command and with the help of WORKDIR setting the working directory for Maven and copy the required files and folders to maven we are using the COPY command and RUN command can download the required commands in the image we are downloading mvn command. 

Note: # is used for comments in Dockerfile.

My Personal Notes arrow_drop_up
Last Updated : 27 Apr, 2023
Like Article
Save Article
Related Tutorials