Dockerfile directive syntax¶
Dockerfile directive syntax refers to the set of instructions used to script the automated building of Docker images^[400-devops-06-kubernetes-k8s-paas-01docker.md]. A Dockerfile acts as a manifest that allows developers to orchestrate the layers of an image, defining everything from the base operating system to the application startup command^[400-devops-06-kubernetes-k8s-paas-01docker.md].
Core Directive Groups¶
The instructions within a Dockerfile can be categorized into four functional groups based on their role in the build lifecycle^[400-devops-06-kubernetes-k8s-paas-01docker.md]:
- Environment & Configuration:
USER,WORKDIR,ENV - File System & Networking:
ADD,EXPOSE - Execution:
RUN - Startup:
CMD,ENTRYPOINT
Syntax Reference¶
The following directives are commonly used to define the image structure and runtime behavior^[400-devops-06-kubernetes-k8s-paas-01docker.md]:
FROM¶
Defines the base image for subsequent instructions^[400-devops-06-kubernetes-k8s-paas-01docker.md].
* Syntax: FROM <image_name>
USER¶
Specifies the user name (or UID) to use when running the image^[400-devops-06-kubernetes-k8s-paas-01docker.md].
* Syntax: USER <user>
ENV¶
Sets environment variables that are available during the build and subsequently in the running container^[400-devops-06-kubernetes-k8s-paas-01docker.md].
* Syntax: ENV <key> <value>
RUN¶
Executes commands in a new layer on top of the current image and commits the results^[400-devops-06-kubernetes-k8s-paas-01docker.md]. It is commonly used for installing packages or modifying system configurations.
* Syntax: RUN <command>
WORKDIR¶
Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow it in the Dockerfile^[400-devops-06-kubernetes-k8s-paas-01docker.md]. If the directory does not exist, it will be created.
* Syntax: WORKDIR <dir>
ADD¶
Copies files, directories, or remote file URLs from the host system into the container filesystem^[400-devops-06-kubernetes-k8s-paas-01docker.md].
* Syntax: ADD <src> <dest>
EXPOSE¶
Informs Docker that the container listens on the specified network ports at runtime^[400-devops-06-kubernetes-k8s-paas-01docker.md]. This does not actually publish the port, but serves as a documentation mechanism between the image builder and the container operator.
* Syntax: EXPOSE <port>
CMD¶
Provides the default command to execute when a container is started from the image^[400-devops-06-kubernetes-k8s-paas-01docker.md]. The container's lifecycle is tied to this process; when the command finishes, the container exits.
* Syntax: CMD ["executable","param1","param2"] (exec form)
Example¶
The following example demonstrates a complete build script combining several directives to configure an Nginx server^[400-devops-06-kubernetes-k8s-paas-01docker.md]:
FROM 909336740/nginx:v1.12.2
USER root
ENV WWW /usr/share/nginx/html
ENV CONF /etc/nginx/conf.d
# Run system updates
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo 'Asia/Shanghai' >/etc/timezone
# Set working directory and copy files
WORKDIR $WWW
ADD index.html $WWW/index.html
ADD demo.od.com.conf $CONF/demo.od.com.conf
# Document the port
EXPOSE 80
# Define the startup command
CMD ["nginx","-g","daemon off;"]
Related Concepts¶
- [[Docker]]
- Container
- [[Docker Image]]
Sources¶
^[400-devops-06-kubernetes-k8s-paas-01docker.md]