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

Docker Foundation (4)-- Dockerfile Writing

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

Share

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

There are two ways to create images in Docker:

1. Container-based mirroring:

docker commit …

2. Making mirror images based on Dockerfile;

docker build …

Dockerfile production steps:

1. Create a directory;

2. Create a capitalized file in the directory for docker build to identify;

3. Because the execution environment of Dockerfile is in the directory created, you can create a corresponding file for Dockerfile to use;

ex:

[root@master ~]# ls docimageDockerfile index.html [root@master ~]# cat docimage/DockerfileFROM busybox:latest LABEL maintainer="hevttc" ENV DOC_ROOT="/tmp/" \ VOLUME_ROOT="/data/mydata" \ ARGU="/usr/sbin/" COPY index.html /tmp/ VOLUME ${VOLUME_ROOT} RUN ifconfig -a && \ cat ${DOC_ROOT}index.html

Dockerfile format:

Dockerfile Format

The line beginning with "#" is a comment line (multiple lines, anywhere);

INSTRUCTION arguments

Instructions and corresponding parameters, it is recommended that instructions be capitalized, parameters be lowercase, default case can be;

Can be completed with one line of instructions, try not to use multiple lines of instructions;

The first executable instruction must be a FROM instruction;

Docker is to execute the contents of Dockerfile in sequence, pay attention to the writing order;

There are several important commands in Dockerfile:

1. FROM (specify base mirror)

FROM |: or

FROM @

: Specifies the name as the base image;

: label of base image, optional, default to latest when omitted;

2. LABEL (write creator information of current Dockerfile);

LABEL = = = …

3. COPY (copy host files to mirror);

COPY …or COPY ["",... "" ]

: The source file or directory to copy. Wildcards are supported.

: Target path, i.e. file system path of image being created; absolute path is recommended, otherwise, COPY specifies WORKDIR as its starting path;

Note: When there are white space characters in the path, the second format is usually used;

ADD (similar to COPY command, support TAR file and URL path);

ADD …or ADD ["",... "" ]

Note:

If URL does not end with "/", the specified file will be downloaded and created directly as; if URL ends with "/", the file specified by filename URL will be downloaded and saved as/;

If it is a tar file in compressed format on the local system, it will be expanded into a directory, which behaves like the "tar -x" command; however, tar files retrieved via URL will not be expanded automatically;

If there is more than one, or it uses wildcards indirectly or directly, it must be a directory path ending with "/"; if it does not end with "/", it is treated as a normal file and the contents of will be written directly to;

WORKDIR (used to specify the working directory for all RUN, CMD, ENTRYPOINT, COPY and ADD in Dockerfile;);

WORKDIR

WORKDIR can occur multiple times in a Dockerfile, and its path can be relative, but relative to the path specified by the previous WORKDIR directive;

In addition, WORKDIR can also call variables defined by ENV;

VOLUME (used to create a mount point directory in image, which can be used to mount volumes on Docker Host);

VOLUME or VOLUME ["mountpoint"]

Personally, it is not recommended to use: this is Docker daemon to create managed volumes, not binding mounts;

7. EXPOSE (open the port to be listened to for the container, and realize communication with the outside);

EXPOSE [/] [/] [/] …

Used to specify the transport layer protocol, which can be either TCP or UDP, and the default is TCP;

ENV (defines environment variables for mirroring, can be called by other instructions, build_time and run_time can be used);

ENV or ENV =...

Call format:

$variable_name or ${variable_name}

9. ARG (variable used in creating mirror is build_time, generally ARG is used to write version number and author);

ARG [=]

10.RUN (used to specify the program to run during Docker build);

RUN or

RUN ["","","",...] (json array)

CMD (similar to RUN command, executed at run_time);

CMD

CMD ["","","",...] or

CMD ["","",...] (Provides default parameters for ENTRYPOINT directive)

12. ENTRYPOINT (similar to CMD command)

ENTRYPOINT

ENTRYPOINT ["","","",...]

[root@master ~]# cat docimage1/entrypoint.sh#!/ bin/sh exec $@[root@master ~]# cat docimage1/DockerfileFROM nginx:1.14-alpine COPY entrypoint.sh ["/usr/local/nginx","-g","daemon off;"] ENTRYPOINT ["/bin/entrypoint.sh"][root@master ~]# ll docimage1/total usage 8-rw-r-r- 1 root 129 November 1 20:42 Dockerfile-rwxr-xr-x 1 root 19 November 1 20:16 entrypoint.sh

command execution diagram

Create a Dockerfile;

[root@www ~]# mkdir docimage[root@www ~]# vim docimage/Dockerfile[root@www ~]#[root@www ~]# vim docimage/test.txt[root@www ~]# cat docimage/DockerfileFROM nginx:1.14-alpine LABEL maintainer="liujingyu" ENV DOC_ROOT="/tmp" \ VOLUME_ROOT=/data/mydata COPY test.txt ${DOC_ROOT} VOLUME ${VOLUME_ROOT} RUN echo 123 > ${DOC_ROOT}/test.txt CMD cat ${DOC_ROOT}/test.txt[root@www ~]#[root@www ~]# cat docimage/test.txtTHIS is Docker TEST;

Creating Mirrors with Dockerfile:

[root@www ~]# docker build docimage/ -t ljymyy/nginx:v0.2Sending build context to Docker daemon 3.072kBStep 1/7 : FROM nginx:1.14-alpine ---> 77bae8d00654Step 2/7 : LABEL maintainer="liujingyu" ---> Using cache ---> a012ac4c6f51Step 3/7 : ENV DOC_ROOT="/tmp" VOLUME_ROOT=/data/mydata ---> Using cache ---> ee6ed363bfdeStep 4/7 : COPY test.txt ${DOC_ROOT} ---> Using cache ---> a416671fde2aStep 5/7 : VOLUME ${VOLUME_ROOT} ---> Using cache ---> 84bf38b22301Step 6/7 : RUN echo 123 > ${DOC_ROOT}/test.txt ---> Using cache ---> 756cd9857096Step 7/7 : CMD cat ${DOC_ROOT}/test.txt ---> Running in a2f99fcc5fbaRemoving intermediate container a2f99fcc5fba ---> dac468a636e0Successfully built dac468a636e0Successfully tagged ljymyy/nginx:v0.2[root@www ~]# docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEljymyy/nginx v0.2 dac468a636e0 2 minutes ago 17.7MB

Run this image:

[root@www ~]# docker container run --rm --name ngx1 ljymyy/nginx:v0.2123

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