Skip to content
Related Articles
Open in App
Not now

Related Articles

What is Dockerfile Syntax?

Improve Article
Save Article
  • Last Updated : 31 Oct, 2022
Improve Article
Save Article

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.

1. FROM: 

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.

Example:

FROM java: 8

2. MAINTAINER: 

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.

Example:

MAINTAINER Firstname Lastname <example@geeksforgeeks.com>

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.

Example:

  • RUN unzip install.zip /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.

Example:

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   

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.

Example:

ENV URL_POST=production.example-gfg.com

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

Example:

ENTRYPOINT ["/start.sh"]

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.

Example:

  • CMD [“program-foreground”]
  • CMD [“executable”, “program1”, “program2”]

Note: If you have both ENVIRONMENT and CMD, they are combined together.

8. EXPOSE:

EXPOSE statement maps a port into the container. The ports can be TCP or UDP but by default, it is TCP.

Example:

EXPOSE 3030

9. VOLUME:

The VOLUME statement defines shared volumes or ephemeral volumes depending upon whether you have one or two arguments.

Example:

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.

          VOLUME [“/shared-data”]

10. WORKDIR:

 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.

Example:

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.

Example:

USER geeksforgeeks
USER 4000
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!