#
七、Dockerfile
Docker 可以通過 Dockerfile 的內(nèi)容來自動(dòng)構(gòu)建鏡像。Dockerfile 是一個(gè)包含創(chuàng)建鏡像所有命令的文本文件,通過docker build命令可以根據(jù) Dockerfile 的內(nèi)容構(gòu)建鏡像,在介紹如何構(gòu)建之前先介紹下 Dockerfile 的基本語法結(jié)構(gòu)。
Dockerfile 有以下指令選項(xiàng):
- FROM
- MAINTAINER
- RUN
- CMD
- EXPOSE
- ENV
- ADD
- COPY
- ENTRYPOINT
- VOLUME
- USER
- WORKDIR
- ONBUILD
7.1 FROM
用法:
或者
- FROM指定構(gòu)建鏡像的基礎(chǔ)源鏡像,如果本地沒有指定的鏡像,則會(huì)自動(dòng)從 Docker 的公共庫 pull 鏡像下來。
- FROM必須是 Dockerfile 中非注釋行的第一個(gè)指令,即一個(gè) Dockerfile 從FROM語句開始。
- FROM可以在一個(gè) Dockerfile 中出現(xiàn)多次,如果有需求在一個(gè) Dockerfile 中創(chuàng)建多個(gè)鏡像。
- 如果FROM語句沒有指定鏡像標(biāo)簽,則默認(rèn)使用latest標(biāo)簽。
7.2 MAINTAINER
用法:
指定創(chuàng)建鏡像的用戶
RUN 有兩種使用方式
每條RUN指令將在當(dāng)前鏡像基礎(chǔ)上執(zhí)行指定命令,并提交為新的鏡像,后續(xù)的RUN都在之前RUN提交后的鏡像為基礎(chǔ),鏡像是分層的,可以通過一個(gè)鏡像的任何一個(gè)歷史提交點(diǎn)來創(chuàng)建,類似源碼的版本控制。
exec 方式會(huì)被解析為一個(gè) JSON 數(shù)組,所以必須使用雙引號(hào)而不是單引號(hào)。exec 方式不會(huì)調(diào)用一個(gè)命令 shell,所以也就不會(huì)繼承相應(yīng)的變量,如:
這種方式是不會(huì)達(dá)到輸出 HOME 變量的,正確的方式應(yīng)該是這樣的
RUN [ "sh", "-c", "echo", "$HOME" ]
RUN產(chǎn)生的緩存在下一次構(gòu)建的時(shí)候是不會(huì)失效的,會(huì)被重用,可以使用--no-cache選項(xiàng),即docker build --no-cache,如此便不會(huì)緩存。
7.3 CMD
CMD有三種使用方式:
- CMD
- CMD
- CMD command param1 param2 (shell form)
CMD指定在 Dockerfile 中只能使用一次,如果有多個(gè),則只有最后一個(gè)會(huì)生效。
CMD的目的是為了在啟動(dòng)容器時(shí)提供一個(gè)默認(rèn)的命令執(zhí)行選項(xiàng)。如果用戶啟動(dòng)容器時(shí)指定了運(yùn)行的命令,則會(huì)覆蓋掉CMD指定的命令。
CMD會(huì)在啟動(dòng)容器的時(shí)候執(zhí)行,build 時(shí)不執(zhí)行,而RUN只是在構(gòu)建鏡像的時(shí)候執(zhí)行,后續(xù)鏡像構(gòu)建完成之后,啟動(dòng)容器就與RUN無關(guān)了,這個(gè)初學(xué)者容易弄混這個(gè)概念,這里簡(jiǎn)單注解一下。
7.4 EXPOSE
EXPOSE <port> [<port>...]
告訴 Docker 服務(wù)端容器對(duì)外映射的本地端口,需要在 docker run 的時(shí)候使用-p或者-P選項(xiàng)生效。
7.5 ENV
ENV <key> <value> # 只能設(shè)置一個(gè)變量
ENV <key>=<value> ... # 允許一次設(shè)置多個(gè)變量
指定一個(gè)環(huán)節(jié)變量,會(huì)被后續(xù)RUN指令使用,并在容器運(yùn)行時(shí)保留。
例子:
ENV myName="John Doe" myDog=Rex\ The\ Dog \
myCat=fluffy
等同于
ENV myName John Doe
ENV myDog Rex The Dog
ENV myCat fluffy
7.6 ADD
ADD復(fù)制本地主機(jī)文件、目錄或者遠(yuǎn)程文件 URLS 從 并且添加到容器指定路徑中 。
支持通過 GO 的正則模糊匹配,具體規(guī)則可參見 Go filepath.Match
ADD hom* /mydir/ # adds all files starting with "hom"
ADD hom?.txt /mydir/ # ? is replaced with any single character
- 路徑必須是絕對(duì)路徑,如果 不存在,會(huì)自動(dòng)創(chuàng)建對(duì)應(yīng)目錄
- 路徑必須是 Dockerfile 所在路徑的相對(duì)路徑
- 如果是一個(gè)目錄,只會(huì)復(fù)制目錄下的內(nèi)容,而目錄本身則不會(huì)被復(fù)制
7.7 COPY
COPY復(fù)制新文件或者目錄從 并且添加到容器指定路徑中 。用法同ADD,唯一的不同是不能指定遠(yuǎn)程文件 URLS。
7.8 ENTRYPOINT
- ENTRYPOINT
- ENTRYPOINT command param1 param2 (shell form)
配置容器啟動(dòng)后執(zhí)行的命令,并且不可被 docker run 提供的參數(shù)覆蓋,而CMD是可以被覆蓋的。如果需要覆蓋,則可以使用docker run --entrypoint選項(xiàng)。
每個(gè) Dockerfile 中只能有一個(gè)ENTRYPOINT,當(dāng)指定多個(gè)時(shí),只有最后一個(gè)生效。
Exec form ENTRYPOINT 例子
通過ENTRYPOINT使用 exec form 方式設(shè)置穩(wěn)定的默認(rèn)命令和選項(xiàng),而使用CMD添加默認(rèn)之外經(jīng)常被改動(dòng)的選項(xiàng)。
FROM ubuntu
ENTRYPOINT ["top", "-b"]
CMD ["-c"]
通過 Dockerfile 使用ENTRYPOINT展示前臺(tái)運(yùn)行 Apache 服務(wù)
FROM debian:stable
RUN apt-get update && apt-get install -y --force-yes apache2
EXPOSE 80 443
VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"]
ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
Shell form ENTRYPOINT 例子
這種方式會(huì)在/bin/sh -c中執(zhí)行,會(huì)忽略任何CMD或者docker run命令行選項(xiàng),為了確保docker stop能夠停止長(zhǎng)時(shí)間運(yùn)行ENTRYPOINT的容器,確保執(zhí)行的時(shí)候使用exec選項(xiàng)。
FROM ubuntu
ENTRYPOINT exec top -b
如果在ENTRYPOINT忘記使用exec選項(xiàng),則可以使用CMD補(bǔ)上:
FROM ubuntu
ENTRYPOINT top -b
CMD --ignored-param1 # --ignored-param2 ... --ignored-param3 ... 依此類推
7.9 VOLUME
創(chuàng)建一個(gè)可以從本地主機(jī)或其他容器掛載的掛載點(diǎn),后續(xù)具體介紹。
7.10 USER
指定運(yùn)行容器時(shí)的用戶名或 UID,后續(xù)的RUN、CMD、ENTRYPOINT也會(huì)使用指定用戶。
7.11 WORKDIR
為后續(xù)的RUN、CMD、ENTRYPOINT指令配置工作目錄。可以使用多個(gè)WORKDIR指令,后續(xù)命令如果參數(shù)是相對(duì)路徑,則會(huì)基于之前命令指定的路徑。
WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd
最終路徑是/a/b/c。
WORKDIR指令可以在ENV設(shè)置變量之后調(diào)用環(huán)境變量:
ENV DIRPATH /path
WORKDIR $DIRPATH/$DIRNAME
最終路徑則為 /path/$DIRNAME。
7.12 ONBUILD
配置當(dāng)所創(chuàng)建的鏡像作為其它新創(chuàng)建鏡像的基礎(chǔ)鏡像時(shí),所執(zhí)行的操作指令。
例如,Dockerfile 使用如下的內(nèi)容創(chuàng)建了鏡像 image-A:
[...]
ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src
[...]
如果基于 image-A 創(chuàng)建新的鏡像時(shí),新的 Dockerfile 中使用 FROM image-A 指定基礎(chǔ)鏡像時(shí),會(huì)自動(dòng)執(zhí)行 ONBUILD 指令內(nèi)容,等價(jià)于在后面添加了兩條指令。
# Automatically run the following
ADD . /app/src
RUN /usr/local/bin/python-build --dir /app/src
使用ONBUILD指令的鏡像,推薦在標(biāo)簽中注明,例如 ruby:1.9-onbuild。
7.13 Dockerfile Examples
# Nginx
#
# VERSION 0.0.1
FROM ubuntu
MAINTAINER Victor Vieux <victor@docker.com>
RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server
# Firefox over VNC
#
# VERSION 0.3
FROM ubuntu
# Install vnc, xvfb in order to create a 'fake' display and firefox
RUN apt-get update && apt-get install -y x11vnc xvfb firefox
RUN mkdir ~/.vnc
# Setup a password
RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
# Autostart firefox (might not be the best way, but it does the trick)
RUN bash -c 'echo "firefox" >> /.bashrc'
EXPOSE 5900
CMD ["x11vnc", "-forever", "-usepw", "-create"]
# Multiple images example
#
# VERSION 0.1
FROM ubuntu
RUN echo foo > bar
# Will output something like ===> 907ad6c2736f
FROM ubuntu
RUN echo moo > oink
# Will output something like ===> 695d7793cbe4
# You?ll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with
# /oink.
7.14 docker build
$ docker build --help
Usage: docker build [OPTIONS] PATH | URL | -
Build a new image from the source code at PATH
--force-rm=false Always remove intermediate containers, even after unsuccessful builds # 移除過渡容器,即使構(gòu)建失敗
--no-cache=false Do not use cache when building the image # 不實(shí)用 cache
-q, --quiet=false Suppress the verbose output generated by the containers
--rm=true Remove intermediate containers after a successful build # 構(gòu)建成功后移除過渡層容器
-t, --tag="" Repository name (and optionally a tag) to be applied to the resulting image in case of success
參考文檔:Dockerfile Reference
7.15 dockerfile 最佳實(shí)踐
為了在docker build過程中更快上傳和更加高效,應(yīng)該使用一個(gè).dockerignore文件用來排除構(gòu)建鏡像時(shí)不需要的文件或目錄。例如,除非.git在構(gòu)建過程中需要用到,否則你應(yīng)該將它添加到.dockerignore文件中,這樣可以節(jié)省很多時(shí)間。
為了降低復(fù)雜性、依賴性、文件大小以及構(gòu)建時(shí)間,應(yīng)該避免安裝額外的或不必要的包。例如,不需要在一個(gè)數(shù)據(jù)庫鏡像中安裝一個(gè)文本編輯器。
在大多數(shù)情況下,一個(gè)容器應(yīng)該只單獨(dú)跑一個(gè)程序。解耦應(yīng)用到多個(gè)容器使其更容易橫向擴(kuò)展和重用。如果一個(gè)服務(wù)依賴另外一個(gè)服務(wù),可以參考 Linking Containers Together。
我們知道每執(zhí)行一個(gè)指令,都會(huì)有一次鏡像的提交,鏡像是分層的結(jié)構(gòu),對(duì)于Dockerfile,應(yīng)該找到可讀性和最小化層之間的平衡。
如果可能,通過字母順序來排序,這樣可以避免安裝包的重復(fù)并且更容易更新列表,另外可讀性也會(huì)更強(qiáng),添加一個(gè)空行使用\換行:
RUN apt-get update && apt-get install -y \
bzr \
cvs \
git \
mercurial \
subversion
鏡像構(gòu)建過程中會(huì)按照Dockerfile的順序依次執(zhí)行,每執(zhí)行一次指令 Docker 會(huì)尋找是否有存在的鏡像緩存可復(fù)用,如果沒有則創(chuàng)建新的鏡像。如果不想使用緩存,則可以在docker build時(shí)添加--no-cache=true選項(xiàng)。
從基礎(chǔ)鏡像開始就已經(jīng)在緩存中了,下一個(gè)指令會(huì)對(duì)比所有的子鏡像尋找是否執(zhí)行相同的指令,如果沒有則緩存失效。在大多數(shù)情況下只對(duì)比Dockerfile指令和子鏡像就足夠了。ADD和COPY指令除外,執(zhí)行ADD和COPY時(shí)存放到鏡像的文件也是需要檢查的,完成一個(gè)文件的校驗(yàn)之后再利用這個(gè)校驗(yàn)在緩存中查找,如果檢測(cè)的文件改變則緩存失效。RUN apt-get -y update命令只檢查命令是否匹配,如果匹配就不會(huì)再執(zhí)行更新了。
為了有效地利用緩存,你需要保持你的 Dockerfile 一致,并且盡量在末尾修改。
Dockerfile 指令
- FROM: 只要可能就使用官方鏡像庫作為基礎(chǔ)鏡像
- RUN: 為保持可讀性、方便理解、可維護(hù)性,把長(zhǎng)或者復(fù)雜的RUN語句使用\分隔符分成多行
- 不建議RUN apt-get update獨(dú)立成行,否則如果后續(xù)包有更新,那么也不會(huì)再執(zhí)行更新
- 避免使用RUN apt-get upgrade或者dist-upgrade,很多必要的包在一個(gè)非privileged權(quán)限的容器里是無法升級(jí)的。如果知道某個(gè)包更新,使用apt-get install -y xxx
- 標(biāo)準(zhǔn)寫法
- RUN apt-get update && apt-get install -y package-bar package-foo
例子:
RUN apt-get update && apt-get install -y \
aufs-tools \
automake \
btrfs-tools \
build-essential \
curl \
dpkg-sig \
git \
iptables \
libapparmor-dev \
libcap-dev \
libsqlite3-dev \
lxc=1.0* \
mercurial \
parallel \
reprepro \
ruby1.9.1 \
ruby1.9.1-dev \
s3cmd=1.1.0*
- CMD: 推薦使用CMD [“executable”, “param1”, “param2”…]這種格式,CMD [“param”, “param”]則配合ENTRYPOINT使用
- EXPOSE: Dockerfile 指定要公開的端口,使用docker run時(shí)指定映射到宿主機(jī)的端口即可
- ENV: 為了使新的軟件更容易運(yùn)行,可以使用ENV更新PATH變量。如ENV PATH /usr/local/nginx/bin:$PATH確保CMD ["nginx"]即可運(yùn)行
ENV也可以這樣定義變量:
ENV PG_MAJOR 9.3
ENV PG_VERSION 9.3.4
RUN curl -SL http://example.com/postgres-$PG_VERSION.tar.xz | tar -xJC /usr/src/postgress && …
ENV PATH /usr/local/postgres-$PG_MAJOR/bin:$PATH
- ADDorCOPY:ADD比COPY多一些特性「tar 文件自動(dòng)解包和支持遠(yuǎn)程 URL」,不推薦添加遠(yuǎn)程 URL
如不推薦這種方式:
ADD http://example.com/big.tar.xz /usr/src/things/
RUN tar -xJf /usr/src/things/big.tar.xz -C /usr/src/things
RUN make -C /usr/src/things all
推薦使用 curl 或者 wget 替換,使用如下方式:
RUN mkdir -p /usr/src/things \
&& curl -SL http://example.com/big.tar.gz \
| tar -xJC /usr/src/things \
&& make -C /usr/src/things all
如果不需要添加 tar 文件,推薦使用COPY。
參考文檔:
六、Docker 網(wǎng)絡(luò)配置
圖: Docker - container and lightweight virtualization
Dokcer 通過使用 Linux 橋接提供容器之間的通信,docker0 橋接接口的目的就是方便 Docker 管理。當(dāng) Docker daemon 啟動(dòng)時(shí)需要做以下操作:
- creates the docker0 bridge if not present
- # 如果 docker0 不存在則創(chuàng)建
- searches for an IP address range which doesn’t overlap with an existing route
- # 搜索一個(gè)與當(dāng)前路由不沖突的 ip 段
- picks an IP in the selected range
- assigns this IP to the docker0 bridge
6.1 Docker 四種網(wǎng)絡(luò)模式
四種網(wǎng)絡(luò)模式摘自 Docker 網(wǎng)絡(luò)詳解及 pipework 源碼解讀與實(shí)踐
docker run 創(chuàng)建 Docker 容器時(shí),可以用 --net 選項(xiàng)指定容器的網(wǎng)絡(luò)模式,Docker 有以下 4 種網(wǎng)絡(luò)模式:
- host 模式,使用 --net=host 指定。
- container 模式,使用 --net=container:NAMEorID 指定。
- none 模式,使用 --net=none 指定。
- bridge 模式,使用 --net=bridge 指定,默認(rèn)設(shè)置。
host 模式
如果啟動(dòng)容器的時(shí)候使用 host 模式,那么這個(gè)容器將不會(huì)獲得一個(gè)獨(dú)立的 Network Namespace,而是和宿主機(jī)共用一個(gè) Network Namespace。容器將不會(huì)虛擬出自己的網(wǎng)卡,配置自己的 IP 等,而是使用宿主機(jī)的 IP 和端口。
例如,我們?cè)?10.10.101.105/24 的機(jī)器上用 host 模式啟動(dòng)一個(gè)含有 web 應(yīng)用的 Docker 容器,監(jiān)聽 tcp 80 端口。當(dāng)我們?cè)谌萜髦袌?zhí)行任何類似 ifconfig 命令查看網(wǎng)絡(luò)環(huán)境時(shí),看到的都是宿主機(jī)上的信息。而外界訪問容器中的應(yīng)用,則直接使用 10.10.101.105:80 即可,不用任何 NAT 轉(zhuǎn)換,就如直接跑在宿主機(jī)中一樣。但是,容器的其他方面,如文件系統(tǒng)、進(jìn)程列表等還是和宿主機(jī)隔離的。
container 模式
這個(gè)模式指定新創(chuàng)建的容器和已經(jīng)存在的一個(gè)容器共享一個(gè) Network Namespace,而不是和宿主機(jī)共享。新創(chuàng)建的容器不會(huì)創(chuàng)建自己的網(wǎng)卡,配置自己的 IP,而是和一個(gè)指定的容器共享 IP、端口范圍等。同樣,兩個(gè)容器除了網(wǎng)絡(luò)方面,其他的如文件系統(tǒng)、進(jìn)程列表等還是隔離的。兩個(gè)容器的進(jìn)程可以通過 lo 網(wǎng)卡設(shè)備通信。
none模式
這個(gè)模式和前兩個(gè)不同。在這種模式下,Docker 容器擁有自己的 Network Namespace,但是,并不為 Docker容器進(jìn)行任何網(wǎng)絡(luò)配置。也就是說,這個(gè) Docker 容器沒有網(wǎng)卡、IP、路由等信息。需要我們自己為 Docker 容器添加網(wǎng)卡、配置 IP 等。
bridge模式
圖:The Container World | Part 2 Networking
bridge 模式是 Docker 默認(rèn)的網(wǎng)絡(luò)設(shè)置,此模式會(huì)為每一個(gè)容器分配 Network Namespace、設(shè)置 IP 等,并將一個(gè)主機(jī)上的 Docker 容器連接到一個(gè)虛擬網(wǎng)橋上。當(dāng) Docker server 啟動(dòng)時(shí),會(huì)在主機(jī)上創(chuàng)建一個(gè)名為 docker0 的虛擬網(wǎng)橋,此主機(jī)上啟動(dòng)的 Docker 容器會(huì)連接到這個(gè)虛擬網(wǎng)橋上。虛擬網(wǎng)橋的工作方式和物理交換機(jī)類似,這樣主機(jī)上的所有容器就通過交換機(jī)連在了一個(gè)二層網(wǎng)絡(luò)中。接下來就要為容器分配 IP 了,Docker 會(huì)從 RFC1918 所定義的私有 IP 網(wǎng)段中,選擇一個(gè)和宿主機(jī)不同的IP地址和子網(wǎng)分配給 docker0,連接到 docker0 的容器就從這個(gè)子網(wǎng)中選擇一個(gè)未占用的 IP 使用。如一般 Docker 會(huì)使用 172.17.0.0/16 這個(gè)網(wǎng)段,并將 172.17.42.1/16 分配給 docker0 網(wǎng)橋(在主機(jī)上使用 ifconfig 命令是可以看到 docker0 的,可以認(rèn)為它是網(wǎng)橋的管理接口,在宿主機(jī)上作為一塊虛擬網(wǎng)卡使用)
6.2 列出當(dāng)前主機(jī)網(wǎng)橋
$ sudo brctl show # brctl 工具依賴 bridge-utils 軟件包 bridge name bridge id STP enabled interfaces
docker0 8000.000000000000 no
6.3 查看當(dāng)前 docker0 ip
$ sudo ifconfig docker0
docker0 Link encap:Ethernet HWaddr xx:xx:xx:xx:xx:xx
inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0
在容器運(yùn)行時(shí),每個(gè)容器都會(huì)分配一個(gè)特定的虛擬機(jī)口并橋接到 docker0。每個(gè)容器都會(huì)配置同 docker0 ip 相同網(wǎng)段的專用 ip 地址,docker0 的 IP 地址被用于所有容器的默認(rèn)網(wǎng)關(guān)。
6.4 運(yùn)行一個(gè)容器
$ sudo docker run -t -i -d ubuntu /bin/bash
52f811c5d3d69edddefc75aff5a4525fc8ba8bcfa1818132f9dc7d4f7c7e78b4 $ sudo brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.fef213db5a66 no vethQCDY1N
以上, docker0 扮演著 52f811c5d3d6 container 這個(gè)容器的虛擬接口 vethQCDY1N interface 橋接的角色。
使用特定范圍的 IP
Docker 會(huì)嘗試尋找沒有被主機(jī)使用的 ip 段,盡管它適用于大多數(shù)情況下,但是它不是萬能的,有時(shí)候我們還是需要對(duì) ip 進(jìn)一步規(guī)劃。Docker 允許你管理 docker0 橋接或者通過-b選項(xiàng)自定義橋接網(wǎng)卡,需要安裝bridge-utils軟件包。
基本步驟如下:
- ensure Docker is stopped
- create your own bridge (bridge0 for example)
- assign a specific IP to this bridge
- start Docker with the -b=bridge0 parameter
# Stopping Docker and removing docker0 $ sudo service docker stop $ sudo ip link set dev docker0 down $ sudo brctl delbr docker0 # Create our own bridge $ sudo brctl addbr bridge0 $ sudo ip addr add 192.168.5.1/24 dev bridge0 $ sudo ip link set dev bridge0 up # Confirming that our bridge is up and running $ ip addr show bridge0
4: bridge0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state UP group default
link/ether 66:38:d0:0d:76:18 brd ff:ff:ff:ff:ff:ff
inet 192.168.5.1/24 scope global bridge0
valid_lft forever preferred_lft forever # Tell Docker about it and restart (on Ubuntu) $ echo 'DOCKER_OPTS="-b=bridge0"' >> /etc/default/docker $ sudo service docker start
參考文檔: Network Configuration
6.5 不同主機(jī)間容器通信
不同容器之間的通信可以借助于 pipework 這個(gè)工具:
$ git clone https://github.com/jpetazzo/pipework.git
$ sudo cp -rp pipework/pipework /usr/local/bin/
安裝相應(yīng)依賴軟件
$ sudo apt-get install iputils-arping bridge-utils -y
橋接網(wǎng)絡(luò)
橋接網(wǎng)絡(luò)可以參考 日常問題處理 Tips 關(guān)于橋接的配置說明,這里不再贅述。
# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c291412cd no eth0
docker0 8000.56847afe9799 no vetheb48029
可以刪除 docker0,直接把 docker 的橋接指定為 br0。也可以保留使用默認(rèn)的配置,這樣單主機(jī)容器之間的通信可以通過 docker0,而跨主機(jī)不同容器之間通過 pipework 新建 docker 容器的網(wǎng)卡橋接到 br0,這樣跨主機(jī)容器之間就可以通信了。
$ sudo service docker stop
$ sudo ip link set dev docker0 down
$ sudo brctl delbr docker0
$ echo 'DOCKER_OPTS="-b=br0"' >> /etc/default/docker
$ sudo service docker start
$ sudo systemctl stop docker
$ sudo ip link set dev docker0 down
$ sudo brctl delbr docker0
$ cat /etc/sysconfig/docker | grep 'OPTIONS='
OPTIONS=--selinux-enabled -b=br0 -H fd://
$ sudo systemctl start docker
pipework
不同容器之間的通信可以借助于 pipework 這個(gè)工具給 docker 容器新建虛擬網(wǎng)卡并綁定 IP 橋接到 br0
$ git clone https://github.com/jpetazzo/pipework.git
$ sudo cp -rp pipework/pipework /usr/local/bin/
$ pipework
Syntax:
pipework <hostinterface> [-i containerinterface] <guest> <ipaddr>/<subnet>[@default_gateway] [macaddr][@vlan]
pipework <hostinterface> [-i containerinterface] <guest> dhcp [macaddr][@vlan]
pipework --wait [-i containerinterface]
如果刪除了默認(rèn)的 docker0 橋接,把 docker 默認(rèn)橋接指定到了 br0,則最好在創(chuàng)建容器的時(shí)候加上--net=none,防止自動(dòng)分配的 IP 在局域網(wǎng)中有沖突。
$ sudo docker run --rm -ti --net=none ubuntu:14.04 /bin/bash
root@a46657528059:/#
$ # Ctrl-P + Ctrl-Q 回到宿主機(jī) shell,容器 detach 狀態(tài)
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a46657528059 ubuntu:14.04 "/bin/bash" 4 minutes ago Up 4 minutes hungry_lalande
$ sudo pipework br0 -i eth0 a46657528059 192.168.115.10/24@192.168.115.2
# 默認(rèn)不指定網(wǎng)卡設(shè)備名,則默認(rèn)添加為 eth1
# 另外 pipework 不能添加靜態(tài)路由,如果有需求則可以在 run 的時(shí)候加上 --privileged=true 權(quán)限在容器中手動(dòng)添加,
# 但這種安全性有缺陷,可以通過 ip netns 操作
$ sudo docker attach a46657528059
root@a46657528059:/# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 86:b6:6b:e8:2e:4d
inet addr:192.168.115.10 Bcast:0.0.0.0 Mask:255.255.255.0
inet6 addr: fe80::84b6:6bff:fee8:2e4d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:648 (648.0 B) TX bytes:690 (690.0 B)
root@a46657528059:/# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.115.2 0.0.0.0 UG 0 0 0 eth0
192.168.115.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
使用ip netns添加靜態(tài)路由,避免創(chuàng)建容器使用--privileged=true選項(xiàng)造成一些不必要的安全問題:
$ docker inspect --format="{{ .State.Pid }}" a46657528059 # 獲取指定容器 pid
6350
$ sudo ln -s /proc/6350/ns/net /var/run/netns/6350
$ sudo ip netns exec 6350 ip route add 192.168.0.0/16 dev eth0 via 192.168.115.2
$ sudo ip netns exec 6350 ip route # 添加成功
192.168.0.0/16 via 192.168.115.2 dev eth0
... ...
在其它宿主機(jī)進(jìn)行相應(yīng)的配置,新建容器并使用 pipework 添加虛擬網(wǎng)卡橋接到 br0,測(cè)試通信情況即可。
另外,pipework 可以創(chuàng)建容器的 vlan 網(wǎng)絡(luò),這里不作過多的介紹了,官方文檔已經(jīng)寫的很清楚了,可以查看以下兩篇文章:
五、Docker 端口映射
# Find IP address of container with ID <container_id> 通過容器 id 獲取 ip $ sudo docker inspect <container_id> | grep IPAddress | cut -d ’"’ -f 4
無論如何,這些 ip 是基于本地系統(tǒng)的并且容器的端口非本地主機(jī)是訪問不到的。此外,除了端口只能本地訪問外,對(duì)于容器的另外一個(gè)問題是這些 ip 在容器每次啟動(dòng)的時(shí)候都會(huì)改變。
Docker 解決了容器的這兩個(gè)問題,并且給容器內(nèi)部服務(wù)的訪問提供了一個(gè)簡(jiǎn)單而可靠的方法。Docker 通過端口綁定主機(jī)系統(tǒng)的接口,允許非本地客戶端訪問容器內(nèi)部運(yùn)行的服務(wù)。為了簡(jiǎn)便的使得容器間通信,Docker 提供了這種連接機(jī)制。
5.1 自動(dòng)映射端口
-P使用時(shí)需要指定--expose選項(xiàng),指定需要對(duì)外提供服務(wù)的端口
$ sudo docker run -t -P --expose 22 --name server ubuntu:14.04
使用docker run -P自動(dòng)綁定所有對(duì)外提供服務(wù)的容器端口,映射的端口將會(huì)從沒有使用的端口池中 (49000..49900) 自動(dòng)選擇,你可以通過docker ps、docker inspect <container_id>或者docker port <container_id> <port>確定具體的綁定信息。
5.2 綁定端口到指定接口
基本語法
$ sudo docker run -p [([<host_interface>:[host_port]])|(<host_port>):]<container_port>[/udp] <image> <cmd>
默認(rèn)不指定綁定 ip 則監(jiān)聽所有網(wǎng)絡(luò)接口。
綁定 TCP 端口
# Bind TCP port 8080 of the container to TCP port 80 on 127.0.0.1 of the host machine. $ sudo docker run -p 127.0.0.1:80:8080 <image> <cmd> # Bind TCP port 8080 of the container to a dynamically allocated TCP port on 127.0.0.1 of the host machine. $ sudo docker run -p 127.0.0.1::8080 <image> <cmd> # Bind TCP port 8080 of the container to TCP port 80 on all available interfaces of the host machine. $ sudo docker run -p 80:8080 <image> <cmd> # Bind TCP port 8080 of the container to a dynamically allocated TCP port on all available interfaces $ sudo docker run -p 8080 <image> <cmd>
綁定 UDP 端口
# Bind UDP port 5353 of the container to UDP port 53 on 127.0.0.1 of the host machine. $ sudo docker run -p 127.0.0.1:53:5353/udp <image> <cmd>
二、Docker 安裝
docker 的相關(guān)安裝方法這里不作介紹,具體安裝參考 官檔
獲取當(dāng)前 docker 版本
$ sudo docker version
Client version: 1.3.2
Client API version: 1.15
Go version (client): go1.3.3
Git commit (client): 39fa2fa/1.3.2
OS/Arch (client): linux/amd64
Server version: 1.3.2
Server API version: 1.15
Go version (server): go1.3.3
Git commit (server): 39fa2fa/1.3.2
三、Docker 基礎(chǔ)用法
Docker HUB : Docker鏡像首頁,包括官方鏡像和其它公開鏡像
因?yàn)閲榈脑颍瑖鴥?nèi)下載 Docker HUB 官方的相關(guān)鏡像比較慢,可以使用 docker.cn 鏡像,鏡像保持和官方一致,關(guān)鍵是速度塊,推薦使用。
3.1 Search images
$ sudo docker search ubuntu
3.2 Pull images
$ sudo docker pull ubuntu # 獲取 ubuntu 官方鏡像 $ sudo docker images # 查看當(dāng)前鏡像列表
3.3 Running an interactive shell
$ sudo docker run -i -t ubuntu:14.04 /bin/bash
- docker run - 運(yùn)行一個(gè)容器
- -t - 分配一個(gè)(偽)tty (link is external)
- -i - 交互模式 (so we can interact with it)
- ubuntu:14.04 - 使用 ubuntu 基礎(chǔ)鏡像 14.04
- /bin/bash - 運(yùn)行命令 bash shell
注: ubuntu 會(huì)有多個(gè)版本,通過指定 tag 來啟動(dòng)特定的版本 [image]:[tag]
$ sudo docker ps # 查看當(dāng)前運(yùn)行的容器, ps -a 列出當(dāng)前系統(tǒng)所有的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c9129e9df10 ubuntu:14.04 /bin/bash 6 minutes ago Up 6 minutes cranky_babbage
3.4 相關(guān)快捷鍵
- 退出:Ctrl-Dorexit
- detach:Ctrl-P + Ctrl-Q
- attach:docker attach CONTAINER-ID
四、Docker 命令幫助
4.1 docker help
docker command
$ sudo docker # docker 命令幫助
Commands:
attach Attach to a running container # 當(dāng)前 shell 下 attach 連接指定運(yùn)行鏡像
build Build an image from a Dockerfile # 通過 Dockerfile 定制鏡像
commit Create a new image from a container's changes # 提交當(dāng)前容器為新的鏡像
cp Copy files/folders from the containers filesystem to the host path
# 從容器中拷貝指定文件或者目錄到宿主機(jī)中
create Create a new container # 創(chuàng)建一個(gè)新的容器,同 run,但不啟動(dòng)容器
diff Inspect changes on a container's filesystem # 查看 docker 容器變化
events Get real time events from the server # 從 docker 服務(wù)獲取容器實(shí)時(shí)事件
exec Run a command in an existing container # 在已存在的容器上運(yùn)行命令
export Stream the contents of a container as a tar archive
# 導(dǎo)出容器的內(nèi)容流作為一個(gè) tar 歸檔文件[對(duì)應(yīng) import ]
history Show the history of an image # 展示一個(gè)鏡像形成歷史
images List images # 列出系統(tǒng)當(dāng)前鏡像
import Create a new filesystem image from the contents of a tarball
# 從tar包中的內(nèi)容創(chuàng)建一個(gè)新的文件系統(tǒng)映像[對(duì)應(yīng) export]
info Display system-wide information # 顯示系統(tǒng)相關(guān)信息
inspect Return low-level information on a container # 查看容器詳細(xì)信息
kill Kill a running container # kill 指定 docker 容器
load Load an image from a tar archive # 從一個(gè) tar 包中加載一個(gè)鏡像[對(duì)應(yīng) save]
login Register or Login to the docker registry server
# 注冊(cè)或者登陸一個(gè) docker 源服務(wù)器
logout Log out from a Docker registry server # 從當(dāng)前 Docker registry 退出
logs Fetch the logs of a container # 輸出當(dāng)前容器日志信息
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT
# 查看映射端口對(duì)應(yīng)的容器內(nèi)部源端口
pause Pause all processes within a container # 暫停容器
ps List containers # 列出容器列表
pull Pull an image or a repository from the docker registry server
# 從docker鏡像源服務(wù)器拉取指定鏡像或者庫鏡像
push Push an image or a repository to the docker registry server
# 推送指定鏡像或者庫鏡像至docker源服務(wù)器
restart Restart a running container # 重啟運(yùn)行的容器
rm Remove one or more containers # 移除一個(gè)或者多個(gè)容器
rmi Remove one or more images
# 移除一個(gè)或多個(gè)鏡像[無容器使用該鏡像才可刪除,否則需刪除相關(guān)容器才可繼續(xù)或 -f 強(qiáng)制刪除]
run Run a command in a new container
# 創(chuàng)建一個(gè)新的容器并運(yùn)行一個(gè)命令
save Save an image to a tar archive # 保存一個(gè)鏡像為一個(gè) tar 包[對(duì)應(yīng) load]
search Search for an image on the Docker Hub # 在 docker hub 中搜索鏡像
start Start a stopped containers # 啟動(dòng)容器
stop Stop a running containers # 停止容器
tag Tag an image into a repository # 給源中鏡像打標(biāo)簽
top Lookup the running processes of a container # 查看容器中運(yùn)行的進(jìn)程信息
unpause Unpause a paused container # 取消暫停容器
version Show the docker version information # 查看 docker 版本號(hào)
wait Block until a container stops, then print its exit code
# 截取容器停止時(shí)的退出狀態(tài)值
Run 'docker COMMAND --help' for more information on a command.
docker option
Usage of docker:
--api-enable-cors=false Enable CORS headers in the remote API # 遠(yuǎn)程 API 中開啟 CORS 頭
-b, --bridge="" Attach containers to a pre-existing network bridge # 橋接網(wǎng)絡(luò)
use 'none' to disable container networking
--bip="" Use this CIDR notation address for the network bridge's IP, not compatible with -b
# 和 -b 選項(xiàng)不兼容,具體沒有測(cè)試過
-d, --daemon=false Enable daemon mode # daemon 模式
-D, --debug=false Enable debug mode # debug 模式
--dns=[] Force docker to use specific DNS servers # 強(qiáng)制 docker 使用指定 dns 服務(wù)器
--dns-search=[] Force Docker to use specific DNS search domains # 強(qiáng)制 docker 使用指定 dns 搜索域
-e, --exec-driver="native" Force the docker runtime to use a specific exec driver # 強(qiáng)制 docker 運(yùn)行時(shí)使用指定執(zhí)行驅(qū)動(dòng)器
--fixed-cidr="" IPv4 subnet for fixed IPs (ex: 10.20.0.0/16)
this subnet must be nested in the bridge subnet (which is defined by -b or --bip)
-G, --group="docker" Group to assign the unix socket specified by -H when running in daemon mode
use '' (the empty string) to disable setting of a group
-g, --graph="/var/lib/docker" Path to use as the root of the docker runtime # 容器運(yùn)行的根目錄路徑
-H, --host=[] The socket(s) to bind to in daemon mode # daemon 模式下 docker 指定綁定方式[tcp or 本地 socket]
specified using one or more tcp://host:port, unix:///path/to/socket, fd://* or fd://socketfd.
--icc=true Enable inter-container communication # 跨容器通信
--insecure-registry=[] Enable insecure communication with specified registries (no certificate verification for HTTPS and enable HTTP fallback) (e.g., localhost:5000 or 10.20.0.0/16)
--ip="0.0.0.0" Default IP address to use when binding container ports # 指定監(jiān)聽地址,默認(rèn)所有 ip
--ip-forward=true Enable net.ipv4.ip_forward # 開啟轉(zhuǎn)發(fā)
--ip-masq=true Enable IP masquerading for bridge's IP range
--iptables=true Enable Docker's addition of iptables rules # 添加對(duì)應(yīng) iptables 規(guī)則
--mtu=0 Set the containers network MTU # 設(shè)置網(wǎng)絡(luò) mtu
if no value is provided: default to the default route MTU or 1500 if no default route is available
-p, --pidfile="/var/run/docker.pid" Path to use for daemon PID file # 指定 pid 文件位置
--registry-mirror=[] Specify a preferred Docker registry mirror
-s, --storage-driver="" Force the docker runtime to use a specific storage driver # 強(qiáng)制 docker 運(yùn)行時(shí)使用指定存儲(chǔ)驅(qū)動(dòng)
--selinux-enabled=false Enable selinux support # 開啟 selinux 支持
--storage-opt=[] Set storage driver options # 設(shè)置存儲(chǔ)驅(qū)動(dòng)選項(xiàng)
--tls=false Use TLS; implied by tls-verify flags # 開啟 tls
--tlscacert="/root/.docker/ca.pem" Trust only remotes providing a certificate signed by the CA given here
--tlscert="/root/.docker/cert.pem" Path to TLS certificate file # tls 證書文件位置
--tlskey="/root/.docker/key.pem" Path to TLS key file # tls key 文件位置
--tlsverify=false Use TLS and verify the remote (daemon: verify client, client: verify daemon) # 使用 tls 并確認(rèn)遠(yuǎn)程控制主機(jī)
-v, --version=false Print version information and quit # 輸出 docker 版本信息
4.2 docker search
$ sudo docker search --help
Usage: docker search TERM
Search the Docker Hub for images # 從 Docker Hub 搜索鏡像 --automated=false Only show automated builds
--no-trunc=false Don't truncate output
-s, --stars=0 Only displays with at least xxx stars
示例:
$ sudo docker search -s 100 ubuntu # 查找 star 數(shù)至少為 100 的鏡像,找出只有官方鏡像 start 數(shù)超過 100,默認(rèn)不加 s 選項(xiàng)找出所有相關(guān) ubuntu 鏡像 NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Official Ubuntu base image 425 [OK]
4.3 docker info
$ sudo docker info
Containers: 1 # 容器個(gè)數(shù) Images: 22 # 鏡像個(gè)數(shù) Storage Driver: devicemapper # 存儲(chǔ)驅(qū)動(dòng) Pool Name: docker-8:17-3221225728-pool
Pool Blocksize: 65.54 kB
Data file: /data/docker/devicemapper/devicemapper/data
Metadata file: /data/docker/devicemapper/devicemapper/metadata
Data Space Used: 1.83 GB
Data Space Total: 107.4 GB
Metadata Space Used: 2.191 MB
Metadata Space Total: 2.147 GB
Library Version: 1.02.84-RHEL7 (2014-03-26) Execution Driver: native-0.2 # 存儲(chǔ)驅(qū)動(dòng) Kernel Version: 3.10.0-123.el7.x86_64
Operating System: CentOS Linux 7 (Core)
4.4 docker pull && docker push
$ sudo docker pull --help # pull 拉取鏡像 Usage: docker pull [OPTIONS] NAME[:TAG] Pull an image or a repository from the registry
-a, --all-tags=false Download all tagged images in the repository $ sudo docker push # push 推送指定鏡像 Usage: docker push NAME[:TAG] Push an image or a repository to the registry
示例:
$ sudo docker pull ubuntu # 下載官方 ubuntu docker 鏡像,默認(rèn)下載所有 ubuntu 官方庫鏡像 $ sudo docker pull ubuntu:14.04 # 下載指定版本 ubuntu 官方鏡像
$ sudo docker push 192.168.0.100:5000/ubuntu # 推送鏡像庫到私有源[可注冊(cè) docker 官方賬戶,推送到官方自有賬戶] $ sudo docker push 192.168.0.100:5000/ubuntu:14.04 # 推送指定鏡像到私有源
4.5 docker images
列出當(dāng)前系統(tǒng)鏡像
$ sudo docker images --help
Usage: docker images [OPTIONS] [NAME] List images
-a, --all=false Show all images (by default filter out the intermediate image layers) # -a 顯示當(dāng)前系統(tǒng)的所有鏡像,包括過渡層鏡像,默認(rèn) docker images 顯示最終鏡像,不包括過渡層鏡像 -f, --filter=[] Provide filter values (i.e. 'dangling=true') --no-trunc=false Don't truncate output
-q, --quiet=false Only show numeric IDs
示例:
$ sudo docker images # 顯示當(dāng)前系統(tǒng)鏡像,不包括過渡層鏡像 $ sudo docker images -a # 顯示當(dāng)前系統(tǒng)所有鏡像,包括過渡層鏡像 $ sudo docker images ubuntu # 顯示當(dāng)前系統(tǒng) docker ubuntu 庫中的所有鏡像 REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 12.04 ebe4be4dd427 4 weeks ago 210.6 MB
ubuntu 14.04 e54ca5efa2e9 4 weeks ago 276.5 MB
ubuntu 14.04-ssh 6334d3ac099a 7 weeks ago 383.2 MB
4.6 docker rmi
刪除一個(gè)或者多個(gè)鏡像
$ sudo docker rmi --help
Usage: docker rmi IMAGE [IMAGE...] Remove one or more images
-f, --force=false Force removal of the image # 強(qiáng)制移除鏡像不管是否有容器使用該鏡像 --no-prune=false Do not delete untagged parents # 不要?jiǎng)h除未標(biāo)記的父鏡像
4.7 docker run
$ sudo docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] Run a command in a new container
-a, --attach=[] Attach to stdin, stdout or stderr.
-c, --cpu-shares=0 CPU shares (relative weight) # 設(shè)置 cpu 使用權(quán)重 --cap-add=[] Add Linux capabilities
--cap-drop=[] Drop Linux capabilities
--cidfile="" Write the container ID to the file # 把容器 id 寫入到指定文件 --cpuset="" CPUs in which to allow execution (0-3, 0,1) # cpu 綁定 -d, --detach=false Detached mode: Run container in the background, print new container id # 后臺(tái)運(yùn)行容器 --device=[] Add a host device to the container (e.g. --device=/dev/sdc:/dev/xvdc) --dns=[] Set custom dns servers # 設(shè)置 dns --dns-search=[] Set custom dns search domains # 設(shè)置 dns 域搜索 -e, --env=[] Set environment variables # 定義環(huán)境變量 --entrypoint="" Overwrite the default entrypoint of the image # ? --env-file=[] Read in a line delimited file of ENV variables # 從指定文件讀取變量值 --expose=[] Expose a port from the container without publishing it to your host # 指定對(duì)外提供服務(wù)端口 -h, --hostname="" Container host name # 設(shè)置容器主機(jī)名 -i, --interactive=false Keep stdin open even if not attached # 保持標(biāo)準(zhǔn)輸出開啟即使沒有 attached --link=[] Add link to another container (name:alias) # 添加鏈接到另外一個(gè)容器 --lxc-conf=[] (lxc exec-driver only) Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1" -m, --memory="" Memory limit (format: <number><optional unit>, where unit = b, k, m or g) # 內(nèi)存限制 --name="" Assign a name to the container # 設(shè)置容器名 --net="bridge" Set the Network mode for the container # 設(shè)置容器網(wǎng)絡(luò)模式 'bridge': creates a new network stack for the container on the docker bridge 'none': no networking for this container 'container:<name|id>': reuses another container network stack 'host': use the host network stack inside the container. Note: the host mode gives the container full access to local system services such as D-bus and is therefore considered insecure.
-P, --publish-all=false Publish all exposed ports to the host interfaces # 自動(dòng)映射容器對(duì)外提供服務(wù)的端口 -p, --publish=[] Publish a container's port to the host # 指定端口映射 format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort (use 'docker port' to see the actual mapping) --privileged=false Give extended privileges to this container # 提供更多的權(quán)限給容器 --restart="" Restart policy to apply when a container exits (no, on-failure[:max-retry], always) --rm=false Automatically remove the container when it exits (incompatible with -d) # 如果容器退出自動(dòng)移除和 -d 選項(xiàng)沖突 --security-opt=[] Security Options
--sig-proxy=true Proxify received signals to the process (even in non-tty mode). SIGCHLD is not proxied.
-t, --tty=false Allocate a pseudo-tty # 分配偽終端 -u, --user="" Username or UID # 指定運(yùn)行容器的用戶 uid 或者用戶名 -v, --volume=[] Bind mount a volume (e.g., from the host: -v /host:/container, from docker: -v /container) # 掛載卷 --volumes-from=[] Mount volumes from the specified container(s) # 從指定容器掛載卷 -w, --workdir="" Working directory inside the container # 指定容器工作目錄
示例:
$ sudo docker images ubuntu
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 14.04 e54ca5efa2e9 4 weeks ago 276.5 MB
... ... $ sudo docker run -t -i -c 100 -m 512MB -h test1 -d --name="docker_test1" ubuntu /bin/bash # 創(chuàng)建一個(gè) cpu 優(yōu)先級(jí)為 100,內(nèi)存限制 512MB,主機(jī)名為 test1,名為 docker_test1 后臺(tái)運(yùn)行 bash 的容器 a424ca613c9f2247cd3ede95adfbaf8d28400cbcb1d5f9b69a7b56f97b2b52e5 $ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a424ca613c9f ubuntu:14.04 /bin/bash 6 seconds ago Up 5 seconds docker_test1 $ sudo docker attach docker_test1
root@test1:/# pwd /
root@test1:/# exit exit
關(guān)于cpu優(yōu)先級(jí):
By default all groups have 1024 shares. A group with 100 shares will get a ~10% portion of the CPU time - archlinux cgroups
4.8 docker start|stop|kill... ...
dockerstart|stop|kill|restart|pause|unpause|rm|commit|inspect|logs
- docker start CONTAINER [CONTAINER...]
- # 運(yùn)行一個(gè)或多個(gè)停止的容器
- docker stop CONTAINER [CONTAINER...]
- # 停掉一個(gè)或多個(gè)運(yùn)行的容器-t選項(xiàng)可指定超時(shí)時(shí)間
- docker kill [OPTIONS] CONTAINER [CONTAINER...]
- # 默認(rèn) kill 發(fā)送 SIGKILL 信號(hào)-s可以指定發(fā)送 kill 信號(hào)類型
- docker restart [OPTIONS] CONTAINER [CONTAINER...]
- # 重啟一個(gè)或多個(gè)運(yùn)行的容器-t選項(xiàng)可指定超時(shí)時(shí)間
- docker pause CONTAINER
- docker unpause CONTAINER
- docker rm [OPTIONS] CONTAINER [CONTAINER...]
- # 移除一個(gè)或多個(gè)容器
- -f, --force=false Force removal of running container
- -l, --link=false Remove the specified link and not the underlying container
- -v, --volumes=false Remove the volumes associated with the container
- docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
- # 提交指定容器為鏡像
- -a, --author="" Author (e.g., "John Hannibal Smith hannibal@a-team.com")
- -m, --message="" Commit message
- -p, --pause=true Pause container during commit
- # 默認(rèn) commit 是暫停狀態(tài)
- docker inspect CONTAINER|IMAGE [CONTAINER|IMAGE...]
- docker logs CONTAINER
- # 輸出指定容器日志信息
- -f, --follow=false Follow log output
- -t, --timestamps=false Show timestamps
- --tail="all" Output the specified number of lines at the end of logs (defaults to all logs)
參考文檔:Docker Run Reference
4.9 Docker 1.3 新增特性和命令
Digital Signature Verification
Docker 1.3 版本將使用數(shù)字簽名自動(dòng)驗(yàn)證所有官方庫的來源和完整性,如果一個(gè)官方鏡像被篡改或者被破壞,目前 Docker 只會(huì)對(duì)這種情況發(fā)出警告而并不阻止容器的運(yùn)行。
Inject new processes withdocker exec
docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in an existing container
-d, --detach=false Detached mode: run command in the background
-i, --interactive=false Keep STDIN open even if not attached
-t, --tty=false Allocate a pseudo-TTY
為了簡(jiǎn)化調(diào)試,可以使用docker exec命令通過 Docker API 和 CLI 在運(yùn)行的容器上運(yùn)行程序。
$ docker exec -it ubuntu_bash bash
上例將在容器 ubuntu_bash 中創(chuàng)建一個(gè)新的 Bash 會(huì)話。
Tune container lifecycles withdocker create
我們可以通過docker run <image name>命令創(chuàng)建一個(gè)容器并運(yùn)行其中的程序,因?yàn)橛泻芏嘤脩粢髣?chuàng)建容器的時(shí)候不啟動(dòng)容器,所以docker create應(yīng)運(yùn)而生了。
$ docker create -t -i fedora bash
6d8af538ec541dd581ebc2a24153a28329acb5268abe5ef868c1f1a261221752
上例創(chuàng)建了一個(gè)可寫的容器層 (并且打印出容器 ID),但是并不運(yùn)行它,可以使用以下命令運(yùn)行該容器:
$ docker start -a -i 6d8af538ec5
bash-4.2#
Security Options
通過--security-opt選項(xiàng),運(yùn)行容器時(shí)用戶可自定義 SELinux 和 AppArmor 卷標(biāo)和配置。
$ docker run --security-opt label:type:svirt_apache -i -t centos \ bash
上例只允許容器監(jiān)聽在 Apache 端口,這個(gè)選項(xiàng)的好處是用戶不需要運(yùn)行 docker 的時(shí)候指定--privileged選項(xiàng),降低安全風(fēng)險(xiǎn)。
參考文檔:Docker 1.3: signed images, process injection, security options, Mac shared directories
4.10 Docker 1.5 新特性
參考文檔:Docker 1.5 新特性
一、Docker 簡(jiǎn)介
Docker 兩個(gè)主要部件:
- Docker: 開源的容器虛擬化平臺(tái)
- Docker Hub: 用于分享、管理 Docker 容器的 Docker SaaS 平臺(tái) -- Docker Hub
Docker 使用客戶端-服務(wù)器 (C/S) 架構(gòu)模式。Docker 客戶端會(huì)與 Docker 守護(hù)進(jìn)程進(jìn)行通信。Docker 守護(hù)進(jìn)程會(huì)處理復(fù)雜繁重的任務(wù),例如建立、運(yùn)行、發(fā)布你的 Docker 容器。Docker 客戶端和守護(hù)進(jìn)程可以運(yùn)行在同一個(gè)系統(tǒng)上,當(dāng)然你也可以使用 Docker 客戶端去連接一個(gè)遠(yuǎn)程的 Docker 守護(hù)進(jìn)程。Docker 客戶端和守護(hù)進(jìn)程之間通過 socket 或者 RESTful API 進(jìn)行通信。
1.1 Docker 守護(hù)進(jìn)程
如上圖所示,Docker 守護(hù)進(jìn)程運(yùn)行在一臺(tái)主機(jī)上。用戶并不直接和守護(hù)進(jìn)程進(jìn)行交互,而是通過 Docker 客戶端間接和其通信。
1.2 Docker 客戶端
Docker 客戶端,實(shí)際上是 docker 的二進(jìn)制程序,是主要的用戶與 Docker 交互方式。它接收用戶指令并且與背后的 Docker 守護(hù)進(jìn)程通信,如此來回往復(fù)。
1.3 Docker 內(nèi)部
要理解 Docker 內(nèi)部構(gòu)建,需要理解以下三種部件:
- Docker 鏡像 - Docker images
- Docker 倉庫 - Docker registeries
- Docker 容器 - Docker containers
Docker 鏡像
Docker 鏡像是 Docker 容器運(yùn)行時(shí)的只讀模板,每一個(gè)鏡像由一系列的層 (layers) 組成。Docker 使用 UnionFS 來將這些層聯(lián)合到單獨(dú)的鏡像中。UnionFS 允許獨(dú)立文件系統(tǒng)中的文件和文件夾(稱之為分支)被透明覆蓋,形成一個(gè)單獨(dú)連貫的文件系統(tǒng)。正因?yàn)橛辛诉@些層的存在,Docker 是如此的輕量。當(dāng)你改變了一個(gè) Docker 鏡像,比如升級(jí)到某個(gè)程序到新的版本,一個(gè)新的層會(huì)被創(chuàng)建。因此,不用替換整個(gè)原先的鏡像或者重新建立(在使用虛擬機(jī)的時(shí)候你可能會(huì)這么做),只是一個(gè)新 的層被添加或升級(jí)了。現(xiàn)在你不用重新發(fā)布整個(gè)鏡像,只需要升級(jí),層使得分發(fā) Docker 鏡像變得簡(jiǎn)單和快速。
Docker 倉庫
Docker 倉庫用來保存鏡像,可以理解為代碼控制中的代碼倉庫。同樣的,Docker 倉庫也有公有和私有的概念。公有的 Docker 倉庫名字是 Docker Hub。Docker Hub 提供了龐大的鏡像集合供使用。這些鏡像可以是自己創(chuàng)建,或者在別人的鏡像基礎(chǔ)上創(chuàng)建。Docker 倉庫是 Docker 的分發(fā)部分。
Docker 容器
Docker 容器和文件夾很類似,一個(gè)Docker容器包含了所有的某個(gè)應(yīng)用運(yùn)行所需要的環(huán)境。每一個(gè) Docker 容器都是從 Docker 鏡像創(chuàng)建的。Docker 容器可以運(yùn)行、開始、停止、移動(dòng)和刪除。每一個(gè) Docker 容器都是獨(dú)立和安全的應(yīng)用平臺(tái),Docker 容器是 Docker 的運(yùn)行部分。
FriendShip
Hello everybody,
大家好,
a life without a friend is a life without a sun.
生活沒有朋友,就像生活沒有太陽。
Everyone need friends in their life.
每個(gè)人的生活都需要朋友。
Friends always help each other. A friend in need is a friend indeed.
朋友經(jīng)常互相幫忙。患難朋友才是真朋友。
When you are down, friends lift you up.
當(dāng)你倒下的時(shí)候,朋友扶你起來。
When you lose your way, friends guide you and cheer you on.
當(dāng)你迷路的時(shí)候,朋友為你指引方向,并為你加油。
When your are in trouble, friends come and help you.
當(dāng)你遇到苦難的時(shí)候,朋友趕來幫助你。
So cherish your friend, do not save your loving words.
所以,贊美你的朋友吧,不要吝嗇你的話。
Please lift your friends up when they are down.
當(dāng)你的朋友倒下的時(shí)候,請(qǐng)扶起他們。
Please guide your friends and cheer them on when they lost their way.
當(dāng)你的朋友迷路的時(shí)候,請(qǐng)為他們指明方向,并為他們加油。
Please help your friends when they are in trouble.
當(dāng)你的朋友們遇到困難的時(shí)候,請(qǐng)幫助他們。
1、REPAIR_MODE及其應(yīng)用場(chǎng)景
REPAIR_MODE=0, 初次執(zhí)行默認(rèn)模式,每次操作上個(gè)動(dòng)作執(zhí)行成功的數(shù)據(jù)。
REPAIR_MODE=1, 糾錯(cuò)恢復(fù)采用模式,每次操作重新獲取所有符合條件的數(shù)據(jù)。
2、糾錯(cuò)處理過程
獲取糾錯(cuò)數(shù)據(jù)修改保存,并重置回初始狀態(tài)
獲取糾錯(cuò)數(shù)據(jù)修改保存,并回退到某一狀態(tài)
?客戶如何選擇哪些數(shù)據(jù)進(jìn)行糾錯(cuò)
3、暫停恢復(fù)處理
5、mock大數(shù)據(jù)量的測(cè)試,分析執(zhí)行效率
6、如何實(shí)現(xiàn)較快的開發(fā)調(diào)試定時(shí)任務(wù)?
7、zkNodeKey檢查和使用
8、定時(shí)任務(wù)和管理引擎、執(zhí)行引擎優(yōu)化
alter table SYS_BATCHTASK_DEF_FILTER
drop primary key (FILTER_ID, DEF_ID);
drop table if exists SYS_BATCHTASK_DEF_FILTER;
create table SYS_BATCHTASK_DEF_FILTER
(
DEF_ID varchar(60) not null,
FILTER_ID varchar(60) not null,
FILTER_NAME varchar(255),
RUN_ORDER varchar(2) default '50',
RUN_TYPE varchar(2), /*執(zhí)行類型:runCreated,runCorrect,runResume,runPause*/
RUN_STEP varchar(2), /*執(zhí)行階段:main_block,part_exec,main_finish*/
BEFORE_AFTER varchar(2) default '1', /*執(zhí)行時(shí)機(jī):前1,后2*/
UPDATE_FLAG varchar(2) default '1', /*完成更新:更新1,不更新0*/
NOTE varchar(512)
);
alter table SYS_BATCHTASK_DEF_FILTER
add primary key (DEF_ID, FILTER_ID);
摘要: 定時(shí)任務(wù),灰度發(fā)布
閱讀全文