Linux Dockerfile & harbor
时间: 20181113
目录
为何要使用Dockerfile文件
Dockerfile
dockerfile 制作原理
About Dockerfile
Dockerfile Format
Dockerfile Instructions(FROM,LABEL,COPY,ADD,WORKDIR,VOLUME,EXPOSE,ENV,CMD
RUN,ENTRYPOINT,USER,HEALTHCHECK,SHELL,ARG,ONBUILD)
harbor(私有docker仓库)
总结
为何要使用Dockerfile文件
官方所提供的镜像如何自定义其配置?
容器的配置文件
1. 在运行时使用docker run
2. 将原镜像运行成container然后配置完成使用commit生成自定义镜像
3. 在运行时为其传递参数环境变量 docker run -e
4. 将配置文件放置于存储卷中,运行时添加此存储卷
上述几种虽然可以提供配置文件,但是操作起来相当繁琐
方便创建image时直接提供相应的配置,用来为image提供自定义的配置文件
自定义image所
Dockerfile
FROM baseImage
所有的文件必须在工作目录中
一条指令加一个镜像层
所指定的指令是baseImage所拥有的指令
dockerfile 制作原理
开启一个所指定的baseImage, 根据dockerfile所给定的指令 制作imagecontainer
About Dockerfile
Dockerfile is nothing but the source code for building Docker images
Docker can build images automatically by reading the instructions
from a Dockerfile
A Dockerfile is a text document that contains all the commands a
user could call on the command line to assemble an image
Using docker build users can create an automated build that executes
several command-line instructions in succession
Dockerfile Format
Format
# Comment
INSTRUCTION arguments
The first instruction must be 'FROM' in order to specify the Base
Image from which you are building.
Docker runs instructions in a Dockerfile in order
The instruction is not case-sensitive(字符大小写).
However, convention is for them to be UPPERCASE to distinguish
them from arguments more easily
Environment replacement
Environment variables (declared with the ENV statement) can also be
used in certain instructions as variables to be interpreted by the
Dockerfile
Environment variables are notated in the Dockerfile either
with $variable_name or ${variable_name}
The ${variable_name} syntax also supports a few of the
standard bash modifiers
${variable:-word} indicates that if variable is set then the result
will be that value. If variable is not set then word will be the
result.
${variable:+word} indicates that if variable is set then word will be
the result, otherwise the result is the empty string.(用的较少)
.dockerignore file
Before the docker CLI sends the context to the docker daemon, it looks
for a file named .dockerignore in the root directory of the context
If this file exists, the CLI modifies the context to exclude files and
directories that match patterns in it
The CLI interprets the .dockerignore file as a newline-separated list of
patterns similar to the file globs of Unix shells
此文件里所写的文件列表不会被复制到所要构建的镜像中
Dockerfile Instructions
FROM
FROM指令是最重的一个且必须为Dockerfile文件开篇的第一个非注释行,用于
为映像文件构建过程指定基准镜像,后续的指令运行于此基准镜像所提供的运
行环境
Dockerfile文件中也可以存在多个ENTRYPOINT指令,但仅有最后一个会生效
USER
用于指定运行image时的或运行Dockerfile中任何RUN、CMD或ENTRYPOINT
指令指定的程序时的用户名或UID默认情况下,container的运行身份为root用户
Syntax
USER |
需要注意的是,可以为任意数字,但实践中其必须为
/etc/passwd中某用户的有效UID,否则,docker run命令将运行失败
HEALTHCHECK
The HEALTHCHECK instruction tells Docker how to test a container
to check that it is still working.
This can detect cases such as a web server that is stuck in an
infinite loop and unable to handle new connections, even though the
server process is still running.
The HEALTHCHECK instruction has two forms:
HEALTHCHECK [OPTIONS] CMD command (check container health by
running a command inside the container)
HEALTHCHECK NONE (disable any healthcheck inherited from the
base image)
The options that can appear before CMD are:
--interval=DURATION (default: 30s)
--timeout=DURATION (default: 30s)
--start-period=DURATION (default: 0s)
--retries=N (default: 3)
The command’s exit status indicates the health status of the container.
The possible values are:
0: success - the container is healthy and ready for use
1: unhealthy - the container is not working correctly
2: reserved - do not use this exit code
For example
HEALTHCHECK --interval=5m --timeout=3s \
CMD curl -f http://localhost/ || exit 1
SHELL
The SHELL instruction allows the default shell used for the shell
form of commandsto be overridden.
The default shell on Linux is ["/bin/sh", "-c"], and on Windows is
["cmd", "/S","/C"].
The SHELL instruction must be written in JSON form in a Dockerfile.
Each SHELL instruction overrides all previous SHELL instructions,
and affects all subsequent instructions.
ARG
The ARG instruction defines a variable that users can pass at
build-time to the builder with the docker build command using the
--build-arg = flag.
If a user specifies a build argument that was not defined in the
Dockerfile, the build outputs a warning.
Syntax: ARG [=]
A Dockerfile may include one or more ARG instructions.
An ARG instruction can optionally include a default value:
ARG version=1.14
ARG user=windy
ONBUILD
用于在Dockerfile中定义一个触发器