免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Docker
Table of Contents

輕量級虛擬化Docker

一、Docker基本介紹

Docker發(fā)端于一個名為dotcloud的開源項目;隨著編寫者不斷挖掘它的潛力,它迅速變成了一個炙手可熱的項目。它由GO語言編寫的,并且只支持Linux。它基于Linux容器(LxC)來創(chuàng)建一個虛擬環(huán)境。Docker不會通過建立獨有的操作系統(tǒng)、進(jìn)程和對硬件進(jìn)行模擬來創(chuàng)建屬于自己的虛擬機(jī)。請注意:虛擬環(huán)境VE(Virtual Environment)和虛擬機(jī)(VM)很不一樣。虛擬機(jī)是由虛擬工具或者模擬器(HyperV 、VMWare等)創(chuàng)建的,是一個全鏡像的主機(jī)源,其中包括操作系統(tǒng)、硬盤調(diào)整、網(wǎng)絡(luò)和虛擬進(jìn)程。過于臃腫的結(jié)構(gòu)吃掉了大量的硬盤空間同時拖慢了運行和開機(jī)速度。

一臺VE就像是輕量級的VM,它在已有的內(nèi)核關(guān)于底層硬件的鏡像上建立一個可以用來運行應(yīng)用的‘容器’。它也可以用來創(chuàng)建操作系統(tǒng),因為所謂的操作系統(tǒng)也不過是一個跑在內(nèi)核上的應(yīng)用而已??梢园袲ocker想象成LxC的一個強(qiáng)化版,只是具有以下LxC所不具有的特性:

  • 強(qiáng)大的可移植性:你可以使用Docker創(chuàng)造一個綁定了你所有你所需要的應(yīng)用的對象。這個對象可以被轉(zhuǎn)移并被安裝在任何一個安裝了 Docker 的 Linux 主機(jī)上。
  • 版本控制: Docker自帶git功能,能夠跟蹤一個容器的成功版本并記錄下來,并且可以對不同的版本進(jìn)行檢測,提交新版本,回滾到任意的一個版本等功能等等。
  • 組件的可重用性: Docker 允許創(chuàng)建或是套用一個已經(jīng)存在的包。舉個例子,如果你有許多臺機(jī)器都需要安裝 Apache 和 MySQL 數(shù)據(jù)庫,你可以創(chuàng)建一個包含了這兩個組件的‘基礎(chǔ)鏡像’。然后在創(chuàng)建新機(jī)器的時候使用這個鏡像進(jìn)行安裝就行了。
  • 可分享的類庫:已經(jīng)有上千個可用的容器被上傳并被分享到一個共有倉庫中registry.hub.docker.com??紤]到AWS對于不同環(huán)境下的調(diào)試和發(fā)布,這一做法是十分聰明的。

LxC是一個Linux提供的收容功能接口,通過LxC提供的API和簡單的工具,使得Linux用戶可以簡單的創(chuàng)建和管理系統(tǒng)或者應(yīng)用的空間。LXC容器

Docker通常用于如下場景:

web應(yīng)用的自動化打包和發(fā)布;自動化測試和持續(xù)集成、發(fā)布;在服務(wù)型環(huán)境中部署和調(diào)整數(shù)據(jù)庫或其他的后臺應(yīng)用;從頭編譯或者擴(kuò)展現(xiàn)有的OpenShift或Cloud Foundry平臺來搭建自己的PaaS環(huán)境。

Docker實踐解決方案:

  • 隔離性:Docker在文件系統(tǒng)和網(wǎng)絡(luò)級別隔離了應(yīng)用。從這個意義上來講很像在運行”真正的“虛擬機(jī)。
  • 重復(fù)性:用你喜歡的方式準(zhǔn)備系統(tǒng)(登錄并在所有軟件里執(zhí)行apt-get命令,或者使用Dockerfile),然后把修改提交到鏡像中。你可以隨意實例化若干個實例,或者把鏡像傳輸?shù)搅硪慌_機(jī)器,完全重現(xiàn)同樣的設(shè)置。
  • 安全性:Docker容器比普通的進(jìn)程隔離更為安全。Docker團(tuán)隊已經(jīng)確定了一些安全問題,正在著手解決。
  • 資源約束:Docker現(xiàn)在能限制CPU的使用率和內(nèi)存用量。目前還不能直接限制磁盤的使用情況。
  • 易于安裝:Docker有一個Docker Index,這個倉庫存儲了現(xiàn)成的Docker鏡像,你用一條命令就可以完成實例化。比如說,要使用Clojure REPL鏡像,只要運行docker run -t -i zefhemel/clojure-repl命令就能自動獲取并運行該鏡像。
  • 易于移除:不需要應(yīng)用了?銷毀容器就行。
  • 升級、降級:和EC2VM一樣:先啟動應(yīng)用的新版本,然后把負(fù)載均衡器切換到新的端口。
  • 快照、備份:Docker能提交鏡像并給鏡像打標(biāo)簽,和EC2上的快照不同,Docker是立即處理的。

參考文檔:

二、docker 安裝配置

2.1、Docker install

Docker的安裝非常簡單,這里只介紹Ubuntu 14.04的安裝,其他發(fā)行版本的安裝可以參考官網(wǎng)手冊。

$ sudo apt-get update$ sudo apt-get install docker.io$ sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker

獲取當(dāng)前docker版本

$ sudo docker versionClient version: 1.1.1Client API version: 1.13Go version (client): go1.2.1Git commit (client): bd609d2Server version: 1.1.1Server API version: 1.13Go version (server): go1.2.1Git commit (server): bd609d2

三、Docker images

  • Docker index Docker鏡像首頁,包括官方鏡像和其它公開鏡像

3.1、Search index images

$ sudo docker search ubuntu

3.2、Pull images

$ sudo docker pull ubuntu # remote index 獲取ubuntu官方鏡像$ sudo docker images # 查看當(dāng)前鏡像列表REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZEubuntu              13.10               5e019ab7bf6d        3 weeks ago         180 MBubuntu              saucy               5e019ab7bf6d        3 weeks ago         180 MBubuntu              12.04               74fe38d11401        3 weeks ago         209.6 MB... ...

3.3、Running an interactive shell

$ sudo docker run -i -t ubuntu:14.04 /bin/bash
  • docker run - 運行一個容器
  • -t - 分配一個(偽)tty (link is external)
  • -i - 交互模式(so we can interact with it)
  • ubuntu - 使用ubuntu基礎(chǔ)鏡像
  • /bin/bash - 運行bash shell

注: ubuntu會有多個版本,通過指定tag來啟動特定的版本[image]:[tag]

$ sudo docker ps # 查看當(dāng)前運行的容器, ps -a列出當(dāng)前系統(tǒng)所有的容器CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES6c9129e9df10        ubuntu:14.04        /bin/bash           6 minutes ago       Up 6 minutes                            cranky_babbage

3.4、相關(guān)快捷鍵

  • 退出:Ctrl-D or exit
  • detach:Ctrl-p + Ctrl-q
  • attach: docker attach CONTAINER ID

四、docker常用命令

4.1、docker help

$ sudo docker   # docker命令幫助Usage: docker [OPTIONS] COMMAND [arg...] -H=[unix:///var/run/docker.sock]: tcp://host:port to bind/connect to or unix://path/to/socket to useA self-sufficient runtime for linux containers.Commands:    attach    Attach to a running container                 # 當(dāng)前shell下attach連接指定運行鏡像    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ī)中    diff      Inspect changes on a container's filesystem   # 查看docker容器變化    events    Get real time events from the server          # 從docker服務(wù)獲取容器實時事件    export    Stream the contents of a container as a tar archive                 # 導(dǎo)出容器的內(nèi)容流作為一個tar歸檔文件[對應(yīng)import]    history   Show the history of an image                  # 展示一個鏡像形成歷史    images    List images                                   # 列出系統(tǒng)當(dāng)前鏡像    import    Create a new filesystem image from the contents of a tarball                # 從tar包中的內(nèi)容創(chuàng)建一個新的文件系統(tǒng)映像[對應(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              # 從一個tar包中加載一個鏡像[對應(yīng)save]    login     Register or Login to the docker registry server                 # 注冊或者登陸一個docker源服務(wù)器    logs      Fetch the logs of a container                 # 輸出當(dāng)前容器日志信息    port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT              # 查看映射端口對應(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                   # 重啟運行的容器    rm        Remove one or more containers                 # 移除一個或者多個容器    rmi       Remove one or more images                               # 移除一個或多個鏡像[無容器使用該鏡像才可刪除,否則需刪除相關(guān)容器才可繼續(xù)或-f強(qiáng)制刪除]    run       Run a command in a new container              # 在一個新的容器中運行一個命令    save      Save an image to a tar archive                # 保存一個鏡像為一個tar包[對應(yīng)load]    search    Search for an image in the docker index       # 在docker index中搜索鏡像    start     Start a stopped containers                    # 啟動容器    stop      Stop a running containers                     # 停止容器    tag       Tag an image into a repository                # 給源中鏡像打標(biāo)簽    top       Lookup the running processes of a container   # 查看容器中運行的進(jìn)程信息    unpause   Unpause a paused container                    # 取消暫停容器    version   Show the docker version information           # 查看docker版本號    wait      Block until a container stops, then print its exit code                 # 截取容器停止時的退出狀態(tài)值

docker選項幫助

$ sudo docker --helpUsage 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選項不兼容,具體沒有測試過  -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運行時使用指定執(zhí)行驅(qū)動器  -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              # 容器運行的根目錄路徑  -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                       # 跨容器通信  --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ā)  --iptables=true                        Enable Docker's addition of iptables rules                 # 添加對應(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文件位置  -r, --restart=true                     Restart previously running containers                      # 重新啟動以前運行的容器                       -s, --storage-driver=""                Force the docker runtime to use a specific storage driver  # 強(qiáng)制docker運行時使用指定存儲驅(qū)動  --selinux-enabled=false                Enable selinux support                                     # 開啟selinux支持  --storage-opt=[]                       Set storage driver options                                 # 設(shè)置存儲驅(qū)動選項  --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.1.1、docker search

官方鏡像源地址:registry.hub.docker.com

$ sudo docker searchUsage: docker search TERMSearch the docker index for images      # 從docker鏡像主頁搜索鏡像  --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選項找出所有相關(guān)ubuntu鏡像NAME      DESCRIPTION                  STARS     OFFICIAL   AUTOMATEDubuntu    Official Ubuntu base image   425       [OK]       

4.1.2、docker info

$ sudo docker info Containers: 7                       # 容器個數(shù)Images: 102                         # 鏡像個數(shù)Storage Driver: aufs                # 存儲驅(qū)動,默認(rèn)aufs Root Dir: /var/lib/docker/aufs     # 根目錄 Dirs: 116Execution Driver: native-0.2        # 執(zhí)行驅(qū)動Kernel Version: 3.13.0-24-genericWARNING: No swap limit support

4.1.3、docker pull && docker push

$ sudo docker pull                  # pull拉取鏡像Usage: docker pull NAME[:TAG]Pull an image or a repository from the registry$ 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# 推送鏡像庫到私有源[可注冊docker官方賬戶,推送到官方自有賬戶]$ sudo docker push 192.168.0.100:5000/ubuntu:14.04 # 推送指定鏡像到私有源

4.1.4、docker images

列出當(dāng)前系統(tǒng)鏡像

$ sudo docker images -hUsage: 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 SIZEubuntu                     12.04               ebe4be4dd427        4 weeks ago         210.6 MBubuntu                     14.04               e54ca5efa2e9        4 weeks ago         276.5 MBubuntu                     14.04-ssh           6334d3ac099a        7 weeks ago         383.2 MB

4.1.5、docker rmi

刪除一個或者多個鏡像

$ sudo docker rmiUsage: 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   # 不要刪除未標(biāo)記的父鏡像

4.1.6、docker run

$ sudo docker run 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)重  --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 # 后臺運行容器  --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    # 指定對外提供服務(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)         # 添加鏈接到另外一個容器[這個會專門章節(jié)講解]  --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   # 自動映射容器對外提供服務(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)限給容器  --rm=false                 Automatically remove the container when it exits (incompatible with -d) # 如果容器退出自動移除和-d選項沖突  --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                                    # 指定運行容器的用戶uid或者用戶名  -v, --volume=[]            Bind mount a volume (e.g., from the host: -v /host:/container, from docker: -v /container)                                  # 掛載卷[這個會專門章節(jié)講解]  --volumes-from=[]          Mount volumes from the specified container(s)      # 從指定容器掛載卷  -w, --workdir=""           Working directory inside the container             # 指定容器工作目錄

示例:

$ sudo docker images ubuntuREPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZEubuntu              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)建一個cpu優(yōu)先級為100,內(nèi)存限制512MB,主機(jī)名為test1,名為docker_test1后臺運行bash的容器a424ca613c9f2247cd3ede95adfbaf8d28400cbcb1d5f9b69a7b56f97b2b52e5$ sudo docker ps CONTAINER ID        IMAGE           COMMAND         CREATED             STATUS              PORTS       NAMESa424ca613c9f        ubuntu:14.04    /bin/bash       6 seconds ago       Up 5 seconds                    docker_test1$ sudo docker attach docker_test1root@test1:/# pwd/root@test1:/# exitexit

關(guān)于cpu優(yōu)先級:

By default all groups have 1024 shares. A group with 100 shares will get a ~10% portion of the CPU time:

4.1.7、docker start|stop|kill... ...

docker start|stop|kill|restart|pause|unpause|rm|commit|inspect|logs

4.2、參考文檔

五、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ī)是訪問不到的。此外,除了端口只能本地訪問外,對于容器的另外一個問題是這些ip在容器每次啟動的時候都會改變。

Docker解決了容器的這兩個問題,并且給容器內(nèi)部服務(wù)的訪問提供了一個簡單而可靠的方法。Docker通過端口綁定主機(jī)系統(tǒng)的接口,允許非本地客戶端訪問容器內(nèi)部運行的服務(wù)。為了簡便的使得容器間通信,Docker提供了這種連接機(jī)制。

5.1、自動映射端口

-P使用時需要指定--expose選項,指定需要對外提供服務(wù)的端口

$ sudo docker run -t -P --expose 22 --name server  ubuntu:14.04

使用docker run -P自動綁定所有對外提供服務(wù)的容器端口,映射的端口將會從沒有使用的端口池中(49000..49900)自動選擇,你可以通過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ò)接口。

5.2.1、綁定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>

5.2.2、綁定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>

六、配置網(wǎng)絡(luò)

Docker uses Linux bridge capabilities to provide network connectivity to containers. The docker0 bridge interface is managed by Docker for this purpose. When the Docker daemon starts it :

Dokcer通過使用Linux橋接提供容器之間的通信,docker0橋接接口的目的就是方便Docker管理。當(dāng)Docker daemon啟動時需要做以下操作:

  • creates the docker0 bridge if not present 如果docker0不存在則創(chuàng)建
  • searches for an IP address range which doesn’t overlap with an existing route 搜索一個與當(dāng)前路由不沖突的ip段
  • picks an IP in the selected range 在確定的范圍中選擇ip
  • assigns this IP to the docker0 bridge 綁定ip到docker0

6.1、列出當(dāng)前主機(jī)網(wǎng)橋

$ sudo brctl show  # brctl工具依賴bridge-utils軟件包bridge name bridge id STP enabled interfacesdocker0 8000.000000000000 no

6.2、查看當(dāng)前docker0 ip

$ sudo ifconfig docker0docker0 Link encap:Ethernet HWaddr xx:xx:xx:xx:xx:xxinet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0

在容器運行時,每個容器都會分配一個特定的虛擬機(jī)口并橋接到docker0。每個容器都會配置同docker0 ip相同網(wǎng)段的專用ip地址,docker0 的IP地址被用于所有容器的默認(rèn)網(wǎng)關(guān)。

6.3、運行一個容器

$ sudo docker run -t -i -d ubuntu /bin/bash52f811c5d3d69edddefc75aff5a4525fc8ba8bcfa1818132f9dc7d4f7c7e78b4$ sudo brctl showbridge name bridge id STP enabled interfacesdocker0 8000.fef213db5a66 no vethQCDY1N

以上, docker0 扮演著52f811c5d3d6 container這個容器的虛擬接口vethQCDY1N interface橋接的角色。

6.3.1、使用特定范圍的IP

Docker會嘗試尋找沒有被主機(jī)使用的ip段,盡管它適用于大多數(shù)情況下,但是它不是萬能的,有時候我們還是需要對ip進(jìn)一步的規(guī)劃。Docker允許你管理docker0橋接或者通過-b選項自定義橋接網(wǎng)卡,需要安裝bridge-utils軟件包。

基本步驟如下:

  • ensure Docker is stopped 確保docker的進(jìn)程是停止的
  • create your own bridge (bridge0 for example) 創(chuàng)建自定義網(wǎng)橋
  • assign a specific IP to this bridge 給網(wǎng)橋分配特定的ip
  • start Docker with the -b=bridge0 parameter 以-b的方式指定網(wǎng)橋
# Stop Docker$ sudo service docker stop# Clean docker0 bridge and# add your very own bridge0$ sudo ifconfig docker0 down$ sudo brctl addbr bridge0$ sudo ifconfig bridge0 192.168.227.1 netmask 255.255.255.0# Edit your Docker startup file$ echo "DOCKER_OPTS=\"-b=bridge0\"" >> /etc/default/docker# Start Docker$ sudo service docker start# Ensure bridge0 IP is not changed by Docker$ sudo ifconfig bridge0bridge0 Link encap:Ethernet HWaddr xx:xx:xx:xx:xx:xxinet addr:192.168.227.1 Bcast:192.168.227.255 Mask:255.255.255.0# Run a container$ docker run -i -t ubuntu /bin/bash# Container IP in the 192.168.227/24 rangeroot@261c272cd7d5:/# ifconfig eth0eth0 Link encap:Ethernet HWaddr xx:xx:xx:xx:xx:xxinet addr:192.168.227.5 Bcast:192.168.227.255 Mask:255.255.255.0# bridge0 IP as the default gatewayroot@261c272cd7d5:/# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.227.1 0.0.0.0 UG 0 0 0 eth0192.168.227.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

6.4、不同主機(jī)間容器通信

不同容器之間的通信可以借助于pipework這個工具:

$ git clone https://github.com/jpetazzo/pipework.git$ sudo cp -rp pipework/pipework /usr/local/bin/

6.4.1、安裝相應(yīng)依賴軟件

$ sudo apt-get install arping bridge-utils -y

6.4.2、橋接網(wǎng)絡(luò)

Ubuntu14.04

# cat /etc/network/interfacesauto loiface lo inet loopbackauto eth0iface eth0 inet manualauto br0iface br0 inet staticaddress 10.0.128.219netmask 255.255.255.192gateway 10.0.128.254bridge_ports eth0bridge_stp offbridge_fd 0bridge_maxwait 0dns-nameservers 10.0.127.110dns-search intranet.123u.com
啟動br0,使橋接生效
# ifup br0# Bash=$(docker run -i -d -t 10.0.128.219:5000/ubuntu:14.04 /bin/bash)# pipework br0 $Bash 10.0.128.223/26

6.5、參考文檔

七、構(gòu)建docker私有庫

為方便管理,我們需要對官方的鏡像做一些定制,我們可以構(gòu)建私有的docker registry

7.1、快速構(gòu)建

The fastest way to get running:

  • install docker:apt-get install docker.io
  • run the registry: docker run -p 5000:5000 registry

That will use the official image from the Docker index.[因為國內(nèi)被墻的原因,速度比較慢,推薦第二種方式]

7.2、傳統(tǒng)構(gòu)建方式

$ sudo apt-get install build-essential python-dev libevent-dev python-pip liblzma-dev$ git clone https://github.com/dotcloud/docker-registry.git$ cd docker-registry/$ cp config/config_sample.yml config/config.yml$ mkdir /data/registry -p$ pip install .

7.2.1、啟動

$ sudo gunicorn --access-logfile - --debug -k gevent -b 0.0.0.0:5000 -w 1 docker_registry.wsgi:application

生產(chǎn)環(huán)境可以通過如supervisord創(chuàng)建8個workers,或者通過nginx和apache來管理,具體可以參考docker-registry readme

$ sudo gunicorn -k gevent --max-requests 100 --graceful-timeout 3600 -t 3600 -b localhost:5000 -w 8 docker_registry.wsgi:application

7.2.2、提交指定容器到私有庫

$ docker tag 74fe38d11401 192.168.0.219:5000/ubuntu:12.04$ docker push 192.168.0.219:5000/ubuntu

7.3、參考

八、容器數(shù)據(jù)管理

docker管理數(shù)據(jù)的方式有兩種:

  • 數(shù)據(jù)卷
  • 數(shù)據(jù)卷容器

8.1、數(shù)據(jù)卷

數(shù)據(jù)庫是一個或多個容器專門指定繞過Union File System的目錄,為持續(xù)性或共享數(shù)據(jù)提供一些有用的功能:

  • 數(shù)據(jù)卷可以在容器間共享和重用
  • 數(shù)據(jù)卷數(shù)據(jù)改變是直接修改的
  • 數(shù)據(jù)卷數(shù)據(jù)改變不會被包括在容器中
  • 數(shù)據(jù)卷是持續(xù)性的,直到?jīng)]有容器使用它們

8.1.2、添加一個數(shù)據(jù)卷

你可以使用-v選項添加一個數(shù)據(jù)卷,或者可以使用多次-v選項為一個docker容器運行掛載多個數(shù)據(jù)卷。

$ sudo docker run --name data -v /data -t -i centos:6.4 /bin/bash# 創(chuàng)建數(shù)據(jù)卷綁定到到新建容器,新建容器中會創(chuàng)建/data數(shù)據(jù)卷bash-4.1# ls -ld /data/drwxr-xr-x 2 root root 4096 Jul 23 06:59 /data/bash-4.1# df -ThFilesystem    Type    Size  Used Avail Use% Mounted on... ...              ext4     91G  4.6G   82G   6% /data

創(chuàng)建的數(shù)據(jù)卷可以通過docker inspect獲取宿主機(jī)對應(yīng)路徑

$ sudo docker inspect data... ...    "Volumes": {        "/data": "/var/lib/docker/vfs/dir/151de401d268226f96d824fdf444e77a4500aed74c495de5980c807a2ffb7ea9"    }, # 可以看到創(chuàng)建的數(shù)據(jù)卷宿主機(jī)路徑    "VolumesRW": {        "/data1": true    }... ...

8.1.3、掛載宿主機(jī)目錄為一個數(shù)據(jù)卷

-v選項除了可以創(chuàng)建卷,也可以掛載當(dāng)前主機(jī)的一個目錄到容器中。

$ sudo docker run --name web -v /source/:/web -t -i centos:6.4 /bin/bashbash-4.1# ls -ld /web/drwxr-xr-x 2 root root 4096 Jul 23 06:59 /web/bash-4.1# df -Th... ...              ext4     91G  4.6G   82G   6% /webbash-4.1# exit

默認(rèn)掛載卷是可讀寫的,可以在掛載時指定只讀

$ sudo docker run --rm --name test -v /source/:/test:ro -t -i centos:6.4 /bin/bash

8.2、創(chuàng)建和掛載一個數(shù)據(jù)卷容器

如果你有一些持久性的數(shù)據(jù)并且想在容器間共享,或者想用在非持久性的容器上,最好的方法是創(chuàng)建一個數(shù)據(jù)卷容器,然后從此容器上掛載數(shù)據(jù)。

創(chuàng)建數(shù)據(jù)卷容器

$ sudo docker run -t -i -d -v /test --name test centos:6.4 /bin/bash

使用--volumes-from選項在另一個容器中掛載/test卷

$ sudo docker run -t -i -d --volumes-from test --name test1 centos:6.4 /bin/bash

添加另一個容器

$ sudo docker run -t -i -d --volumes-from test --name test2 centos:6.4 /bin/bash

也可以繼承其它掛載有/test卷的容器

$ sudo docker run -t -i -d --volumes-from test1 --name test3 centos:6.4 /bin/bash

8.3、備份、恢復(fù)或遷移數(shù)據(jù)卷

$ sudo docker run --volumes-from test -v $(pwd):/backup centos:6.4 tar cvf /backup/test.tar /testtar: Removing leading `/' from member namestar: /test/test.tar: file is the archive; not dumped/test//test/b/test/d/test/c/test/a

啟動一個新的容器并且從test容器中掛載卷。然后我們掛載當(dāng)前目錄$(pwd)到容器中為backup,然后備份卷數(shù)據(jù)為test.tar,即備份數(shù)據(jù)存放在當(dāng)前宿主機(jī)的所在路徑下。

$ ls        # 當(dāng)前目錄下產(chǎn)生了test卷的備份文件test.tartest.tar

你可以恢復(fù)給同一個容器或者另外的容器,新建容器并解壓備份文件到新的容器數(shù)據(jù)卷:

$ sudo docker run -t -i -d -v /test --name test2 centos:6.4  /bin/bash$ sudo docker run --volumes-from test2 -v $(pwd):/backup centos:6.4 tar xvf /backup/test.tar# 恢復(fù)之前的文件到新建卷中test/test/btest/dtest/ctest/a

8.4、參考

九、鏈接容器

之前介紹過通過端口映射連接容器中對外提供的服務(wù)。這是你與運行在容器中的服務(wù)和應(yīng)用進(jìn)行交互的方式之一。在本節(jié)中,會介紹同網(wǎng)絡(luò)端口方式訪問一樣好用的方式,即鏈接容器。docker容器具有一個鏈接系統(tǒng)允許多個容器連接在一起并且共享連接信息。docker鏈接將創(chuàng)建一個父子關(guān)系,其中父容器可以看到關(guān)于它子容器選擇的信息。

9.1 容器命名

執(zhí)行docker鏈接依賴容器的名稱,在創(chuàng)建容器時,如果不指定容器的名字,則默認(rèn)會自動創(chuàng)建一個名字。這種命名提供了兩個很有用的功能:

  • 1、給容器命名方便記憶,如命名運行web應(yīng)用的容器為web
  • 2、為docker容器提供一個參考,允許方便其他容器調(diào)用,如把容器web鏈接到容器db

可以通過--name選項給容器自定義命名:

$ sudo docker run -d -t -i --name bash centos:6.4 bash$ sudo docker ps -lCONTAINER ID    IMAGE           COMMAND     CREATED              STATUS             PORTS   NAMES70be821b7804    centos:6.4      bash        About a minute ago   Up About a minute          bash                

也可以通過docker inspect獲取容器名

$ sudo docker inspect -f "{{ .Name }}" 70be821b7804/bash
注:容器名稱必須是唯一,意思是你只能命名一個叫`web`的容器。如果你想復(fù)用容器名,則必須在創(chuàng)建新的容器前通過`docker rm`刪除舊的容器。

9.2 鏈接容器

鏈接允許容器間安全可見通信,使用--link選項創(chuàng)建鏈接。

$ sudo docker run -d -t -i --name test1 centos:6.4 bash

創(chuàng)建一個容器鏈接到test1容器

$ sudo docker run -d -t -i --name test2 --link test1:test1 centos:6.4 bash
  • --link name:alias
$ sudo docker psCONTAINER ID    IMAGE       COMMAND CREATED         STATUS      PORTS   NAMES1966fe2a909c    centos:6.4  bash    2 minutes ago   Up 2 minutes        test220f7f53b61df    centos:6.4  bash    4 minutes ago   Up 4 minutes        test1,test2/test1

從上面命令輸出可以看出test2鏈接到test1容器的父/子關(guān)系。

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Docker學(xué)習(xí)筆記:Docker 基礎(chǔ)用法和命令幫助
使用Docker搭建Java Web運行環(huán)境
非常詳細(xì)的 Docker 學(xué)習(xí)筆記
Docker容器學(xué)習(xí)梳理--基礎(chǔ)知識(2)
docker
『中級篇』什么是Container(15) – IT人故事會
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服