Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Dockerfile Best practices (1)

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

In the article "Docker deploys your first application," we have used Dockerfile to build the image, which complements the instructions that Dockerfile uses frequently.

Docker can generate mirrors by reading instructions in Dockerfile. Dockerfile is a text file, and all the user's instructions for the mirror operation can be written in the Dockerfile file, and finally use docker build to build the image.

In "Docker deploy your first application", we used the command "docker image build-t bulletinboard:1.0." the docker image build command builds the image by reading Dockerfile and the specified context, ending with a "." Point, which is the context in which the image is built.

The context is processed recursively. Therefore, all subdirectories in that context are included.

The construction of the image is done by the Docker daemon (Docker daemon), not by CLI. The first thing to do in the build process is to send the entire context (recursively) to the daemon. In most cases, it is best to start with an empty directory as the context and save the Dockerfile in that directory. Add only the files needed to generate the Dockerfile.

Note: never use the "/" root as the context, such as the following command, because it will transfer all files under the host "/" root to the daemon of Docker, which can be verified by executing the following command in the development environment.

# docker image build /

To build a configuration file or package into a mirror in the context of a build, use the COPY directive in Dockerfile. To improve build performance, you can exclude files and directories by adding .dockerboxes files to the context directory. Usually Dockerfile, located in the root directory of the context, use the-f flag in docker build to specify the docker file anywhere in the file system, and the-t flag to specify the repository where the image is built and the tag tag, for example:

# cat > / tmp/centos 6f5007fa547d

Step 3ax 4: RUN apk update & & apk add socat & & rm-r / var/cache/

-- > Running in b157222691fb

Fetch http://dl-cdn.alpinelinux.org/alpine/v3.2/main/x86_64/APKINDEX.tar.gz

V3.2.3-474-g10ee65f [http://dl-cdn.alpinelinux.org/alpine/v3.2/main]

OK: 5294 distinct packages available

(1) Installing ncurses-terminfo-base (5.9)

Installing ncurses-libs (5.9)

(3x4) Installing readline (6.3.008)

(4ax 4) Installing socat (1.7.3.0)

Executing busybox-1.23.2.trigger

OK: 7 MiB in 19 packages

Removing intermediate container b157222691fb

-- > 58c5258280f7

Step 4x4: CMD env | grep _ TCP= | (sed's pedigree. Passport Port\ ([0-9] *\) _ TCP=tcp:\ /\ /\ (. *\):\ (. *\) / socat-t 100000000 TCP4-LISTEN:\ 1Forkdepartment reuseaddr TCP4:\ 2:\ 3\ /'& echo wait) | sh

-- > Running in ca843dd16f02

Removing intermediate container ca843dd16f02

-- > 7bf06f4ab80b

Successfully built 7bf06f4ab80b

Successfully tagged demo/demo:v0.1

The second construction

# docker build-t demo/demo:v0.2.

Sending build context to Docker daemon 2.048kB

Step 1/4: FROM alpine:3.2

-- > 98f5f2d17bd1

Step 2/4: MAINTAINER firefly@demo.com

-- > Using cache

-- > 6f5007fa547d

Step 3ax 4: RUN apk update & & apk add socat & & rm-r / var/cache/

-- > Using cache

-- > 58c5258280f7

Step 4x4: CMD env | grep _ TCP= | (sed's pedigree. Passport Port\ ([0-9] *\) _ TCP=tcp:\ /\ /\ (. *\):\ (. *\) / socat-t 100000000 TCP4-LISTEN:\ 1Forkdepartment reuseaddr TCP4:\ 2:\ 3\ /'& echo wait) | sh

-- > Using cache

-- > 7bf06f4ab80b

Successfully built 7bf06f4ab80b

Successfully tagged demo/demo:v0.2

The build cache is only used for mirrors with local parent chains. This also means that these cache images are generated by previous builds, or that the entire chain of images is loaded in docker load. If you need to specify a cache from cache, you can use the-- mirror option. The image specified with-- cache from does not need to have a parent chain and can be pulled from other repositories.

After completing the build, you can consider pushing the images stored in the local warehouse to the remote warehouse (for example: Harobor)

BuildKit

Starting with version 18.09, Docker supports a new build tool, the buildkit,moby/buildkit project (https://github.com/moby/buildkit). Compared to existing implementation tools, BuildKit provides many features:

1. Detect and skip the execution of unused build phases

2. Parallel independent construction phase

3. Transfer only the files changed in the context incrementally during the build process

4. Detect and skip transferring unused files in context

5. Many new features are implemented in external Dockerfile.

6. Avoid side effects with the rest of API (intermediate images and containers)

7. Automatically prune and set the priority of the generation cache

To use BuildKit, you need to set the environment variable DOCKER_BUILDKIT=1 on CLI before invoking the docker build command.

To understand the experimental Dockerfile syntax available for BuildKit-based builds, refer to the BuildKit documentation (https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md).

Dockerfile instruction and syntax

Instructions in Dockerfile are not case sensitive. However, it is customary to capitalize to make it easier to distinguish them from parameters.

Docker runs the instructions in Dockerfile sequentially. The Dockerfile must begin with the "FROM" directive. Of course, the FROM directive can be preceded by comments and global parameters. The FROM directive specifies the parent image. The FROM can only be preceded by one or more ARG instructions that declare the parameters used in the FROM line of the Dockerfile.

Docker treats lines starting with # as comments.

1. FROM image: label

Specify the (base) image on which the new image is created, and a FROM instruction is required to create each image, for example:

FROM centos:latest

2. MAINTAINER name / email address

Maintainer information, such as:

MAINTAINER firefly@demo.com

3. New image directory of ADD source files

Copy the source file to the newly created image. The source file belongs to the same directory as Dockerfile, and the ADD instruction automatically decompresses the tar and tgz packages, for example:

ADD example.tgz / data

4. COPY source file destination directory

Copy the source text to the new image, and the source file should be in the same directory as Dockerfile, similar to ADD, for example:

COPY sources.list / etc/apt

5. ENV keyword value

Set variables or environment variables, for example:

ENV foo / var/www/html

The value of the variable foo above is / var/www/html

6. RUN command

Execute commands based on an existing image and submit to a new image, usually using RUN when installing the package, for example:

RUN yum-y install sysstat

7. WORKDIR directory

Specify the working directory. After setting the working directory through WORKDIR, the following commands in Dockerfile, such as RUN, CMD, ENTRYPOINT, ADD, COPY, will be executed in this directory. The default container path for subsequent login based on this image is WORKDIR.

8. EXPOSE port number

Specify the port on which the Docker container is opened when running from this image, for example:

EXPOSE 80

9. VOLUME mount point

When the Docker container runs from this image, it sets a mount point, for example:

VOLUME / data

10. CMD ["Program to run", "Parameter 1", "Parameter 2"]

For commands or scripts to be run when the container is started, Dockerfile can only have one CMD command. If there are multiple commands, the last one will be executed. In addition, if / bin/bash is used in the docker run command, CMD will be overwritten. For example:

CMD ["/ bin/bash", "/ root/start.sh"]

Sample demonstration

1. Create a Dockerfile

# mdkir demo# cd demo# cat > Dockerfile

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report