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

Hello, I’m looking at the mountains.

This is the second part of Jakob Jenkov’s Docker Tutorial.

Dockerfile contains a set of instructions on how to build a Docker image, through the Docker build command to execute the Dockerfile file, you can build a Docker image, this article describes how to write a Dockerfile file and build a Docker image.

The benefits of Dockerfile

The Dockerfile file explains in writing how to build a Docker image. Docker images usually contain the following contents:

  • First of all, a basic Docker image is needed to build your own Docker image on this basic Docker image.

  • A set of tools and applications that need to be installed in the Docker image.

  • A set of files (such as configuration files) that need to be copied into a Docker image.

  • Network (TPC/UDP) ports or others that may need to be opened in the firewall.

  • Wait…

First of all, putting this in writing in a Dockerfile means that we don’t have to remember how the application is installed, what the operating system requires, which applications need to be installed, which files need to be assigned, which network ports need to be opened, etc., all of which are recorded in the Dockerfile.

In addition, through Dockerfile file to build Docker image, we do not need to manually perform these tedious repetitive and error-prone work. Docker does this automatically, easily, quickly, and error-free.

Third, we can easily share Dockerfile files with others, and they can build Docker images themselves.

Fourth, Dockerfiles are easily stored in a version controller like Git, so you can keep track of changes to your Dockerfiles (server, application configuration). Version controllers also make it easy for people to collaborate on, for example, dockerfiles and share dockerfiles.

The structure of Dockerfile

A Dockerfile contains a set of instructions, each consisting of a command and a parameter, similar to a command line executable. Here is a simple example of a Dockerfile:

Here are more instructions for installing software and copying files to the image. COPY /myapp/target/myapp.jar /myapp/myapp.jar /myapp/myapp.jar CMD echo Starting Docker ContainerCopy the code

Docker base image

Docker images are made up of layers, and each layer adds something to the final Docker image. Each layer is actually a separate Docker image, so a Docker image is composed of one or more layer images, on which we can add our own layer.

When specifying your own Docker image from a Dockerfile file, you usually start with a base Docker image. This is another Docker image on which you can build your own Docker image. The Docker base image may itself contain multiple layers and be built on top of another base image.

We can specify the Docker image as the base image in the Dockerfile file using the From command, as described in the following section.

MAINTAINER

The MAINTAINER command is used to describe who maintains the Dockerfile file. Such as:

MAINTAINER   Joe Blocks <[email protected]>

Copy the code

MAINTAINER commands are not used often because such information is available in Git storage or elsewhere.

FROM

The FROM command is used to specify the base Docker image. If you start FROM the original Linux image, you can run the following command:

# Latest image FROM UbuntuCopy the code

CMD

The CMD command is used to specify the command to be executed to start the Docker container, which is a Docker image built based on the Dockerfile. Here are some examples of the CMD of the Dockerfile:

CMD echo Docker container started.

Copy the code

In this example, the line “Docker Container Started” is printed.

The next CMD example is to start a Java application:

CMD java -cp /myapp/myapp.jar com.jenkov.myapp.MainClass arg1 arg2 arg3

Copy the code

COPY

The COPY command copies one or more files from a host (the machine that builds a Docker image from a Dockerfile) to a Docker image. The contents that can be copied include files or directories. The following is an example:

COPY    /myapp/target/myapp.jar    /myapp/myapp.jar

Copy the code

This example copies the host’s /myapp/target/myapp.jar file to the Docker’s ongoing /myapp/myapp.jar file. The first argument is the host path (where from) and the second argument is the path of the Docker image (where to).

We can also copy a directory to the Docker image, for example:

COPY    /myapp/config/prod    /myapp/config

Copy the code

This example copies the /myapp/config/prod directory of the host to the /myapp/config directory of the Docker image.

We can also copy multiple files to a directory in the Docker image, for example:

COPY    /myapp/config/prod/conf1.cfg   /myapp/config/prod/conf2.cfg   /myapp/config/

Copy the code

This example is the host/myapp/config/prod/conf1 CFG file and/myapp/conig/prod/conf2 CFG file is copied to the Docker image/myapp/config/directory. Note that the destination directory must end with a/(slash) to work.

ADD

The ADD command works in the same way as the COPY command, with some minor differences:

  • The ADD command copies and extracts TAR files into a Docker image.

  • The ADD command can download files over HTTP and copy them to the Docker image.

Here are some examples:

ADD    myapp.tar    /myapp/

Copy the code

This example is to extract the specified TAR file into the Docker image /myapp/ directory.

Here’s another example:

ADD    http://jenkov.com/myapp.jar    /myapp/

Copy the code

ENV

The ENV command is to set environment variables in the Docker image, which can be used by CMD command to start the application in the Docker image. Here’s an example:

ENV    MY_VAR   123

Copy the code

In this example, the environment variable MY_VAR is set to the value 123.

RUN

RUN can execute command line commands in the Docker image. The execution time is during the construction of the Docker image, so the RUN command is executed only once. The RUN command can be used to install applications, extract files, or other command line functions in a Docker image. These operations need to be performed only once for subsequent use by the Docker image.

RUN apt-get install some-needed-app

Copy the code

ARG

The ARG command allows you to define a parameter that can be passed to the Docker through command arguments when building a Docker image from a Dockerfile file. Such as:

ARG tcpPort

Copy the code

When you run the docker build command to build a docker image using Dockerfile, you can specify the tcpPort parameter, for example:

docker build --build-arg tcpPort=8080 .

Copy the code

Note that tcpPort=8080 after –build-arg is set to 8080.

We can define multiple parameters with multiple ARG commands, for example:

ARG tcpPort
ARG useTls

Copy the code

When building a Docker image, you must provide values for all build parameters. Before version 1.13, an error will be reported if you do not provide a value. After version 1.13, an error will not be reported if you do not provide a value, but a warning will be displayed. Here’s an example:

docker build --build-arg tcpPort=8080 --build-arg useTls=true .

Copy the code

We can set default values for ARG, which will be used if no parameter values are specified when building a Docker image. Here’s an example:

ARG tcpPort=8080
ARG useTls=true

Copy the code

If tcpPort and useTls do not set parameters when generating Docker images, the default values 8080 and true are used.

ARG declared parameters are usually referenced elsewhere in the Dockerfile, such as:

ARG tcpPort=8080
ARG useTls=true

CMD start-my-server.sh -port ${tcpPort} -tls ${useTls}

Copy the code

Note: two references ${tcpPort} and ${useTls}, the reference name is tcpPort and useTls ARG declaration parameters.

docker build --build-arg tcpPort=8080

Copy the code

WORKDIR

The WORKDIR command specifies the working directory in the Docker image. The working directory will take effect for all commands following the WORKDIR directive. For example:

WORKDIR    /java/jdk/bin

Copy the code

EXPOSE

The EXPOSE command opens up the network ports in the Docker container to the public. For example, if the Docker container is running a Web server, that Web server may need to open port 80 for clients to link to it. Here’s an example:

EXPOSE   8080

Copy the code

We can also specify the communication protocols to enable the port, such as UDP and TCP. Here is an example of setting the allowed communication protocol:

EXPOSE   8080/tcp 9999/udp

Copy the code

If no protocol is specified, TCP is used by default.

VOLUME

The VOLUME command creates a directory in the Docker image that can be mounted to the Docker host. In other words, you can create directories in the Docker image, such as /data, that can later be mounted to the /container-data/container1 directory on the Docker host. Once successfully mounted, the container will start. Here is an example of defining a load directory in a Dockerfile using the VOLUME command:

VOLUME   /data

Copy the code

ENTRYPOINT

The ENTRYPOINT command provides the ENTRYPOINT for starting the Docker container from the Docker image. The ENTRYPOINT is the application or command executed when the Docker container is started. In this way, ENTRYPOINT works like CMD, except that with ENTRYPOINT, the Docker container closes when the application executing ENTRYPOINT completes. Thus, ENTRYPOINT makes the Docker image itself an executable command that can be started and then closed. Here is an example of ENTRYPOINT:

ENTRYPOINT java -cp /apps/myapp/myapp.jar com.jenkov.myapp.Main

Copy the code

This example will execute the Java application’s Main class com.jenkov.myapp.main when the container is started and the Docker container is closed when the application is closed.

HEALTHCHECK

The HEALTHCHECK command performs periodic health checks to monitor the health of applications running in the Docker container. If the command returns 0, Docker will consider the application and container normal. If the command returns 1, Docker will consider the application and container abnormal. The following is an example:

HEALTHCHECK java -cp /apps/myapp/healthcheck.jar com.jenkov.myapp.HealthCheck https://localhost/healthcheck

Copy the code

This example USES the Java application com. Jenkov. Myapp. HealthCheck command as a health check, we can use any meaningful health check command.

Health check interval

By default, Docker executes HEALTHCHECK every 30 seconds. If you want to change the interval, you can customize it by using the –interval parameter to specify the interval for the health check. Here is an example of setting the HEALTHCHECK interval to 60 seconds:

HEALTHCHECK --interval=60s java -cp /apps/myapp/healthcheck.jar com.jenkov.myapp.HealthCheck https://localhost/healthcheck

Copy the code

Health check start time

By default, Docker immediately checks the monitoring status of the Docker container. However, some applications may take some time to start up, so it only makes sense to do a health check after a certain amount of time has passed. We can use the –start-period parameter to set the start time of the health check. Here is an example of setting the health check to 5 minutes, giving containers and applications 300 seconds (5 minutes) to start before Docker starts the health check:

HEALTHCHECK --start-period=300s java -cp /apps/myapp/healthcheck.jar com.jenkov.myapp.HealthCheck https://localhost/healthcheck

Copy the code

Timeout period of health check

The health check is likely to time out, and If the HEALTCHECK command needs to complete beyond the given time limit, Docker will consider the health check timed out. You can use the –timeout parameter to set the timeout time. Here is an example of setting the timeout to 5 seconds:

HEALTHCHECK --timeout=5s java -cp /apps/myapp/healthcheck.jar com.jenkov.myapp.HealthCheck https://localhost/healthcheck

Copy the code

Note that Docker also considers the container unhealthy if the health check times out.

Number of health check repeats

If the HEALTHCHECK command fails to execute, the result may be 1 or the execution times out. Docker will retry the HEALTHCHECK command three times before determining that the container is unhealthy to check whether the Docker container returns to the health status. You can use –retries to set the number of retries. Here is an example of setting the retry count to 5:

HEALTHCHECK --retries=5 java -cp /apps/myapp/healthcheck.jar com.jenkov.myapp.HealthCheck https://localhost/healthcheck

Copy the code

Hello, I’m looking at the mountains. Swim in the code, play to enjoy life. If this article is helpful to you, please like, bookmark, follow. Welcome to follow the public account “Mountain Hut”, discover a different world.