鏡像是docker三大核心概念中最重要的
獲取鏡像:
鏡像是運行容器的前提,官方的docker hub網站已經提供了數十萬個鏡像供你們開放下載。
在本地能夠是用docker [image] pull 命令直接從docker hub鏡像源下載、
簡潔的格式爲 docker pull NAME:[:TAG],其中NAME是鏡像名稱,TAG是鏡像的標籤,默認TAG不寫的
話爲最新node
cobbler@cobbler-docker:~$ sudo docker pull centos:7.5.1804 7.5.1804: Pulling from library/centos 7dc0dca2b151: Pull complete Digest: sha256:adbf1369c8dbeaa2bf9df0b891394e3ca09b5272a2fb54e0fadc6b14dd93fcad Status: Downloaded newer image for centos:7.5.1804
不指定TAG標籤,默認會是latest的標籤,也就是最新的 python
cobbler@cobbler-docker:~$ sudo docker pull centos Using default tag: latest latest: Pulling from library/centos aeb7866da422: Pull complete Digest: sha256:67dad89757a55bfdfabec8abd0e22f8c7c12a1856514726470228063ed86593b Status: Downloaded newer image for centos:latest
查看鏡像信息
一、使用images 命令列出鏡像mysql使用docker images 或 docker image ls 命令能夠列出本地主機上已有鏡像的基本信息nginx
cobbler@cobbler-docker:~$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE python 3 3a0b867afa5c 9 days ago 95.1MB ubuntu 16.04 00a14f2a1af9 9 days ago 86.6MB test 0.1 15c59e70583f 9 days ago 84.1MB centos-vim-7.5.1804.tar.gz latest a4d3ff01fb03 10 days ago 354MB nginx 1.14 ecc98fc2f376 4 weeks ago 109MB nginx latest dbfc48660aeb 4 weeks ago 109MB debian stretch-slim 4b4471f624dc 4 weeks ago 55.3MB centos 7.5.1804 76d6bc25b8a5 5 weeks ago 200MB centos latest 75835a67d134 5 weeks ago 200MB alpine latest 196d12cf6ab1 2 months ago 4.41MB ubuntu latest cd6d8154f1e1 2 months ago 84.1MB
二、使用tag命令爲鏡像添加標籤
爲了方便後續的工做使用已經制做好的鏡像,能夠爲鏡像打個專門的標籤以示區分這個鏡像是用於哪一種環境的sql
cobbler@cobbler-docker:~$ sudo docker tag ubuntu:latest ubuntu-nginx:v1 # 使用docker images 能夠看到鏡像列表多出一個ubuntu-nginx:v1的鏡像 cobbler@cobbler-docker:~$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 7.5.1804 76d6bc25b8a5 5 weeks ago 200MB centos latest 75835a67d134 5 weeks ago 200MB ubuntu-nginx v1 cd6d8154f1e1 2 months ago 84.1MB ubuntu latest cd6d8154f1e1 2 months ago 84.1MB
三、使用inspect 命令查看詳細信息docker
3.一、使用docker [image] inspect 命令能夠查看鏡像的詳細信息,好比:做者、適應架構、每層的數字摘要;json
cobbler@cobbler-docker:~$ sudo docker image inspect ubuntu:16.04 [ { "Id": "sha256:00a14f2a1af9f9e8a58062fdb484ff60b07ec67c8b4caadc02cd456cd1009859", "RepoTags": [ "ubuntu:16.04" ], "RepoDigests": [], "Parent": "", "Comment": "Imported from -", "Created": "2018-11-06T04:57:29.713185385Z", "Container": "", "ContainerConfig": { "Hostname": "", ....... ]
3.一、上述代碼是以json格式顯示的,假如只要其中一項內容時,可使用-f來指定,好比,獲取鏡像的DockerVersionubuntu
cobbler@cobbler-docker:~$ sudo docker inspect -f {{.DockerVersion}} ubuntu:16.04 18.06.1-ce
四、查看鏡像歷史用history命令vim
4.一、既然鏡像文件由多個層組成,如何知道各個層的具體內容呢,這時候就可使用History命令,列出鏡像的各層信息centos
cobbler@cobbler-docker:~$ sudo docker history centos:7.5.1804 IMAGE CREATED CREATED BY SIZE COMMENT 76d6bc25b8a5 5 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 5 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 5 weeks ago /bin/sh -c #(nop) ADD file:8f4b3be0c1427b158… 200MB
五、查找鏡像
5.一、用docker search命令能夠搜索Docker Hub官方倉庫中的鏡像。
語法爲 docker search [option] keyword 命令選項: -f, --filter filter:過濾輸出內容 --format string: 格式化輸出內容; --limit int: 限制輸出結果個數,默認爲25個; --no-trunc: 不截斷輸出結果 好比說,要搜索nginx的鏡像: cobbler@cobbler-docker:~$ sudo docker search --filter=is-official=true nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 10194 [OK] kong Open-source Microservice & API Management la… 245 [OK] 搜索被收藏數超過8包括tensorflow的鏡像: cobbler@cobbler-docker:~$ sudo docker search --filter=stars=8 tensorflow NAME DESCRIPTION STARS OFFICIAL AUTOMATED tensorflow/tensorflow Official docker images for deep learning fra… 1182 jupyter/tensorflow-notebook Jupyter Notebook Scientific Python Stack w/ … 91 xblaster/tensorflow-jupyter Dockerized Jupyter with tensorflow 51 [OK] tensorflow/serving Official images for TensorFlow Serving (http… 25 floydhub/tensorflow tensorflow 15 [OK] bitnami/tensorflow-serving Bitnami Docker Image for TensorFlow Serving 13 [OK]
六、刪除和清理鏡像
6.一、使用標籤刪除鏡像:
使用docker rmi或docker image rm命令刪除鏡像
格式docker rmi image [Image ....],其中image能夠爲標籤或id
支持選項包括: -f, -force: 強制刪除鏡像,即便有容器依賴它` -no-prune:不要清理未帶標籤的父鏡像` # 使用docker rmi 刪除鏡像 cobbler@cobbler-docker:~$ sudo docker rmi ubuntu-nginx:v1 Untagged: ubuntu-nginx:v1 # 使用docker image rm 來刪除鏡像 cobbler@cobbler-docker:~$ sudo docker image rm test:0.1 Untagged: test:0.1 Deleted: sha256:15c59e70583fe7f327431857faafe9b6954f0ed9120d040155e997401acb7f7d Deleted: sha256:532dc9a946da8489d6cc68b154ed5854da011698d90ed30787eac04b7de0557b
6.二、使用鏡像ID來刪除鏡像,這裏要注意一點,若是要刪的鏡像正在被容器使用的話是刪不掉的,要先把容器幹掉以後才能刪鏡像,也可使用強制刪除那就須要加上-f,不過不建議使用。
cobbler@cobbler-docker:~$ sudo docker image rm dbfc48660aeb Untagged: nginx:latest Untagged: nginx@sha256:b73f527d86e3461fd652f62cf47e7b375196063bbbd503e853af5be16597cb2e Deleted: sha256:dbfc48660aeb7ef0ebd74b4a7e0822520aba5416556ee43acb9a6350372e516f Deleted: sha256:1a34717cf175feab802f74f0edd1c41a811165f6e6af5cddf9b33f9211acde10 Deleted: sha256:df31c4d2dc314417ca1507e7e6ac4e732683a67b5aec725ede170ea7c2ecc99e
6.三、清理時間久而未使用的鏡像文件,可使用docker image prune 進行清理;
選項: -a, -all 刪除全部無用鏡像,不光是是臨時鏡像 -filter filter;只清理符合給定過濾器的鏡像; -f, -force: 強制刪除鏡像,不須要進行提示確應 # 清理一些時間久遠的遺留鏡像文件層,不過這裏是新環境也不存在遺留問題。 cobbler@cobbler-docker:~$ sudo docker image prune -f Total reclaimed space: 0B
七、建立鏡像
7.一、建立鏡像的方法主要有三種;基於已有鏡像的容器建立、基於本地模版導入、基於Dockerfile建立。
7.1.一、基於已有容器建立、
查看已有的鏡像,而後用centos這個鏡像來建立屬於本身的鏡像;
[root@node7 ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE httpd 2.4.37-alpine 45c9b7b78130 2 weeks ago 91.8MB mysql 5.7 702fb0b7837f 2 weeks ago 372MB centos 7.5.1804 76d6bc25b8a5 5 weeks ago 200MB
7.1.二、首先,啓動一個centos的基礎鏡像
[root@node7 ~]#docker run --name test1 -it centos:7.5.1804 /bin/bash
7.1.三、製做一個新的鏡像文件。
[root@node7 ~]#docker commit -m "yum net-tools" test1 centos-net-toos:v1 sha256:e81a7f5593c755a11f2bfe1bd1a5ddfe12547b68a67edc4e1c8a2182420ca0f4
7.1.四、查看image列表,會有個新鏡像在裏面,也就是剛剛製做好的鏡像文件
[root@node7 ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos-net-toos v1 e81a7f5593c7 33 seconds ago 299MB httpd 2.4.37-alpine 45c9b7b78130 2 weeks ago 91.8MB mysql 5.7 702fb0b7837f 2 weeks ago 372MB centos 7.5.1804 76d6bc25b8a5 5 weeks ago 200MB
7.二、基於本地模版導入:
也能夠直接從一個操做系統模版文件導入一個成一個新鏡像文件
cobbler@cobbler-docker:~$ ls centos-7.5.1804-base.tar.gz Desktop Downloads Music Public ubuntu-18.04.tar.gz centos-vim-7.5.1804.tar.gz Documents examples.desktop Pictures Templates Videos cobbler@cobbler-docker:~$ cobbler@cobbler-docker:~$ sudo cat ubuntu-18.04.tar.gz | sudo docker import - ubuntu:16.04 sha256:00a14f2a1af9f9e8a58062fdb484ff60b07ec67c8b4caadc02cd456cd1009859
7.三、基於dockerfile建立
7.3.一、建立Dockerfile文件
cobbler@cobbler-docker:~$ sudo vim Dockerfile FROM debian:stretch-slim LABEL maintainer="cobbler <cobbler@dklwj.com>" version="1.0" RUN apt-get update && \ apt-get install -y python3 && \ apt-get clean && \ rm -rf /var/lib/apt/lists/*
7.3.二、使用build命令建立新鏡像文件;
cobbler@cobbler-docker:~$ sudo docker image build -t python:3 .
八、導出和導入鏡像:
8.一、導出已製做好的鏡像
導出來以後就能夠部署到別的docker容器裏面cobbler@cobbler-docker:~/images$ sudo docker save -o nginx-1.14.tar.gz nginx:1.14 cobbler@cobbler-docker:~/images$ ls nginx-1.14.tar.gz8.二、導入已製做好的鏡像文件:
8.2.一、先把剛導出的鏡像文件複製到另一臺新部署好的docker容器裏面去cobbler@cobbler-docker:~/images$ sudo scp nginx-1.14.tar.gz root@172.20.7.57:/root root@172.20.7.57's password: nginx-1.14.tar.gz 100% 107MB 53.7MB/s 00:028.2.二、在新docker主機裏面導入剛傳過來的鏡像文件
[root@node7 ~]#docker load -i nginx-1.14.tar.gz f4a5f8f59caa: Loading layer 54.2MB/54.2MB 19c605f267f4: Loading layer 3.584kB/3.584kB Loaded image: nginx:1.14
8.2.三、查看鏡像列表:
[root@node7 ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos-net-toos v1 e81a7f5593c7 4 hours ago 299MB httpd 2.4.37-alpine 45c9b7b78130 2 weeks ago 91.8MB mysql 5.7 702fb0b7837f 2 weeks ago 372MB nginx 1.14 ecc98fc2f376 4 weeks ago 109MB centos 7.5.1804 76d6bc25b8a5 5 weeks ago 200MB