Linux Dockerfile & harbor

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 <[email protected]>"



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通過從網上下載文本中所指定的依賴鏡像作爲基礎鏡像,然後再在其之上

加一層可寫層,開啓運行爲容器,然後將後續所給指令依次執行,然後再將其封裝成一個

用戶所定義的鏡像。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章