Dockerfile instruction syntax¶
Dockerfile instruction syntax refers to the specific set of commands and formatting rules used to define the construction of a Docker image^[600-developer__docker__Dockerfile__Dockerfile-01.md]. These instructions are executed sequentially by the Docker daemon to create a layered filesystem^[600-developer__docker__Dockerfile__Dockerfile-01.md].
Syntax Structure¶
A Dockerfile consists of commands that accept arguments and are typically written in uppercase, though lowercase is also supported^[600-developer__docker__Dockerfile__Dockerfile-01.md].
INSTRUCTION arguments
For example, in the command ENV JAVA_HOME=/jdk1.8.0_91, ENV is the instruction and JAVA_HOME=/jdk1.8.0_91 is the argument^[600-developer__docker__Dockerfile__Dockerfile-01.md].
Comments and Metadata¶
The # character is used to denote comments, which are ignored by the parser^[600-developer__docker__Dockerfile__Dockerfile-01.md]. Metadata can be added to an image using the LABEL instruction, such as specifying a maintainer^[600-developer__docker__Dockerfile__Dockerfile-01.md].
Common Instructions¶
Instructions in a Dockerfile define various stages of the build process, from environment setup to runtime configuration^[600-developer__docker__Dockerfile__Dockerfile-01.md].
Base Image (FROM)¶
The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions^[600-developer__docker__Dockerfile__Dockerfile-01.md].
- Example:
FROM centos:7^[600-developer__docker__Dockerfile__Dockerfile-01.md]
File Operations (ADD, RUN)¶
ADD: Copies files, directories, or remote file URLs from the source context into the container's filesystem at the specified destination^[600-developer__docker__Dockerfile__Dockerfile-01.md].- Example:
ADD jdk-8u91-linux-x64.tar.gz /^[600-developer__docker__Dockerfile__Dockerfile-01.md]
- Example:
RUN: Executes any commands in a new layer on top of the current image and commits the results^[600-developer__docker__Dockerfile__Dockerfile-01.md].- Example:
RUN yum install -y wget^[600-developer__docker__Dockerfile__Dockerfile-01.md]
- Example:
Environment (ENV)¶
The ENV instruction sets environment variables. These variables are key-value pairs that persist into the running container^[600-developer__docker__Dockerfile__Dockerfile-01.md].
- Example:
dockerfile ENV JAVA_HOME=/jdk1.8.0_91 ENV PATH=$PATH:$JAVA_HOME/bin^[600-developer__docker__Dockerfile__Dockerfile-01.md]
Runtime (CMD, EXPOSE)¶
CMD: Specifies the default command to run when a container is launched from the image^[600-developer__docker__Dockerfile__Dockerfile-01.md]. There can only be oneCMDinstruction in a Dockerfile; if multiple are present, the last one takes effect^[600-developer__docker__Dockerfile__Dockerfile-01.md].- Example:
CMD ["/apache-tomcat-8.5.35/bin/catalina.sh", "run"]^[600-developer__docker__Dockerfile__Dockerfile-01.md]
- Example:
EXPOSE: Informs Docker that the container listens on the specified network ports at runtime^[600-developer__docker__Dockerfile__Dockerfile-01.md].- Example:
EXPOSE 8080^[600-developer__docker__Dockerfile__Dockerfile-01.md]
- Example:
Execution¶
To process the syntax in a Dockerfile and build an image, the docker build command is used^[600-developer__docker__Dockerfile__Dockerfile-01.md].
- Example:
docker build -t mytomcat .^[600-developer__docker__Dockerfile__Dockerfile-01.md]
This command reads the Dockerfile from the current directory (.) and tags the resulting image as mytomcat^[600-developer__docker__Dockerfile__Dockerfile-01.md].
Related Concepts¶
- [[Docker]]
- Docker Registry
- [[Containerization]]
Sources¶
^[600-developer__docker__Dockerfile__Dockerfile-01.md]