Dockerfile written document

Image display problems, attach proper way cloud Linked Notes: http://note.youdao.com/noteshare?id=fba6d2f53fd6447ba32c3b7accfeb89b&sub=B36B5904A0804AF5AD3194AAA196F8C3

Write Dockerfile

FROMInstructions and MAINTAINERinstructions

Is the first line of the script FROMcommand. By FROMinstruction, dockerthe compiler can know which base image is performed to compile. All Dockerfile must to FROMbegin instruction. Second instruction MAINTAINER, information used to identify the maintainer mirror.

RUNinstruction

Next is the RUNcommand. This instruction is used to dockerrun the specified command in the build environment. Above this instruction will run in the build environment /bin/sh -c "apt-get update && apt-get -y install ...". RUNThere is another instruction format:

RUN ["程序名", "参数1", "参数2"]

This format run the program, you can run exempt from /bin/shconsumption. This format uses Json format program name and parameters required to form a string array, so if there are special characters quote parameters, need to be escaped.

ENVinstruction

ENVInstruction is used to specify the execution docker runenvironment variables mirroring operation command, the automatic setting. These environment variables can docker runcommand --evnto modify the parameters.

COPYInstructions and ADDinstructions

COPYInstructions for local (Dockerfile location) of the file or folder to the specified path compiler environment. The example above, boot2docker of Dockerfile want the same directory under the Dockerfile kernel_configcopy the files to the build environment /linux-kernal/.config. Dockerfile also provides a similar additional instruction: ADD. Copy the files in terms of ADDinstructions and COPYinstruction format and the effect is exactly the same. The difference between these two commands mainly by two things:

  1. ADDThe instructions may be copied from one container to the download URL of a file system;
  2. ADDCopy the compressed instruction will unlock unpacked format file to the specified location, and the COPYinstruction only copy operation.

CMDinstruction

This is the last instruction throughout Dockerfile script. When the installation has been completed Dockerfile all environments and configuration, through CMDto indicate that the instruction docker runcommand is a command to be executed run-time image. The example above, after the completion of all work, boot2docker compiler to compile the script will output the results to the next local environment.

Other instructions

Above, we learned some of the most commonly used commands by Dockerfile the script boot2docker. Next we learn the few remaining instructions.

EXPOSEinstruction

EXPOSE <端口> [<端口>...]Instructions for identifying, in image applications that will be listening on a port, the port mapping and hoping to host on the network interface. However, for safety, docker runif the command does not bring the port mapping parameter in response, dockerit does not take a port mapping.

ENTRYPOINTinstruction

ENTRYPOINTAnd instructions described earlier CMDas a mirror for indicating operating as a container, or the command to be executed last. These two commands there are similarities, there are differences. It can be configured with different results by using the two instructions.

ENTRYPOINTThere are two instruction formats, CMDinstruction formats:

ENTRYPOINT ["程序名", "参数1", "参数2"]
ENTRYPOINT 命令 参数1 参数2

CMD ["程序名", "参数1", "参数2"]
CMD 命令 参数1 参数2
CMD 参数1 参数2

ENTRYPOINTInlet vessel is run the program. In other words, docker runthe specified command in the command will be provided as an argument to ENTRYPOINTthe specified program. Similarly, the above-mentioned CMDtwo latter format instruction format also provides as a parameter to the ENTRYPOINTspecified program.

The default ENTRYPOINTShi /bin/sh -c. You can be set according to actual needs. But if there is more than one in a Dockerfile in ENTRYPOINTcommand, then only the last ENTRYPOINTinstruction is acting.

A common setting is set to command the necessary parameters ENTRYPOINT, the only other option is to provide run-time. For example: You have a MySQL client program running in the container, and the client needed host addresses, user name and password every time you do not want to enter, you can ENTRYPOINTset: ENTRYPOINT mysql -u <用户名> -p <密码> -h <主机名>. And when you run, you only need to specify the database name.

VOLUMEinstruction

VOLUME ["路径"]

VOLUMECreating instructions for one or more volumes within the container. But more often, in the implementation of docker runthe specified volume as well as local path to be created to map the time. Usage will learn about this in a later chapter.

USERinstruction

USER 用户名或用户ID

USERInstructions for operating the vessel RUNinstructions or CMDuser's instruction. For example, when building a nginx mirror, you want to run the final user is nginx nginx, you can in CMD ["nginx"]as user settings before nginx.

If the running docker runset command -u 用户名parameters, it will cover USERthe user's instruction set.

WORKDIRinstruction

WORKDIR 路径

WORKDIRInstructions for setting execution RUNinstructions, CMDinstruction and ENTRYPOINTworking directory when the instruction is executed. You may be set more than once in Dockerfile in WORKDIRcommand after each set will use the new path.

ONBUILDinstruction

ONBUILD 指令

ONBUILDInstructions for setting a number of instructions which, when present as a mirror image basis by other Dockerfile used FROMwhen referring to instructions, these instructions are executed first before any other instruction execution.

Remark

Illustration ENTRYPOINTand CMDdifferentiated

  • dockerfile only CMD ["cat","/etc/passwd"]start docker
docker run -itd --name docker_name docker_image 

After the above start docker, it will be executed directly cat /etc/passwd. And direct execution

docker run -itd --name docker_name docker_image cat /etc/passwd
  • dockerfile only ENTRYPOINT ['cat']start docker
docker run -itd --name docker_name docker_image  /etc/passwd

After the above start docker, it will be executed directly cat /etc/passwd.

  • dockerfile there ENTRYPOINT ["cat"]and CMD ["/etc/passwd"]start docker
docker run -itd --name docker_name docker_image 

After the above start docker, it will be executed directly cat /etc/passwd.
If, after the start command to add other parameters

docker run -itd --name docker_name docker_image  cat /etc/shadow

Actual performs: cat / etc / passwd cat / etc / shadow this is wrong.

Examples explain

Dockerfile

FROM hub.geovis.io/isphere/ubuntu:18.04_ali  ##基础镜像

RUN mkdir -p /opt/app/ \    ##容器中创建 /opt/app和/nfs/data两个文件夹
    mkdir -p /nfs/data/ \

WORKDIR /opt/app/    ## 切换到/opt/app下,一下命令会在/opt/app下执行

COPY requirements.txt /opt/app   ## 将本地的requirements.txt文档拷贝到容器的/opt/app下


RUN \
    pip3 install -r requirements.txt   ## 执行命令,安装依赖包

COPY . /opt/app/  ## 将本地其他相关文件拷贝到容器/opt/app下

EXPOSE 5000  ## 容器申请端口5000

ENTRYPOINT ["python3"]

CMD ["-m", "swagger_server"]   ##docker run时,会直接执行 python3 -m swagger_server

Local directory structure

Package command

Executed in the directory Dockerfile

docker build -t dockerimage:tag  .

Guess you like

Origin www.cnblogs.com/zhangjxblog/p/12167839.html