03-使用docker鏡像

Docker鏡像

鏡像是Docker三個核心概念中最重要的,運行流程爲
Docker運行——本地是否存在對應鏡像——若不存在Docker嘗試從默認鏡像倉庫下載,用戶也可以自定義配置鏡像倉庫

獲取鏡像

格式爲docker [image] pull NAME[:TAG]
其中NAME是鏡像倉庫名稱,TAG是鏡像的標籤(往往用來代表版本),通常情況下描述一個鏡像需要包括“名稱+標籤”信息
測試獲取一個ubuntu 18.04的系統

[root@docker01 ~]# docker pull ubuntu:18.04
18.04: Pulling from library/ubuntu
423ae2b273f4: Downloading  15.25MB/26.69MB
de83a2304fa1: Download complete 
f9a83bce3af0: Download complete 
423ae2b273f4: Pull complete 
de83a2304fa1: Pull complete 
f9a83bce3af0: Pull complete 
b6b53be908de: Pull complete 
Digest: sha256:04d48df82c938587820d7b6006f5071dbbffceb7ca01d2814f81857c631d44df
Status: Downloaded newer image for ubuntu:18.04
docker.io/library/ubuntu:18.04

如果不定製TAG,則默認會選擇latest標籤

[root@docker01 ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
8a29a15cefae: Pull complete 
Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

注:一般來說,鏡像latest標籤是會跟着鏡像內容的最新版本而變化,內容是不穩定的,因此從穩定性考慮,不要在生產環境使用latest標籤。
使用官方的Docker Hub前綴可以忽略,補全就是

docker pull registry.hub.docker.com/ubuntu:18.04

使用非官方如網易豐巢

docker pull hub.c.163.com/public/ubuntu:18.04

測試運行一個bash應用

[root@docker01 ~]# docker run -it centos:latest bash
[root@894350cbee44 /]# cat /etc/redhat-release 
CentOS Linux release 8.1.1911 (Core) 
[root@894350cbee44 /]# echo "hello world"
hello world
[root@894350cbee44 /]# exit

查看鏡像信息

列出鏡像

[root@docker01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              18.04               72300a873c2c        11 days ago         64.2MB
centos              latest              470671670cac        6 weeks ago         237MB

從以上信息可以看出
來自哪個倉庫——centos
鏡像標籤——latest
鏡像ID
創建時間
鏡像大小

添加鏡像標籤

爲方便後續工作中使用特點鏡像,爲本地鏡像添加一個新的標籤。docker tag命令起的是鏈接作用,鏈接後ID號是一樣的。

[root@docker01 ~]# docker tag centos:latest mycentos:latest
[root@docker01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              18.04               72300a873c2c        11 days ago         64.2MB
centos              latest              470671670cac        6 weeks ago         237MB
mycentos            latest              470671670cac        6 weeks ago         237MB

查看鏡像詳細信息

[root@docker01 ~]# docker  inspect centos:latest

查看鏡像歷史,過長命令會被截斷,--no-trunc選項輸出完整命令

[root@docker01 ~]# docker history centos:latest
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
470671670cac        6 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           6 weeks ago         /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
<missing>           7 weeks ago         /bin/sh -c #(nop) ADD file:aa54047c80ba30064…   237MB

搜尋鏡像

搜索官方提供帶nginx關鍵字的鏡像

[root@docker01 ~]# docker search --filter=is-official=true nginx
NAME                DESCRIPTION                STARS               OFFICIAL            AUTOMATED
nginx               Official build of Nginx.   12745               [OK]

搜索收藏數超過4個的關鍵詞爲tensorflow的鏡像

[root@docker01 ~]# docker search --filter=stars=4 tensorflow
NAME                             DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
tensorflow/tensorflow            Official Docker images for the machine learn…   1631                                    
jupyter/tensorflow-notebook      Jupyter Notebook Scientific Python Stack w/ …   201                                     
tensorflow/serving               Official images for TensorFlow Serving (http…   77                                      
xblaster/tensorflow-jupyter      Dockerized Jupyter with tensorflow              52                                      [OK]

包含鏡像名字、描述、收藏數、是否官方創建等

刪除鏡像

使用標籤刪除鏡像

[root@docker01 ~]# docker rmi mycentos:latest
Untagged: mycentos:latest

強制刪除鏡像,即使有容器依賴他

[root@docker01 ~]# docker rmi -f mycentos:latest
Untagged: mycentos:latest

使用鏡像ID來刪除鏡像

[root@docker01 ~]# docker rmi 72300a873c2c

當有鏡像創建的容器存在時,鏡像文件默認是無法被刪除的

[root@docker01 ~]# docker run myubuntu:18.04 echo 'hello~'
hello~
[root@docker01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES
8029f804939ff        centos:latest       "echo hello~"       About a minute ago   Exited (0) About a minute ago                       compassionate_wescoff
[root@docker01 ~]# docker rmi centos:latest
Error response from daemon: conflict: unable to remove repository reference "centos:latest" (must force) - container 029f804939ff is using its referenced image 470671670cac

此時可以用-f參數強制刪除,但通常不推薦這麼做,正確做法爲
刪除容器

[root@docker01 ~]# docker rm 029f804939ff
029f804939ff

刪除鏡像

[root@docker01 ~]# docker rmi 470671670cac
Untagged: centos:latest
Untagged: centos@sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
Deleted: sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee
Deleted: sha256:0683de2821778aa9546bf3d3e6944df779daba1582631b7ea3517bb36f9e4007

清理鏡像

使用docker一段時間後,系統可能會遺留一些臨時鏡像文件,和一些沒有被使用的鏡像
自動清理臨時的遺留鏡像文件層

[root@docker01 ~]# docker image prune -f
Total reclaimed space: 0B

創建鏡像

創建鏡像的方法主要有3種:基於已有的鏡像容器創建、基於本地模版導入、基於Dockerfile創建。
1、基於已有容器創建 docker [container] commit 命令
常用參數
-a,--author=" " #作者信息
-c,--change=" " #提交的時候執行Dockerfile命令,包括CMD|ENTRYPOINT|ENV|EXPOSE|LABEL|ONBUILD|USER|VOLUME|WORKDIR等
-m,--message=" " #提交信息
-p,--pause=true #提交時暫停容器運行
啓動一個鏡像,在裏面創建一個文件

[root@docker01 ~]# docker run -it ubuntu:18.04 /bin/bash
root@734364a15c55:/# touch test
root@734364a15c55:/# exit

記住容器的ID號爲734364a15c55,與原來的鏡像相比已經發生了改變,這時提交一個新的鏡像

[root@docker01 ~]# docker commit -m "add a file" -a "tcw" 734364a15c55 testfile:0.1
sha256:f14661e2c5eeaf03d14ce4ec0d9e963daeddc3c93a85de959029acf0b1278b4d

此時返回了新創建的鏡像ID信息:sha256:f14661e2c5eeaf03d14ce4ec0d9e963daeddc3c93a85de959029acf0b1278b4d

[root@docker01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
testfile            0.1                 f14661e2c5ee        5 minutes ago       64.2MB
myubuntu            18.04               72300a873c2c        11 days ago         64.2MB
ubuntu              18.04               72300a873c2c        11 days ago         64.2MB

2、基於本地模版導入
我們也可以直接從一個操作系統模版文件導入一個鏡像 docker [image] import [OPTIONS] file|URL| -[REPOSITORY [:TAG]]
這裏直接使用OpenVZ提供的模版來創建
https://wiki.openvz.org/Download/template/precreated

[root@docker01 ~]# wget http://download.openvz.org/template/precreated/ubuntu-14.04-x86_64-minimal.tar.gz
[root@docker01 ~]# cat ubuntu-14.04-x86_64-minimal.tar.gz |docker import - ubuntu:14.04
[root@docker01 oglab]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              14.04               8fc8ce69135d        36 seconds ago      215MB

3、基於Dockerfile創建
基於Dockerfile是最常見的方式,Dockerfile是一個文本文件,利用給的的指令描述基於某個父鏡像創建新鏡像的過程。
建一個python的鏡像

[root@docker01 ~]# vim Dockerfile 

     FROM debian:stretch-slim
     LABEL version="1.0" maintainer="docker user <docker_user@github>"
     RUN apt-get update && \
     apt-get install -y python3 && \
     apt-get clean && \
     rm -rf /var/lib/apt/lists/*
[root@docker01 ~]# docker build . -t python:3

存入和載入鏡像

Docker鏡像的save和load命令docker [image] save | docker [image] load來存出和載入鏡像。
1、存入鏡像

[root@docker01 ~]# docker save -o ubuntu_18.04.tar ubuntu:18.04

2、載入鏡像

[root@docker01 ~]# docker load -i ubuntu_18.04.tar 
Loaded image: ubuntu:18.04
[root@docker01 ~]# docker load < ubuntu_18.04.tar
Loaded image: ubuntu:18.04

上傳鏡像

Docker鏡像的push命令,docker [image] push上傳到鏡像倉庫,默認上傳到Docker Hub官方倉庫

docker tag testfile:0.1 user/test:latest
[root@docker01 ~]# docker push user/test:latest 
The push refers to repository [docker.io/user/test]

user是自己的用戶名,第一次上傳時會輸入登錄信息,之後登錄就會保存在本地的~/.docker目錄下

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