您的位置:首页 > 运维架构 > Docker

老李简单说Docker -- 镜像管理

2018-07-11 20:03 501 查看

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 查看镜像

[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest 05a3bd381fc2 5 weeks ago 1.84 kB

选项说明:
REPOSTITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如ubuntu仓库源里,有15.10、14.04等多个不同的版本,我们使用 REPOSTITORY:TAG 来定义不同的镜像。


1.6 搜索镜像

[root@localhost ~]# docker search centos  ##搜索方式docker search [镜像名字] (下载次数) (官方) (自动化)
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/centos The official build of CentOS. 3734 [OK]
docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 102 [OK]
docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x8... 87 [OK]
docker.io docker.io/tutum/centos Simple CentOS docker image with SSH access 33
docker.io docker.io/imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 31 [OK]
docker.io docker.io/gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glu... 20 [OK]
docker.io docker.io/kinogmt/centos-ssh CentOS with SSH 17 [OK]
docker.io docker.io/centos/php-56-centos7 Platform for building and running PHP 5.6 ... 10
docker.io docker.io/openshift/base-centos7 A Centos7 derived base image for Source-To... 10
docker.io docker.io/centos/python-35-centos7 Platform for building and running Python 3... 9
docker.io docker.io/openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6
docker.io docker.io/openshift/jenkins-2-centos7 A Centos7 based Jenkins v2.x image for use... 5
docker.io docker.io/darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
docker.io docker.io/openshift/ruby-20-centos7 DEPRECATED: A Centos7 based Ruby v2.0 imag... 3
docker.io docker.io/blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
docker.io docker.io/indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developm... 1 [OK]
docker.io docker.io/miko2u/centos6 CentOS6 日本語環境 1 [OK]
docker.io docker.io/openshift/php-55-centos7 DEPRECATED: A Centos7 based PHP v5.5 image... 1
docker.io docker.io/pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag nam... 1
docker.io docker.io/pivotaldata/centos-mingw Using the mingw toolchain to cross-compile... 1
docker.io docker.io/jameseckersall/sonarr-centos Sonarr on CentOS 7 0 [OK]
docker.io docker.io/openshift/wildfly-101-centos7 A Centos7 based WildFly v10.1 image for us... 0
docker.io docker.io/pivotaldata/centos Base centos, freshened up a little with a ... 0
docker.io docker.io/pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated ... 0
docker.io docker.io/smartentry/centos centos with smartentry 0 [OK]
docker search : 从Docker Hub查找镜像

语法

docker search [OPTIONS] TERM

选项说明:
--automated :只列出 automated build类型的镜像;
--no-trunc :显示完整的镜像描述;
-s :列出收藏数不小于指定值的镜像。

[root@localhost ~]# docker search -s 10 tomcat
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomcat is an open source implementa... 1520 [OK]
dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba... 42 [OK]
tomee Apache TomEE is an all-Apache Java EE cert... 41 [OK]
davidcaste/alpine-tomcat Apache Tomcat 7/8 using Oracle Java 7/8 wi... 20 [OK]
consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 16 [OK]
cloudesire/tomcat Tomcat server, 6/7/8 15 [OK]
[root@localhost ~]# docker search -s 10 --no-trunc tomcat
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies 1520 [OK]
dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 based docker container. 42 [OK]
tomee Apache TomEE is an all-Apache Java EE certified stack where Apache Tomcat is top dog. 41 [OK]
davidcaste/alpine-tomcat Apache Tomcat 7/8 using Oracle Java 7/8 with GLIBC 2.21 over Alpine with unlimited JCE patch applied 20 [OK]
consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 16 [OK]
cloudesire/tomcat Tomcat server, 6/7/8 15 [OK]
[root@localhost ~]# docker search --automated tomcat
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba... 42 [OK]
davidcaste/alpine-tomcat Apache Tomcat 7/8 using Oracle Java 7/8 wi... 20 [OK]
consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 16 [OK]
cloudesire/tomcat Tomcat server, 6/7/8 15 [OK]
maluuba/tomcat7 9 [OK]
andreptb/tomcat Debian Jessie based image with Apache Tomc... 8 [OK]
bitnami/tomcat Bitnami Tomcat Docker Image 5 [OK]
aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 3 [OK]
primetoninc/tomcat Apache tomcat 8.5, 8.0, 7.0 1 [OK]
camptocamp/tomcat-logback Docker image for tomcat with logback integ... 1 [OK]
antoineco/tomcat-mod_cluster Apache Tomcat with JBoss mod_cluster 1 [OK]
fabric8/tomcat-8 Fabric8 Tomcat 8 Image 0 [OK]
99taxis/tomcat7 Tomcat7 0 [OK]

1.7 下载镜像

[root@linux-node1 ~]# docker pull centos:7    ##下载指定版本镜像
7: Pulling from library/centos
af4b0a2388c6: Pull complete
Digest: sha256:2671f7a3eea36ce43609e9fe7435ade83094291055f1c96d9d1d1d7c0b986a5d
Status: Downloaded newer image for centos:7
[root@linux-node1 ~]# docker pull nginx      ##下载nginx镜像
Using default tag: latest
latest: Pulling from library/nginx
Digest: sha256:285b49d42c703fdf257d1e2422765c4ba9d3e37768d6ea83d7fe2043dad6e63d
Status: Image is up to date for nginx:latest
[root@linux-node1 ~]# docker images          ##查看现有镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   ff426288ea90        6 days ago          207MB
nginx               latest              3f8a4339aadd        2 weeks ago         108MB

1.8 导出镜像

## docker save -o [镜像名称] [镜像]

[root@linux-node1 ~]# docker save -o centos.tar.gz centos
[root@linux-node1 ~]# ls
anaconda-ks.cfg  centos.tar.gz   #需要将docker导出为tar.gz,后面为镜像名称
[root@linux-node1 ~]# docker save centos > /opt/centos.tar.gz
[root@linux-node1 ~]# ls /opt/
centos.tar.gz
[root@linux-node1 ~]# docker image save nginx >nginx.tar.gz
[root@linux-node1 ~]# du -sh nginx.tar.gz
108M	nginx.tar.gz
回到顶部

1.9 导入镜像

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


1.10 删除镜像

##docker删除可以使用docker rmi 后面加上docker的ID

[root@linux-node1 ~]# docker images            ##查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest abf312888d13 2 weeks ago 181.5 MB
docker.io/centos latest 0584b3d2cf6d 6 weeks ago 196.5 MB
[root@linux-node1 ~]# docker rmi abf312888d13  ##删除镜像 提示:如果镜像已经创建了一个容器,那么将无法进行删除
Untagged: docker.io/nginx:latest
Deleted: sha256:abf312888d132e461c61484457ee9fd0125d666672e22f972f3b8c9a0ed3f0a1
Deleted: sha256:21976f5d6f037350c076d6974b2ac97777c962869d5511855170dc5c926d5aac
Deleted: sha256:f9719c3716279b47727a51595d1482506be469c581383cb529f4005ba9bf2aeb
Deleted: sha256:fe4c16cbf7a4c70a5462654cf2c8f9f69778db280f235229bd98cf8784e878e4
[root@linux-node1 ~]# docker images            ##查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 0584b3d2cf6d 6 weeks ago 196.5 MB

1.11 显示某个镜像的历史

可以看到镜像的每一次-H :以可读的格式打印镜像大小和日期,默认为true;--no-trunc :显示完整的提交记录;-q :仅列出提交记录ID。

[root@linux-node1 ~]# docker history nginx
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
3f8a4339aadd        2 weeks ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B
<missing>           2 weeks ago         /bin/sh -c #(nop)  STOPSIGNAL [SIGTERM]         0B
<missing>           2 weeks ago         /bin/sh -c #(nop)  EXPOSE 80/tcp                0B
<missing>           2 weeks ago         /bin/sh -c ln -sf /dev/stdout /var/log/nginx…   0B
<missing>           2 weeks ago         /bin/sh -c set -x  && apt-get update  && apt…   53.2MB
<missing>           2 weeks ago         /bin/sh -c #(nop)  ENV NJS_VERSION=1.13.8.0.…   0B
<missing>           2 weeks ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.13.8-…   0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B
<missing>           4 weeks ago         /bin/sh -c #(nop) ADD file:f30a8b5b7cdc9ba33…   55.2MB

 1.12 解决镜像下载慢

[root@linux-node1 ~]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://04be47cf.m.daocloud.io
docker version >= 1.12
{"registry-mirrors": ["http://04be47cf.m.daocloud.io"]}
Success.
You need to restart docker to take effect: sudo systemctl restart docker
[root@linux-node1 ~]# systemctl restart docker
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: