![]() |
VOOZH | about |
A Dockerfile is a plain-text script that lists ordered instructions the Docker builder runs to assemble an image and automate creating containers. Each instruction appears on its own line with any arguments after it, and the builder executes lines in order to produce a layered, reproducible image.
The general syntax rules are:
FROM, RUN) and are uppercase by convention but case-insensitive.# and are ignored by the builder..dockerignore to exclude files and speed builds.Dockerfile instructions are the ordered commands written in a Dockerfile that tell the Docker builder how to assemble an image and how the resulting container should run.
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: 8This 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>The RUN instruction executes commands during the Docker image build process and saves the result in a new image layer. It is mainly used to install packages, create directories, or configure the environment.
Example:
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.
Example:
Local Files: ADD run.sh /run.sh
Tar Archives: ADD project.tar.gz /install/
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.
Example:
ENV URL_POST=production.example-gfg.comIt 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"]The CMD instruction specifies the default command to run when a container starts. It can also provide default arguments for the ENTRYPOINT instruction.
Example:
CMD ["program-foreground"]
CMD ["executable", "program1", "program2"]Note: If you have both ENVIRONMENT and CMD, they are combined together.
The EXPOSE instruction informs Docker that the container listens on a specific network port at runtime. By default, the protocol is TCP, but UDP can also be specified.
Example:
EXPOSE 5000
EXPOSE 8080/udpThe VOLUME instruction creates a mount point inside the container and is used for persistent or shared data storage.
Example:
1. If you define a single argument, it creates a volume inside a container.
VOLUME ["/shared-data"]
2. If you define multiple arguments, it creates multiple volumes inside the container.
VOLUME ["/volume1", "/volume2"]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-nameIt 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 4000A 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 .#Using offical maven image as a parent image
FROM maven:3.5-jdk-8-alpine as build
#Setting the working directory to /app
WORKDIR /app
#Copy the current directory contents into the container at current directory
COPY . .
#Install the mvn command for maven
RUN mvn installNote: # is used for comments in Dockerfile.