Docker入門與應用系列(二)鏡像管理

1.1 什麼是鏡像

  簡單說,Docker鏡像是一個不包含Linux內核而又精簡的Linux操做系統。php

1.2 鏡像從哪裏來

  Docker Hub是由Docker公司負責維護的公共註冊中心,包含大量的容器鏡像,Docker工具默認從這個公共鏡像庫下載鏡像。html

https://hub.docker.com/explorenode

  默認是國外的源,下載會慢,能夠國內的源提供下載速度:python

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://04be47cf.m.daocloud.io

1.3 鏡像工做的原理  

  當咱們啓動一個新的容器時,Docker會加載只讀鏡像,並在其之上添加一個讀寫層,並將鏡像中的目錄複製一份到/var/lib/docker/aufs/mnt/容器ID爲目錄下,咱們可使用chroot進入此目錄。若是運行中的容器修改一個已經存在的文件,那麼會將該文件從下面的只讀層複製到讀寫層,只讀層的這個文件就會覆蓋,但還存在,這就實現了文件系統隔離,當刪除容器後,讀寫層的數據將會刪除,只讀鏡像不變。mysql

1.4 鏡像文件存儲結構

docker相關文件存放在:/var/lib/docker目錄下linux

/var/lib/docker/aufs/diff # 每層與其父層之間的文件差別nginx

/var/lib/docker/aufs/layers/ # 每層一個文件,記錄其父層一直到根層之間的IDsql

/var/lib/docker/aufs/mnt  # 聯合掛載點,從只讀層複製到最上層可讀寫層的文件系統數據docker

在創建鏡像時,每次寫操做,都被視做一種增量操做,即在原有的數據層上添加一個新層;因此一個鏡像會有若干個層組成。ubuntu

每次commit提交就會對產生一個ID,就至關於在上一層有加了一層,能夠經過這個ID對鏡像回滾

1.5 查看鏡像

  docker images

[bigberg@localhost ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              f2a91732366c        3 months ago        1.85kB

選項說明:
REPOSTITORY:表示鏡像的倉庫源
TAG:鏡像的標籤
IMAGE ID:鏡像ID
CREATED:鏡像建立時間
SIZE:鏡像大小
同一倉庫源能夠有多個 TAG,表明這個倉庫源的不一樣個版本,如ubuntu倉庫源裏,有15.十、14.04等多個不一樣的版本,咱們使用 REPOSTITORY:TAG 來定義不一樣的鏡像。

1.6 搜索鏡像

語法:

docker search [OPTIONS] TERM

選項說明:
--automated :只列出 automated build類型的鏡像;
--no-trunc :顯示完整的鏡像描述;
-s :列出收藏數不小於指定值的鏡像。

[bigberg@localhost ~]$ docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   4051                [OK]                
ansible/centos7-ansible            Ansible on Centos7                              105                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x86_…   91                                      [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   45                                      [OK]
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              38                                      [OK]
tutum/centos                       Simple CentOS docker image with SSH access      36                                      
gluster/gluster-centos             Official GlusterFS Image [ CentOS-7 +  Glust…   22                                      [OK]
centos/python-35-centos7           Platform for building and running Python 3.5…   18                                      
openshift/base-centos7             A Centos7 derived base image for Source-To-I…   17                                      
kinogmt/centos-ssh                 CentOS with SSH                                 17                                      [OK]
openshift/jenkins-2-centos7        A Centos7 based Jenkins v2.x image for use w…   10                                      
centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational …   7                                       
openshift/mysql-55-centos7         DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   3                                       
darksheer/centos                   Base Centos Image -- Updated hourly             3                                       [OK]
openshift/jenkins-1-centos7        DEPRECATED: A Centos7 based Jenkins v1.x ima…   3                                       
openshift/wildfly-101-centos7      A Centos7 based WildFly v10.1 image for use …   3                                       
pivotaldata/centos                 Base centos, freshened up a little with a Do…   1                                       
miko2u/centos6                     CentOS6 日本語環境                                   1                                       [OK]
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   1                                       
openshift/php-55-centos7           DEPRECATED: A Centos7 based PHP v5.5 image f…   1                                       
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                                       [OK]
jameseckersall/sonarr-centos       Sonarr on CentOS 7                              0                                       [OK]
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   0                                       
smartentry/centos                  centos with smartentry                          0                                       [OK]

1.7 下載鏡像

1.7.1 解決鏡像下載慢的問題

  咱們可使用 Docker 鏡像加速器來解決這個問題,加速器就是鏡像、代理的概念。國內有很多機構提供了免費的加速器以方便你們使用,這裏列出一些經常使用的加速器服務:

  Ubuntu 16.04 和 CentOS 7 這類系統都已經開始使用 systemd 進行系統初始化管理了,對於使用 systemd 的系統,應該經過編輯服務配置文件 docker.service 來進行加速器的配置。 

sudo vim /etc/systemd/system/multi-user.target.wants/docker.service

#在文件中找到 ExecStart= 這一行,而且在其行尾添加上所需的配置。假設咱們的加速器地址爲 https://registry.docker-cn.com,那麼能夠這樣配置

ExecStart=/usr/bin/dockerd --registry-mirror=https://registry.docker-cn.com

sudo systemctl daemon-reload
sudo systemctl restart docker

# 確認配置
sudo ps -ef | grep dockerd
[bigberg@localhost ~]$ ps -ef | grep docker
root       4273      1  1 10:04 ?        00:00:08 /usr/bin/dockerd --registry-mirror=https://registry.docker-cn.com

1.7.2 獲取鏡像

[bigberg@localhost ~]$ docker pull centos:7
7: Pulling from library/centos
af4b0a2388c6: Pull complete 
Digest: sha256:2671f7a3eea36ce43609e9fe7435ade83094291055f1c96d9d1d1d7c0b986a5d
Status: Downloaded newer image for centos:7

  

1.8 查看鏡像

[bigberg@localhost ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              e548f1a579cf        3 days ago          109MB
centos              7                   ff426288ea90        6 weeks ago         207MB
hello-world         latest              f2a91732366c        3 months ago        1.85kB

1.9 導出鏡像

[bigberg@localhost ~]$ docker save -o centos.tar.gz centos

[bigberg@localhost ~]$ docker save  nginx > nginx.tar.gz

[bigberg@localhost ~]$ docker image save hello-world > hello.tar.gz



[bigberg@localhost ~]$ ll
total 320480
-rw------- 1 bigberg bigberg 215760384 Feb 24 10:38 centos.tar.gz
-rw-rw-r-- 1 bigberg bigberg     12800 Feb 24 10:40 hello.tar.gz
-rw-rw-r-- 1 bigberg bigberg 112393728 Feb 24 10:39 nginx.tar.gz

 

1.10 導入鏡像

[root@linux-node1 ~]#docker load --input centos.tar.gz
#使用input導入
[root@linux-node1 ~]# docker load < nginx.tar.gz #使用重定向導入
[root@linux-node1 ~]# docker load < /opt/centos.tar.gz #導入本地鏡像到docker鏡像庫

  

1.11 刪除鏡像

# docker rmi TMAGE ID

[bigberg@localhost ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              e548f1a579cf        3 days ago          109MB
centos              7                   ff426288ea90        6 weeks ago         207MB
hello-world         latest              f2a91732366c        3 months ago        1.85kB
[bigberg@localhost ~]$ docker rmi f2a91732366c
Untagged: hello-world:latest
Untagged: hello-world@sha256:66ef312bbac49c39a89aa9bcc3cb4f3c9e7de3788c944158df3ee0176d32b751
Deleted: sha256:f2a91732366c0332ccd7afd2a5c4ff2b9af81f549370f7a19acd460f87686bc7
Deleted: sha256:f999ae22f308fea973e5a25b57699b5daf6b0f1150ac2a5c2ea9d7fecee50fdf
[bigberg@localhost ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              e548f1a579cf        3 days ago          109MB
centos              7                   ff426288ea90        6 weeks ago         207MB

  

1.12 顯示某個鏡像的歷史

能夠看到鏡像的每一次

-H :以可讀的格式打印鏡像大小和日期,默認爲true;

--no-trunc :顯示完整的提交記錄;

-q :僅列出提交記錄ID。

[bigberg@localhost ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              e548f1a579cf        3 days ago          109MB
centos              7                   ff426288ea90        6 weeks ago         207MB
[bigberg@localhost ~]$ docker -H nginx
^C
[bigberg@localhost ~]$ docker history nginx
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
e548f1a579cf        3 days ago          /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           3 days ago          /bin/sh -c #(nop)  STOPSIGNAL [SIGTERM]         0B                  
<missing>           3 days ago          /bin/sh -c #(nop)  EXPOSE 80/tcp                0B                  
<missing>           3 days ago          /bin/sh -c ln -sf /dev/stdout /var/log/nginx…   22B                 
<missing>           3 days ago          /bin/sh -c set -x  && apt-get update  && apt…   53.4MB              
<missing>           3 days ago          /bin/sh -c #(nop)  ENV NJS_VERSION=1.13.9.0.…   0B                  
<missing>           3 days ago          /bin/sh -c #(nop)  ENV NGINX_VERSION=1.13.9-…   0B                  
<missing>           6 days ago          /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
<missing>           9 days ago          /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           9 days ago          /bin/sh -c #(nop) ADD file:27ffb1ef53bfa3b9f…   55.3MB
相關文章
相關標籤/搜索