“This is the 17th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Docker can automatically build images by reading instructions in a Dockerfile. A Dockerfile is a text document that contains all the commands that a user can up-tune on the command line to combine images. With Docker build users can create an automated build that executes multiple command line instructions in succession.

Let’s start by looking at what a standard Dockerfile text contains:

FROM docker.io/bitnami/minideb:buster
LABEL maintainer "Bitnami <[email protected]>"

ENV HOME="/" \
    OS_ARCH="amd64" \
    OS_FLAVOUR="debian-10" \
    OS_NAME="linux" \
    PATH="/opt/bitnami/common/bin:/opt/bitnami/etcd/bin:$PATH"

COPY prebuildfs /
# Install required system packages and dependencies
RUN install_packages acl ca-certificates curl gzip jq procps tar
RUN . /opt/bitnami/scripts/libcomponent.sh && component_unpack "gosu" "1.14.0-0" --checksum 3e6fc37ca073b10a73a804d39c2f0c028947a1a596382a4f8ebe43dfbaa3a25e
RUN . /opt/bitnami/scripts/libcomponent.sh && component_unpack "etcd" 3.5.1 track of "0" --checksum 912653e5c0879fd27c8bb0327bea171d7ae504611ec0a9c8fcc2ec6184766a63
RUN chmod g+rwX /opt/bitnami

COPY rootfs /
RUN /opt/bitnami/scripts/etcd/postunpack.sh
ENV BITNAMI_APP_NAME="etcd" \
    BITNAMI_IMAGE_VERSION="3.5.1 track of debian - 10 - r27" \
    ETCDCTL_API="3"

EXPOSE 2379 2380

WORKDIR /opt/bitnami/etcd
USER 1001
ENTRYPOINT [ "/opt/bitnami/scripts/etcd/entrypoint.sh" ]
CMD [ "/opt/bitnami/scripts/etcd/run.sh" ]
Copy the code

Let’s analyze what common instructions a Dockerfile contains

FROM the instructions

The format of the command is as follows: FROM :

The tag or digest command is optional. If the latest version is not used, the latest version is used by default. Such as example, the: the FROM docker. IO/bitnami/minideb: arcade

MAINTAINER instruction

MAINTAINER Specifies MAINTAINER information. The format is MAINTAINER

This command has expired. LABEL is recommended

The LABEL instructions

LABEL is used to add metadata to an image. It is used to declare construction information, such as author, organization, and organization. Format: LABEL

=


… For example: LABEL Maintainer “Bitnami

” When using a LABEL, one or more metadata can be specified. Multiple metadata can be separated by Spaces. It is recommended that all metadata be merged through a LABEL directive to avoid excessive intermediate mirrors.
@bitnami.com>
=value>

ENV command

ENV is used to set environment variables in the format ENV


. If you want to set more than one environment variable at a time, the format is

ENV <key>=<value> \
    <key>=<value> \
    <key>=<value>
Copy the code

As in the example:

ENV HOME="/" \
    OS_ARCH="amd64" \
    OS_FLAVOUR="debian-10" \
    OS_NAME="linux" \
    PATH="/opt/bitnami/common/bin:/opt/bitnami/etcd/bin:$PATH"
Copy the code

ARG instruction

ARG is used to specify variables passed to the build runtime in the format: ARG

[=

] Dynamically assigned via –build-arg

=

in docker run, without specifying that its default values will be used.



WORKDIR instruction

The WORKDIR command is used to specify a working directory, similar to the CD command we normally use. WORKDIR Use WORKDIR to set the working directory. Other commands in Dockerfile, such as RUN, CMD, ENTRYPOINT, ADD, and COPY, will be executed in this directory. When a container is run with Docker run, it can be overridden with the -w argument. As in the example:

WORKDIR /opt/bitnami/etcd
Copy the code

The ADD instruction

ADD is used to ADD local files to the image. Tar files are automatically decompressed to access network resources. Format:

ADD <src> <dest>
If the path contains Spaces, use the following notation
ADD ["<src>"."<dest>"]
Copy the code

The COPY instructions

COPY is similar to ADD, but does not automatically decompress files and access network resources. The format is COPY< SRC >

, as shown in the following example:

COPY rootfs /
Copy the code

The RUN command

RUN is used to execute commands during image construction. There are two command execution modes:

  1. Shell format

RUN Example: RUN install_packages ACL ca-certificates curl gzip jq procps tar 2. exec format RUN [“executable”, “param1”, “param2”] example: RUN [“/dev/file”, “p1”, “P2”]

Note that the intermediate image created by the RUN command is cached and reused for the next build. If you do not want to use the cache image, you can specify –no-cache at build time, as shown in the following example:

docker build --no-cache

CMD command

CMD sets the command to be executed at container startup in the following format:

CMD command param1 param2
CMD ["executable"."param1"."param2"]
Call ENTRYPOINT directly to add parameters
CMD ["param1"]
Copy the code

Example: CMD [“/usr/bin/bash”, “–help”]

ENTRYPOINT instruction

ENTRYPOINT is used to configure the container to make it executable. Can be used with CMD in the following format:

ENTRYPOINT ["executable"."param1"."param2"]
ENTRYPOINT command param1 param2
Copy the code

As in the example:

ENTRYPOINT [ "/opt/bitnami/scripts/etcd/entrypoint.sh" ]
CMD [ "/opt/bitnami/scripts/etcd/run.sh" ]
Copy the code

Commands executed by Docker run do not overwrite ENTRYPOINT, and any parameters specified in the Docker run command are passed to the ENTRYPOINT directive again as parameters. Only the last ENTRYPOINT command is active in a Dockerfile, which means that if you specify multiple entryPoints, only the last ENTRYPOINT instruction is executed.

EXPOSE instruction

EXPOSE Specifies the port for interaction with the outside world. The format is EXPOSE [ …]. As an example in:

EXPOSE 2379 2380
Copy the code

EXPOSE does not actively let the container’s port map to the host. When the host accesses the container ports, it needs to publish those ports with -p when docker Run runs the container, or publish all the EXPOSE exported ports with the -p parameter

VOLUME orders

VOLUME specifies a persistent directory. The format is VOLUME [“< SRC >”,… Example:

VOLUME ["/var/www"."/var/log/apache2"."/etc/apache2"]
Copy the code

Like the EXPOSE directive, VOLUME does not mount to the host, but instead needs to be mapped to the host directory through -v when the container is run through docker Run.

The USER instructions

USER specifies the USER name or UID to RUN the container, and subsequent runs also use the specified USER. When a service does not require administrator rights, you can use this command to specify a running user. Format:

USER user
USER user:group
USER uid:group
USER uid
USER user:gid
USER uid:gid
Copy the code

As in the example:

USER 1001
Copy the code

After specifying a USER using USER, the subsequent commands RUN, CMD, and ENTRYPOINT in Dockerfile will use the USER. When you run the container through Docker run, you can override the specified user with the -u argument.

ONBUILD instruction

The ONBUILD command is triggered when the image being built is used as the base image by other images.

ONBUILD RUN /usr/local/bin/on-build --dir /app/src
Copy the code