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文件开篇的第一个非注释行,用于

为映像文件构建过程指定基准镜像,后续的指令运行于此基准镜像所提供的运

行环境

实践中,基准镜像可以是任何可用镜像文件,默认情况下,docker build会在

docker主机上查找指定的镜像文件,在其不存在时,则会从Docker Hub Registry

上拉取所需的镜像文件,如果找不到指定的镜像文件,docker build会返回一个错误信息

Syntax

FROM <repository>[:<tag>] 或

FROM <resository>@<digest>

<reposotiry>:指定作为base image的名称;

<tag>:base image的标签,为可选项,省略时默认为latest;


MAINTANIER (depreacted)

用于让Dockerfile制作者提供本人的详细信息

Dockerfile并不限制MAINTAINER指令可在出现的位置,但推荐将其放置于FROM指令之后

Syntax

MAINTAINER <authtor's detail>

<author's detail>可是任何文本信息,但约定俗成地使用作者名称及邮件地址

MAINTAINER "winthcloud <windy@winthcloud.com>"



The LABEL instruction adds metadata to an image

Syntax: LABEL <key>=<value> <key>=<value> <key>=<value> ...

The LABEL instruction adds metadata to an image.

A LABEL is a key-value pair.

To include spaces within a LABEL value, use quotes and backslashes as

you would in command-line parsing.

An image can have more than one label.

You can specify multiple labels on a single line.


COPY

用于从Docker主机复制文件至创建的新映像文件

Syntax

COPY <src> ... <dest> 或

COPY ["<src>",... "<dest>"]

<src>:要复制的源文件或目录,支持使用通配符

<dest>:目标路径,即正在创建的image的文件系统路径;建议为<dest>

使用绝对路径,否则,COPY指定则以WORKDIR为其起始路径;

注意:在路径中有空白字符时,通常使用第二种格式

文件复制准则

<src>必须是build上下文中的路径,不能是其父目录中的文件

如果<src>是目录,则其内部文件或子目录会被递归复制,但<src>目录自身不会

被复制

如果指定了多个<src>,或在<src>中使用了通配符,则

<dest>必须是一个目录,且必须以/结尾

如果<dest>事先不存在,它将会被自动创建,这包括其父目录路径


ADD

ADD指令类似于COPY指令,ADD支持使用TAR文件和URL路径

Syntax

ADD <src> ... <dest> 或

ADD ["<src>",... "<dest>"]

操作准则

同COPY指令

如果<src>为URL且<dest>不以/结尾,则<src>指定的文件将被下载并直接被创建为

<dest>;如果<dest>以/结尾,则文件名URL指定的文件将被直接下载并保存为

<dest>/<filename>

如果<src>是一个本地系统上的压缩格式的tar文件,它将被展开为一个目录,其行为

类似于“tar -x”命令;然而,通过URL获取到的tar文件将不会自动展开;

如果<src>有多个,或其间接或直接使用了通配符,则<dest>必须是一个以/结尾的目

录路径;如果<dest>不以/结尾,则其被视作一个普通文件,<src>的内容将被直接写

入到<dest>;



WORKDIR

用于为Dockerfile中所有的RUN、CMD、ENTRYPOINT、COPY和ADD指定设定工作目录

Syntax

WORKDIR <dirpath>

在Dockerfile文件中,WORKDIR指令可出现多次,其路径也可以为相对路径,

不过,其是相对此前一个WORKDIR指令指定的路径。另外,WORKDIR也可调用由

ENV指定定义的变量

例如

WORKDIR /var/log

WORKDIR $STATEPATH



VOLUME

用于在image中创建一个挂载点目录,以挂载Docker host上的卷或其它容器上的卷

Syntax

VOLUME <mountpoint> 或 VOLUME ["<mountpoint>"]

如果挂载点目录路径下此前在文件存在,docker run命令会在卷挂载完成后将此

前的所有文件复制到新挂载的卷中,注意这里的VOLUME不支持绑定挂载



EXPOSE

用于为容器打开指定要监听的端口以实现与外部通信

Syntax

EXPOSE <port>[/<protocol>] [<port>[/<protocol>] ...]

<protocol>用于指定传输层协议,可为tcp或udp二者之一,默认为TCP协议

EXPOSE指令可一次指定多个端口,例如

EXPOSE 11211/udp 11211/tcp


ENV

用于为镜像定义所需的环境变量,并可被Dockerfile文件中位于其后的其它指令

(如ENV、ADD、COPY等)所调用

调用格式为$variable_name或${variable_name}

Syntax

ENV <key> <value> 或 ENV <key>=<value> ...

第一种格式中,<key>之后的所有内容均会被视作其<value>的组成部分,因此,一次只

能设置一个变量;

第二种格式可用一次设置多个变量,每个变量为一个"<key>=<value>"的键值对,如果

<value>中包含空格,可以以反斜线(\)进行转义,也可通过对<value>加引号进行标识;另

外,反斜线也可用于续行;

定义多个变量时,建议使用第二种方式,以便在同一层中完成所有功能



RUN

用于指定docker build过程中运行的程序,其可以是任何命令

Syntax

RUN <command> 或

RUN ["<executable>", "<param1>", "<param2>"]

第一种格式中,<command>通常是一个shell命令,且以“/bin/sh -c”来运行它,

这意味着此进程在容器中的PID不为1,不能接收Unix信号,因此,当使用

docker stop <container>命令停止容器时,此进程接收不到SIGTERM信号;

第二种语法格式中的参数是一个JSON格式的数组,其中<executable>为要运行的命令,

后面的<paramN>为传递给命令的选项或参数;然而,此种格式指定的命令不会以

“/bin/sh -c”来发起,因此常见的shell操作如变量替换以及通配符(?,*等)

替换将不会进行;不过,如果要运行的命令依赖于此shell特性的话,可以将其替换

为类似下面的格式。

RUN ["/bin/sh", "-c", "<executable>", "<param1>"]

注意:json数组中,要使用双引号



CMD

类似于RUN指令,CMD指令也可用于运行任何命令或应用程序,不过,二者的运行时间点不同

RUN指令运行于映像文件构建过程中,而CMD指令运行于基于Dockerfile构建出的新映像

文件启动一个容器时

CMD指令的首要目的在于为启动的容器指定默认要运行的程序,且其运行结束后,容器也

将终止;不过,CMD指定的命令其可以被docker run的命令行选项所覆盖

在Dockerfile中可以存在多个CMD指令,但仅最后一个会生效

Syntax

CMD <command> 或

CMD [“<executable>”, “<param1>”, “<param2>”] 或

CMD ["<param1>","<param2>"]

前两种语法格式的意义同RUN

第三种则用于为ENTRYPOINT指令提供默认参数



ENTRYPOINT

类似CMD指令的功能,用于为容器指定默认运行程序,从而使得容器像是一个

单独的可执行程序

与CMD不同的是,由ENTRYPOINT启动的程序不会被docker run命令行指定的

参数所覆盖,而且,这些命令行参数会被当作参数传递给ENTRYPOINT指定

指定的程序

不过,docker run命令的--entrypoint选项的参数可覆盖ENTRYPOINT指令指定的程序

Syntax

ENTRYPOINT <command>

ENTRYPOINT ["<executable>", "<param1>", "<param2>"]

docker run命令传入的命令参数会覆盖CMD指令的内容并且附加到

ENTRYPOINT命令最后做为其参数使用

Dockerfile文件中也可以存在多个ENTRYPOINT指令,但仅有最后一个会生效



USER

用于指定运行image时的或运行Dockerfile中任何RUN、CMD或ENTRYPOINT

指令指定的程序时的用户名或UID默认情况下,container的运行身份为root用户

Syntax

USER <UID>|<UserName>

需要注意的是,<UID>可以为任意数字,但实践中其必须为

/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.

Syntax: SHELL ["executable", "parameters"]

The SHELL instruction can appear multiple times.

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 <varname>=<value> flag.

If a user specifies a build argument that was not defined in the 

Dockerfile, the build outputs a warning.

Syntax: ARG <name>[=<default value>]

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中定义一个触发器

Dockerfile用于build映像文件,此映像文件亦可作为base image被另一个Dockerfile

用作FROM指令的参数,并以之构建新的映像文件

在后面的这个Dockerfile中的FROM指令在build过程中被执行时,将会“触发”创

建其base image的Dockerfile文件中的ONBUILD指令定义的触发器

Syntax

ONBUILD <INSTRUCTION>

尽管任何指令都可注册成为触发器指令,但ONBUILD不能自我嵌套,且不会触发FROM和

MAINTAINER指令

使用包含ONBUILD指令的Dockerfile构建的镜像应该使用特殊的标签,

例如ruby:2.0-onbuild

在ONBUILD指令中使用ADD或COPY指令应该格外小心,因为新构建过程的上下文在缺少指

定的源文件时会失败



使用harbor创建安装配置

到github.com下载harbor程序解压

官方文档里有向导教如何修改配置文件,将其requirement段修改完成后运行install.sh

里边有几个安装选项建议修改

1. 仓库保存位置  secretkey_path = /data

2. hostname

3. harbor_admin_password =

4. db_password = 

即可完成基本的harbor搭建


此程序是从网上会下载一些image启动成容器用来响应用户的操作如上传下载image等


用此可以配置docker不再默认使用https来连接registry,或者将harbor的https启用

#vi /etc/docker/daemon.json

{

    "insecure-registries": ["<ip>:80"] 

}


注意虽然这样也可以使用,但是每次pull, push都需要指定端口为80,

最好还是配置证书做成https



总结

1. docker是一个文本文件,里边的指令是docker daemon可以识别的指令,通过这些指令

docker daemon通过从网上下载文本中所指定的依赖镜像作为基础镜像,然后再在其之上

加一层可写层,开启运行为容器,然后将后续所给指令依次执行,然后再将其封装成一个

用户所定义的镜像。