Image display problems, attach proper way cloud Linked Notes: http://note.youdao.com/noteshare?id=fba6d2f53fd6447ba32c3b7accfeb89b&sub=B36B5904A0804AF5AD3194AAA196F8C3
Write Dockerfile
FROM
Instructions and MAINTAINER
instructions
Is the first line of the script FROM
command. By FROM
instruction, docker
the compiler can know which base image is performed to compile. All Dockerfile must to FROM
begin instruction. Second instruction MAINTAINER
, information used to identify the maintainer mirror.
RUN
instruction
Next is the RUN
command. This instruction is used to docker
run 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 ..."
. RUN
There is another instruction format:
RUN ["程序名", "参数1", "参数2"]
This format run the program, you can run exempt from /bin/sh
consumption. 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.
ENV
instruction
ENV
Instruction is used to specify the execution docker run
environment variables mirroring operation command, the automatic setting. These environment variables can docker run
command --evn
to modify the parameters.
COPY
Instructions and ADD
instructions
COPY
Instructions 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_config
copy the files to the build environment /linux-kernal/.config
. Dockerfile also provides a similar additional instruction: ADD
. Copy the files in terms of ADD
instructions and COPY
instruction format and the effect is exactly the same. The difference between these two commands mainly by two things:
ADD
The instructions may be copied from one container to the download URL of a file system;ADD
Copy the compressed instruction will unlock unpacked format file to the specified location, and theCOPY
instruction only copy operation.
CMD
instruction
This is the last instruction throughout Dockerfile script. When the installation has been completed Dockerfile all environments and configuration, through CMD
to indicate that the instruction docker run
command 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.
EXPOSE
instruction
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 run
if the command does not bring the port mapping parameter in response, docker
it does not take a port mapping.
ENTRYPOINT
instruction
ENTRYPOINT
And instructions described earlier CMD
as 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.
ENTRYPOINT
There are two instruction formats, CMD
instruction formats:
ENTRYPOINT ["程序名", "参数1", "参数2"]
ENTRYPOINT 命令 参数1 参数2
CMD ["程序名", "参数1", "参数2"]
CMD 命令 参数1 参数2
CMD 参数1 参数2
ENTRYPOINT
Inlet vessel is run the program. In other words, docker run
the specified command in the command will be provided as an argument to ENTRYPOINT
the specified program. Similarly, the above-mentioned CMD
two latter format instruction format also provides as a parameter to the ENTRYPOINT
specified program.
The default ENTRYPOINT
Shi /bin/sh -c
. You can be set according to actual needs. But if there is more than one in a Dockerfile in ENTRYPOINT
command, then only the last ENTRYPOINT
instruction 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 ENTRYPOINT
set: ENTRYPOINT mysql -u <用户名> -p <密码> -h <主机名>
. And when you run, you only need to specify the database name.
VOLUME
instruction
VOLUME ["路径"]
VOLUME
Creating instructions for one or more volumes within the container. But more often, in the implementation of docker run
the specified volume as well as local path to be created to map the time. Usage will learn about this in a later chapter.
USER
instruction
USER 用户名或用户ID
USER
Instructions for operating the vessel RUN
instructions or CMD
user'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 run
set command -u 用户名
parameters, it will cover USER
the user's instruction set.
WORKDIR
instruction
WORKDIR 路径
WORKDIR
Instructions for setting execution RUN
instructions, CMD
instruction and ENTRYPOINT
working directory when the instruction is executed. You may be set more than once in Dockerfile in WORKDIR
command after each set will use the new path.
ONBUILD
instruction
ONBUILD 指令
ONBUILD
Instructions for setting a number of instructions which, when present as a mirror image basis by other Dockerfile used FROM
when referring to instructions, these instructions are executed first before any other instruction execution.
Remark
Illustration ENTRYPOINT
and CMD
differentiated
- 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"]
andCMD ["/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 .