Docker入门与应用系列(二)镜像管理

1.1 什么是镜像

  简单说,Docker镜像是一个不包含Linux内核而又精简的Linux操作系统。

1.2 镜像从哪里来

  Docker Hub是由Docker公司负责维护的公共注册中心,包含大量的容器镜像,Docker工具默认从这个公共镜像库下载镜像。

https://hub.docker.com/explore

  默认是国外的源,下载会慢,可以国内的源提供下载速度:

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进入此目录。如果运行中的容器修改一个已经存在的文件,那么会将该文件从下面的只读层复制到读写层,只读层的这个文件就会覆盖,但还存在,这就实现了文件系统隔离,当删除容器后,读写层的数据将会删除,只读镜像不变。

1.4 镜像文件存储结构

docker相关文件存放在:/var/lib/docker目录下

/var/lib/docker/aufs/diff # 每层与其父层之间的文件差异

/var/lib/docker/aufs/layers/ # 每层一个文件,记录其父层一直到根层之间的ID

/var/lib/docker/aufs/mnt  # 联合挂载点,从只读层复制到最上层可读写层的文件系统数据

在建立镜像时,每次写操作,都被视作一种增量操作,即在原有的数据层上添加一个新层;所以一个镜像会有若干个层组成。

每次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.10、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

  

posted @ 2018-02-24 10:54  Bigberg  阅读(999)  评论(0编辑  收藏  举报