鏡像是Docker三大核心概念中最爲重要的
Docker運行容器前須要本地存在對應的鏡像,若是鏡像沒保存在本地,Docker會嘗試先從默認鏡像倉庫下載(默認使用Docker Hub公共註冊服務器中的倉庫),用戶也能夠經過配置,使用自定義的鏡像倉庫php
鏡像是運行容器的前提,官方的Docker Hub網站已經提供了數十萬個鏡像供你們開放下載。
可使用docker pull命令直接從Docker Hub鏡像源來下載鏡像。該命令的格式爲docker pull NAME[:TAG],其中NAME是鏡像倉庫的名稱(用來區分鏡像),TAG是鏡像的標籤(每每用來表示版本信息)。一般狀況下,描述一個鏡像須要包括「名稱+標籤」信息
例如,獲取一個Ubuntu 14.04系統的基礎鏡像可使用以下命令java
FengZhendeMacBook-Pro:java FengZhen$ docker pull ubuntu:14.04 14.04: Pulling from library/ubuntu 28bfaceaff9b: Pull complete ac540055f2f8: Pull complete 2965585ef8b8: Pull complete 2416bb9f3ad2: Pull complete 93b55a6a6807: Pull complete Digest: sha256:885bb6705b01d99544ddb98cbe4e4555d1efe1d052cef90832e72a0688ac6b37 Status: Downloaded newer image for ubuntu:14.04
對於Docker鏡像來講,若是不顯式指定TAG,則默認會選擇latest標籤,這會下載倉庫中最新版本的鏡像mysql
注意:通常來講,鏡像的latest標籤意味着該鏡像的內容會跟蹤最新的非穩定版本而發佈,內容是不穩定的,從穩定性上考慮,不要再生產環境中忽略鏡像的標籤信息或使用默認的latest標記的鏡像。linux
下載過程當中能夠看出,鏡像文件通常由若干層(layer)組成,28bfaceaff9b這樣的串是層的惟一ID,使用docker pull命令下載時會獲取並輸出鏡像的各層信息。當不一樣的鏡像包括相同的層時,本地僅存儲一分內容,減少了須要的存儲空間
在使用不一樣的鏡像倉庫服務器的狀況下,可能會出現鏡像重名的狀況。
嚴格地講,鏡像的倉庫名稱中還應該添加倉庫地址(即registry,註冊服務器)做爲前綴,咱們默認使用的時Docker Hub服務,該前綴能夠省略
例如 docker pull Ubuntu:14.04至關於docker pull registry.hub.docker.com/Ubuntu:14.04
若是從非官方倉庫下載,則須要在倉庫名稱前指定完整的倉庫地址。例如從網易蜂巢的鏡像源來下載ubuntu:14.04鏡像,可使用以下命令
docker pull hub.c.163.com/public/Ubuntu:14.04nginx
pull子命令支持的選項主要包括:
-a, --all-tags=true|false:是否獲取倉庫中的全部鏡像,默認爲否
下載鏡像到本地後,便可隨時使用該鏡像了,例如利用該鏡像建立一個容器,在其中運行bash應用
以下web
docker run -it ubuntu:14.04 bash FengZhendeMacBook-Pro:java FengZhen$ docker run -it ubuntu:14.04 bash root@3697728743e8:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
已是ubuntu的環境了sql
使用images命令列出鏡像docker
FengZhendeMacBook-Pro:java FengZhen$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 14.04 578c3e61a98c 42 hours ago 223MB ubuntu latest 113a43faa138 42 hours ago 81.2MB nginx latest cd5239a0906a 45 hours ago 109MB
在列出的信息中,能夠看到如下幾個字段信息
REPOSITORY:來自於哪一個倉庫,好比ubuntu倉庫用來保存ubuntu系列的基礎鏡像
TAG:鏡像的標籤信息,好比14.0四、latest用來標註不一樣的版本信息。標籤只是標記,並不能標識鏡像內容
IMAGE ID:鏡像的ID(惟一標識鏡像),如ubuntu:lastest和ubutu:16.04鏡像的ID都是2fa927b5cdd3,說明他們目前實際上指向同一個鏡像
CREATED:建立時間,說明鏡像最後的更新時間
SIZE:鏡像大小,優秀的鏡像每每體積都較小
其中鏡像ID信息十分重要,它惟一標識了鏡像。在使用鏡像ID的時候,通常可使用該ID的前若干個字符組成的可區分串來替代完整的IDjson
爲了方便在後續工做中使用特定鏡像,還可使用docker tag命令來爲本地鏡像添加新的標籤。例如添加一個新的newubuntutag:14.04標籤ubuntu
FengZhendeMacBook-Pro:java FengZhen$ docker tag ubuntu:14.04 newUbuntuTag:14.04 Error parsing reference: "newUbuntuTag:14.04" is not a valid repository/tag: invalid reference format: repository name must be lowercase FengZhendeMacBook-Pro:java FengZhen$ docker tag ubuntu:14.04 newubuntutag:14.04 FengZhendeMacBook-Pro:java FengZhen$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE newubuntutag 14.04 578c3e61a98c 42 hours ago 223MB ubuntu 14.04 578c3e61a98c 42 hours ago 223MB nginx latest cd5239a0906a 45 hours ago 109MB
能夠看到多了一個newubuntutag:14.04標籤的鏡像,以後就能夠直接使用newubuntutag:14.04來表示這個鏡像了。能夠看到newubuntutag:14.04鏡像的ID跟ubuntu:14.04的徹底一致。他們實際上指向同一個鏡像文件,只是別名不一樣而已。docker tag命令添加的標籤實際上起到了相似連接的做用
使用docker inspect命令能夠獲取該鏡像的詳細信息,包括製做者、適應架構,各層的數字摘要等。
FengZhendeMacBook-Pro:java FengZhen$ docker inspect ubuntu:14.04 [ { "Id": "sha256:578c3e61a98cb5720e7c8fc152017be1dff373ebd72a32bbe6e328234efc8d1a", "RepoTags": [ "newubuntutag:14.04", "ubuntu:14.04" ], "RepoDigests": [ "ubuntu@sha256:885bb6705b01d99544ddb98cbe4e4555d1efe1d052cef90832e72a0688ac6b37" ], "Parent": "", "Comment": "", "Created": "2018-06-05T21:21:36.655541773Z", "Container": "c7400e9771375b0166af4a1aa294cc20d73147497224b62978d353713aa5262b", "ContainerConfig": { "Hostname": "c7400e977137", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/sh", "-c", "#(nop) ", "CMD [\"/bin/bash\"]" ], "ArgsEscaped": true, "Image": "sha256:d0cda387c3c3171ae6b7fdb9c6e14e54b43c938dd41fc1877b6ad1feac1f4bc3", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "DockerVersion": "17.06.2-ce", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], "ArgsEscaped": true, "Image": "sha256:d0cda387c3c3171ae6b7fdb9c6e14e54b43c938dd41fc1877b6ad1feac1f4bc3", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null }, "Architecture": "amd64", "Os": "linux", "Size": 223367926, "VirtualSize": 223367926, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/95cf58ea97eb479f7942aeeb6a620cf4b1bd1019dfb5d17b0aaa20d5a398d34f/diff:/var/lib/docker/overlay2/90dec2408941a6d7cc4aadb0774510121cf927f07dfa81c3e3f187b8f80c4c6a/diff:/var/lib/docker/overlay2/e6041ed1c1580124e891f0928f5cad622d62f21748c4fb1332aad03ec8bcdaab/diff:/var/lib/docker/overlay2/6076cfa18569a9e4520e9b7881eb3af355223058af3fad8a6fdb6c4cab2b067b/diff", "MergedDir": "/var/lib/docker/overlay2/a00109e35bee4e450a0fcfff9e656ae6d2a1ab7d29d8d3afd3e27c11792e3376/merged", "UpperDir": "/var/lib/docker/overlay2/a00109e35bee4e450a0fcfff9e656ae6d2a1ab7d29d8d3afd3e27c11792e3376/diff", "WorkDir": "/var/lib/docker/overlay2/a00109e35bee4e450a0fcfff9e656ae6d2a1ab7d29d8d3afd3e27c11792e3376/work" }, "Name": "overlay2" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:04964fddc9461e6657e6fb372f96ca77e671871829ed453d933a03c491e70803", "sha256:a4aaef726d022d0d7e49c09bebf4a619d6db41bb05cc21d2f6b86d510e2cf3b3", "sha256:705419d10b1391753cc659618c6c76deae49e55513714025007440a65d4ed3fd", "sha256:6ec525dfd0603634b8e0e7eec752e43f9ab72460f0e3cf29beeba822d708851b", "sha256:70d93396f87f34c5b8fa4080bbf22d06cce8c05341a6b11c2e03c6d4d3959747" ] }, "Metadata": { "LastTagTime": "2018-06-07T14:52:11.7728708Z" } } ]
返回的是一個json格式的消息,若是咱們只要其中一項內容時,可使用參數-f來指定,好比,獲取鏡像的Architecture
FengZhendeMacBook-Pro:java FengZhen$ docker inspect -f {{".Architecture"}} ubuntu:14.04 amd64
使用history命令查看鏡像歷史
使用history子命令,能夠列出各層的建立信息
FengZhendeMacBook-Pro:java FengZhen$ docker history ubuntu:14.04 IMAGE CREATED CREATED BY SIZE COMMENT 578c3e61a98c 42 hours ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 42 hours ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B <missing> 42 hours ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$… 2.76kB <missing> 42 hours ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0B <missing> 42 hours ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 195kB <missing> 42 hours ago /bin/sh -c #(nop) ADD file:3c65bdbcf1318520e… 223MB
過長的命令會被自動截斷,可使用--no-trunc選項來輸出完整信息
FengZhendeMacBook-Pro:java FengZhen$ docker history --no-trunc=true ubuntu:14.04 IMAGE CREATED CREATED BY SIZE COMMENT sha256:578c3e61a98cb5720e7c8fc152017be1dff373ebd72a32bbe6e328234efc8d1a 42 hours ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 42 hours ago /bin/sh -c mkdir -p /run/systemd && echo 'docker' > /run/systemd/container 7B <missing> 42 hours ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$/\1/g' /etc/apt/sources.list 2.76kB <missing> 42 hours ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0B <missing> 42 hours ago /bin/sh -c set -xe && echo '#!/bin/sh' > /usr/sbin/policy-rc.d && echo 'exit 101' >> /usr/sbin/policy-rc.d && chmod +x /usr/sbin/policy-rc.d && dpkg-divert --local --rename --add /sbin/initctl && cp -a /usr/sbin/policy-rc.d /sbin/initctl && sed -i 's/^exit.*/exit 0/' /sbin/initctl && echo 'force-unsafe-io' > /etc/dpkg/dpkg.cfg.d/docker-apt-speedup && echo 'DPkg::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' > /etc/apt/apt.conf.d/docker-clean && echo 'APT::Update::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' >> /etc/apt/apt.conf.d/docker-clean && echo 'Dir::Cache::pkgcache ""; Dir::Cache::srcpkgcache "";' >> /etc/apt/apt.conf.d/docker-clean && echo 'Acquire::Languages "none";' > /etc/apt/apt.conf.d/docker-no-languages && echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > /etc/apt/apt.conf.d/docker-gzip-indexes && echo 'Apt::AutoRemove::SuggestsImportant "false";' > /etc/apt/apt.conf.d/docker-autoremove-suggests 195kB <missing> 42 hours ago /bin/sh -c #(nop) ADD file:3c65bdbcf1318520e7af157cb00317c8312ec16bcaf181d0db919c45f0d7a85e in / 223MB
使用docker search 命令能夠搜索遠程倉庫中共享的鏡像,默認搜索官方倉庫中的鏡像。用法爲docker search TERM,支持的參數主要包括
--automated=true|false:僅顯示自動建立的鏡像,默認爲否
--no-trunc=true|false:輸出信息不截斷顯示,默認爲否
-s, --stars=X:指定僅顯示評價爲指定星級以上的鏡像,默認爲0,即輸出全部鏡像
例如,搜索全部自動建立的評價爲3+的帶nginx關鍵字的鏡像,以下
FengZhendeMacBook-Pro:java FengZhen$ docker search --automated -s 3 nginx Flag --automated has been deprecated, use --filter=is-automated=true instead Flag --stars has been deprecated, use --filter=stars=3 instead NAME DESCRIPTION STARS OFFICIAL AUTOMATED jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1343 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 547 [OK] jrcs/letsencrypt-nginx-proxy-companion LetsEncrypt container to use with nginx as p… 375 [OK] webdevops/php-nginx Nginx with PHP-FPM 104 [OK] bitnami/nginx Bitnami nginx Docker Image 53 [OK] zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server wi… 52 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 35 [OK] tobi312/rpi-nginx NGINX on Raspberry Pi / armhf 19 [OK] blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 9 [OK] wodby/drupal-nginx Nginx for Drupal container image 9 [OK] webdevops/nginx Nginx container 8 [OK] nginxdemos/hello NGINX webserver that serves a simple page co… 6 [OK] 1science/nginx Nginx Docker images that include Consul Temp… 4 [OK]
可看到提示
Flag --automated has been deprecated, use --filter=is-automated=true instead Flag --stars has been deprecated, use --filter=stars=3 instead
--automated不提倡使用,建議使用--filter=is-automated=true代替
-s建議使用--filter=stars=3代替
以下
FengZhendeMacBook-Pro:java FengZhen$ docker search nginx --filter=stars=3 --filter=is-automated=true NAME DESCRIPTION STARS OFFICIAL AUTOMATED jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1344 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 547 [OK] jrcs/letsencrypt-nginx-proxy-companion LetsEncrypt container to use with nginx as p… 375 [OK] webdevops/php-nginx Nginx with PHP-FPM 105 [OK] bitnami/nginx Bitnami nginx Docker Image 53 [OK] zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server wi… 52 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 35 [OK] tobi312/rpi-nginx NGINX on Raspberry Pi / armhf 19 [OK] wodby/drupal-nginx Nginx for Drupal container image 9 [OK] blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 9 [OK] webdevops/nginx Nginx container 8 [OK] 1science/nginx Nginx Docker images that include Consul Temp… 4 [OK]
能夠看到返回了不少包含關鍵字的鏡像,其中包括鏡像名字、描述、星級(表示該鏡像的受歡迎程度)、是否官方建立、是否自動建立等,默認的輸出結果將按照星級評價進行排序。
FengZhendeMacBook-Pro:java FengZhen$ docker search nginx --filter=stars=3 NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 8745 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1344 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 547 [OK] jrcs/letsencrypt-nginx-proxy-companion LetsEncrypt container to use with nginx as p… 375 [OK] kong Open-source Microservice & API Management la… 192 [OK] webdevops/php-nginx Nginx with PHP-FPM 105 [OK] kitematic/hello-world-nginx A light-weight nginx container that demonstr… 99 bitnami/nginx Bitnami nginx Docker Image 53 [OK] zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server wi… 52 [OK] linuxserver/nginx An Nginx container, brought to you by LinuxS… 35 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 35 [OK] tobi312/rpi-nginx NGINX on Raspberry Pi / armhf 19 [OK] nginxdemos/nginx-ingress NGINX Ingress Controller for Kubernetes . Th… 11 wodby/drupal-nginx Nginx for Drupal container image 9 [OK] blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 9 [OK] webdevops/nginx Nginx container 8 [OK] centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 6 1science/nginx Nginx Docker images that include Consul Temp… 4 [OK] centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 3
使用標籤刪除鏡像
使用docker rmi命令能夠刪除鏡像,命令格式爲docker rmi IMAGE [IMAGE..],其中IMAGE能夠爲標籤或ID
例如,要刪除掉newubuntutag:14.04鏡像,可使用以下命令
FengZhendeMacBook-Pro:java FengZhen$ docker rmi newubuntutag:14.04 Untagged: newubuntutag:14.04
當同一個鏡像擁有多個標籤的時候,docker rmi命令只是刪除該鏡像多個標籤中的指定標籤而已,並不影響鏡像文件。所以上述操做至關於只是刪除了鏡像578c3e61a98c的一個標籤而已。
再次查看本地鏡像,發現ubuntu:14.04的鏡像仍是存在的
FengZhendeMacBook-Pro:java FengZhen$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 14.04 578c3e61a98c 3 days ago 223MB ubuntu latest 113a43faa138 3 days ago 81.2MB nginx latest cd5239a0906a 3 days ago 109MB
但當鏡像只剩下一個標籤的時候,使用docker rmi命令就會完全刪除鏡像
例如刪除ubuntu:latest的鏡像,因爲該鏡像沒有額外的標籤指向它,執行docker rmi命令,能夠看出它會刪除這個鏡像文件的全部層
FengZhendeMacBook-Pro:java FengZhen$ docker rmi ubuntu:latest Untagged: ubuntu:latest Untagged: ubuntu@sha256:5f4bdc3467537cbbe563e80db2c3ec95d548a9145d64453b06939c4592d67b6d Deleted: sha256:113a43faa1382a7404681f1b9af2f0d70b182c569aab71db497e33fa59ed87e6 Deleted: sha256:a9fa410a3f1704cd9061a802b6ca6e50a0df183cb10644a3ec4cac9f6421677a Deleted: sha256:b21f75f60422609fa79f241bf80044e6e133dd0662851afb12dacd22d199233a Deleted: sha256:038d2d2aa4fb988c06f04e3af208cc0c1dbd9703aa04905ade206d783e7bc06a Deleted: sha256:b904d425ea85240d6af5a6c6f145e05d5e0127f547f8eb4f68552962df846e81 Deleted: sha256:db9476e6d963ed2b6042abef1c354223148cdcdbd6c7416c71a019ebcaea0edb
使用鏡像ID刪除鏡像
當使用docker rmi命令,而且後面跟上鏡像ID(也能夠是能進行區分部分ID串前綴)時,會先嚐試刪除全部指向該鏡像的標籤,而後刪除該鏡像文件自己。
注意,當有該鏡像建立的容器存在時,鏡像文件默認是沒法被刪除的,例如,先利用ubuntu:14.04鏡像建立一個簡單的容器來輸出一段話
FengZhendeMacBook-Pro:java FengZhen$ docker run ubuntu:14.04 echo 'hello! docker,i am here'
hello! docker,i am here
使用docker ps –a 命令能夠看到本機上存在的全部容器
FengZhendeMacBook-Pro:java FengZhen$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 71ec56c543c4 ubuntu:14.04 "echo 'hello! docker…" About a minute ago Exited (0) About a minute ago ecstatic_gates
能夠看到後臺存在一個退出狀態的容器,是剛基於ubuntu:14.04鏡像建立的,試圖刪除該鏡像,Docker會提示有容器正在運行,沒法刪除
FengZhendeMacBook-Pro:java FengZhen$ docker rmi ubuntu:14.04 Error response from daemon: conflict: unable to remove repository reference "ubuntu:14.04" (must force) - container 3697728743e8 is using its referenced image 578c3e61a98c
若是想強行刪除鏡像,可使用-f參數
FengZhendeMacBook-Pro:java FengZhen$ docker rmi -f ubuntu:14.04 Untagged: ubuntu:14.04 Untagged: ubuntu@sha256:885bb6705b01d99544ddb98cbe4e4555d1efe1d052cef90832e72a0688ac6b37 Deleted: sha256:578c3e61a98cb5720e7c8fc152017be1dff373ebd72a32bbe6e328234efc8d1a
注意,一般並不推薦使用-f參數來強制刪除一個存在容器依賴的鏡像。正確的作法是先刪除依賴該鏡像的全部容器,再來刪除鏡像。
先刪除容器ID爲71ec56c543c4
FengZhendeMacBook-Pro:java FengZhen$ docker rm 71ec56c543c4 71ec56c543c4
再使用ID來刪除鏡像,此時會正常打印出刪除的各層信息
FengZhendeMacBook-Pro:java FengZhen$ docker rmi 578c3e61a98c Untagged: ubuntu:14.04 Untagged: ubuntu@sha256:885bb6705b01d99544ddb98cbe4e4555d1efe1d052cef90832e72a0688ac6b37 Deleted: sha256:578c3e61a98cb5720e7c8fc152017be1dff373ebd72a32bbe6e328234efc8d1a Deleted: sha256:520e4d96640a482cd5c5be5f42e2941d2d6001262be2e5916420639ed1d4e288 Deleted: sha256:603f8df1d8f7ccc2850f6147923eeb147f5bf5990382955aa19ce9d4fdc7faae Deleted: sha256:701ca6996674b5523da7961585844506ff4eaa30efe40a5421b41473fa190aa1 Deleted: sha256:2278559b727b2777bc71de91abe6f78a330b402cd0c07f77fe9751745ba28ec1 Deleted: sha256:04964fddc9461e6657e6fb372f96ca77e671871829ed453d933a03c491e70803
建立鏡像的方法主要有三種
1. 基於已有鏡像的容器建立
2. 基於本地模板導入
3. 基於Dockerfile建立
基於已有鏡像的容器建立
該方法主要是使用docker commit命令,命令格式爲
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
主要選項包括:
-a, --author=」」: 做者信息
-c, --change=[] : 提交的時候執行Dockerfile指令,包括CMD|ENTRYPOINT|ENV|EXPOSE|LABEL|ONBUILD|USER|VOLUME|WORKDIR等
-m, --message=」」: 提交消息
-p, --pause=true: 提交時暫停容器運行
演示如何使用該命令建立一個新鏡像。首先啓動一個鏡像,並在其中進行修改操做,例如建立一個test文件,以後退出
FengZhendeMacBook-Pro:java FengZhen$ docker run -it ubuntu:14.04 /bin/bash root@de56c2499e0f:/# touch test root@de56c2499e0f:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys test tmp usr var root@de56c2499e0f:/# exit exit
此容器ID爲de56c2499e0f
此時該容器跟原ubuntu14.04鏡像相比,已經發生了改變,可使用docker commit命令來提交爲一個新的鏡像。提交時可使用ID或名稱來指定容器
FengZhendeMacBook-Pro:java FengZhen$ docker commit -m "Added a new file" -a "Docker FengZhen" de56c2499e0f test:0.1
sha256:3f520588bfe80d30c5964c07bba702bf67b2cc3cd9db1464992b7f474a2c558e
順利的話,會返回新建立的鏡像的ID,例如3f520588bfe80d30c5964c07bba702bf67b2cc3cd9db1464992b7f474a2c558e
此時查看鏡像列表,會發現新建立的鏡像已經存在了。
FengZhendeMacBook-Pro:java FengZhen$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 0.1 3f520588bfe8 59 seconds ago 223MB ubuntu 14.04 578c3e61a98c 3 days ago 223MB nginx latest cd5239a0906a 3 days ago 109MB
啓動新鏡像,會發現存在該test文件。
FengZhendeMacBook-Pro:java FengZhen$ docker run -it test:0.1 /bin/bash root@932e7ddfe656:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys test tmp usr var
基於本地模板導入
用戶能夠直接從一個操做系統模板文件導入一個鏡像,主要使用docker import命令,命令格式爲
docker import [OPTIONS] file|URL|-[REPOSITORY[;TAG]]
要直接導入一個鏡像,可使用OpenVZ提供的模板來建立,或者用其它已導出的鏡像模板來建立。OPENVZ模板的下載地址爲
https://openvz.org/Download/template/precreated
例如,下載了ubuntu-14.04的模板壓縮包,以後使用一下命令導入
FengZhendeMacBook-Pro:java FengZhen$ cat /Users/FengZhen/Downloads/ubuntu-14.04-x86_64-minimal.tar.gz | docker import - ubuntu:14.04 sha256:9b485381ee643d26cc1bd53f47d75feb60740a57edb1b91a6f1cd2a89b7c12ec FengZhendeMacBook-Pro:java FengZhen$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 14.04 9b485381ee64 6 seconds ago 215MB test 0.1 3f520588bfe8 17 minutes ago 223MB ubuntu <none> 578c3e61a98c 3 days ago 223MB nginx latest cd5239a0906a 3 days ago 109MB
發現會把以前的版本TAG覆蓋掉
修改下,先將新導入的鏡像刪除
FengZhendeMacBook-Pro:java FengZhen$ docker rmi ubuntu:14.04 Untagged: ubuntu:14.04
重命名
docker tag 578c3e61a98c ubuntu:14.04
從新導入
FengZhendeMacBook-Pro:java FengZhen$ cat /Users/FengZhen/Downloads/ubuntu-14.04-x86_64-minimal.tar.gz | docker import - ubuntu-mini:14.04 sha256:93d765cbffc3270af003f3d192a3b53833acd53bf4063a4a57c670c2e774cd6e
也能夠直接將新導入的重命名
若是本地鏡像存在多個鏡像標籤,好比
FengZhendeMacBook-Pro:java FengZhen$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu-mini 14.04 93d765cbffc3 About a minute ago 215MB test 0.1 3f520588bfe8 22 minutes ago 223MB ubuntu-new 14.04 578c3e61a98c 3 days ago 223MB ubuntu 14.04 578c3e61a98c 3 days ago 223MB nginx latest cd5239a0906a 3 days ago 109MB
存在ubuntu:14.04和ubuntu-new:14.04,ubuntu-new:14.04是經過tag命令新建的鏡像標籤docker tag ubuntu:14.04 ubuntu-new:14.04
此時若是使用tag對指定的鏡像ID進行倉庫:TAG重命名,會再新建一個鏡像標籤
FengZhendeMacBook-Pro:java FengZhen$ docker tag 578c3e61a98c ubuntu-edit:14.04
FengZhendeMacBook-Pro:java FengZhen$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu-mini 14.04 93d765cbffc3 2 minutes ago 215MB
test 0.1 3f520588bfe8 23 minutes ago 223MB
ubuntu-edit 14.04 578c3e61a98c 3 days ago 223MB
ubuntu-new 14.04 578c3e61a98c 3 days ago 223MB
ubuntu 14.04 578c3e61a98c 3 days ago 223MB
nginx latest cd5239a0906a 3 days ago 109MB
會發現多了一個ubuntu-edit:14.04鏡像標籤
用戶可使用docker save 和 docker load命令來存出和載入鏡像
存出鏡像
若是要導出鏡像到本地文件,可使用docker save命令。例如,導出本地的ubuntu:14.04鏡像爲文件ubuntu_14.04.tar,以下
FengZhendeMacBook-Pro:java FengZhen$ docker save -o ubuntu_14.04.tar ubuntu:14.04
以後就能夠經過複製ubuntu_14.04.tar文件將鏡像分享給他人
載入鏡像
可使用docker load將導出的tar文件再導入到本地鏡像庫,例如從文件ubuntu_14.04.tar導入鏡像到本地鏡像列表,以下
先將本地標籤爲ubuntu:14.04的鏡像標籤刪除
FengZhendeMacBook-Pro:java FengZhen$ docker rmi ubuntu:14.04 Untagged: ubuntu:14.04 Untagged: ubuntu@sha256:885bb6705b01d99544ddb98cbe4e4555d1efe1d052cef90832e72a0688ac6b37
載入
FengZhendeMacBook-Pro:java FengZhen$ docker load --input ubuntu_14.04.tar Loaded image: ubuntu:14.04
查看本地鏡像
FengZhendeMacBook-Pro:java FengZhen$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu-mini 14.04 93d765cbffc3 16 minutes ago 215MB test 0.1 3f520588bfe8 37 minutes ago 223MB ubuntu-edit 14.04 578c3e61a98c 3 days ago 223MB ubuntu-new 14.04 578c3e61a98c 3 days ago 223MB ubuntu 14.04 578c3e61a98c 3 days ago 223MB nginx latest cd5239a0906a 3 days ago 109MB
發現已經導入成功。
導入也可使用
docker load < ubuntu_14.04.tar
可使用docker push命令上傳鏡像到倉庫,默認上傳到Docker Hub官方倉庫(須要登陸)。命令格式以下
docker push NAME[:TAG] | [REGISTRY_HOST[:REGISTRY_PORT]/]NAME[:TAG]
用戶在Docker Hub網站註冊後能夠上傳自制的鏡像。例如用戶user上傳本地的test:latest鏡像,能夠先添加新的標籤user/test:latest,而後用docker push 命令上傳鏡像
docker tag test:latest user/test:latest docker push user/test:latest FengZhendeMacBook-Pro:java FengZhen$ docker tag nginx:latest fengzhen/nginx:latest FengZhendeMacBook-Pro:java FengZhen$ docker push fengzhen/nginx:latest The push refers to repository [docker.io/fengzhen/nginx] 3ff93588120e: Preparing 24ee0a3fd4b9: Preparing d626a8ad97a1: Preparing