Docker系列3:Docker镜像详解

1、Docker镜像介绍
html

一、镜像组成介绍nginx

    image.png

分层构建的,底层的是bootfs,上面的是rootfsweb

  • bootfs的类型能够是btrfs、aufs、lxc,还须要一个内核,可是这个内核仅仅是用于启动容器的用户控件的docker

  • rootfs表现为一个根文件系统,这里面包括不少的文件和目录apache

  • 在启动容器的时候,这两层都是以只读的方式来挂载的。json

构建镜像的基本流程vim

  1. 先准备一个bootfsbash

  2. 而后安装一个最小系统(base image)服务器

  3. 在系统安装应用,若是是构建apache的镜像,那么就在base image上安装apachecurl

    image.png

注意:

  • 镜像都是只读的

  • 当启动容器的时候,会在镜像的基础上再添加一个当前容器的专用层,这层是读写的

  • 在删除容器的时候,这个容器的专属的这个读写层就会被删除,因此默认容器没法实现数据的持久存储。

二、镜像仓库

    前面一讲过了,专门用来存储docker iamge的哪一个位置称之为 docker registry,在启动容器的时候,本地的docker daemon会从指定的docker registry下载镜像,并完成启动。

docker registry是能够分为多类 的

  • Vendor registry:官方的仓库

  • Mirror registry:像阿里云之类的镜像加速

  • Private registry:用户本身建立镜像仓库,好比企业内部须要大规模部署时候,能够本身定制镜像,并放到本身仓库中

通常的registry有两部分组成:

第一部分:Repository

  • 一个registry能够有多个repository

  • Repository能够分为顶级仓库和用户仓库,用户仓库的命名是:用户名/仓库名

  • Repository的名称通常就是应用的名称,并且在Repository中有应用的多个版本

第二部分:index

  • 维护帐户信息

  • 提供检索端口

三、从镜像仓库下载镜像的方法

格式以下

docker pull <registry>[:port] /[<namespace>/]<name>:<tag>
  • registry:port 这里是指定从哪一个docker服务器来获取镜像,若是用的是docker官方本身的仓库(hub.docker.com),那么这里能够省略。

  • namespace 这里是指定来自于哪一个名称空间,也就是哪一个用户的仓库, 若是的顶级的仓库,那么这个也能够省略

除了https://hub.docker.com以后,其实还有别的,例如:https://hub.daocloud.io/,再例如CoreOS所维护的:https://quay.io

  • 由于不是默认仓库, 所以在下载镜像的时候,须要指定地址


从quay.io 下载 flannel举例以下

第一步:登陆https://quay.io,搜索flannel

image.png

第二步:找到项目地址

image.png

第三步:查看下载镜像的方法

image.png

  • 这种方法是不能用的,由于须要指定标签

第四步:查看具体的标签

image.pngimage.png

image.png

第五步:下载镜像

[root@host1 ~]# docker pull quay.io/coreos/flannel:v0.11.0-s390x
[root@host1 ~]# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
busybox                  latest              b534869c81f0        2 weeks ago         1.22MB
nginx                    1.14-alpine         8a2fb25a19f5        8 months ago        16MB
quay.io/coreos/flannel   v0.10.0-s390x       463654e4ed2d        23 months ago       47MB


2、制做镜像

一、制做镜像方法种类

  • 基于dockerfile

  • 基于现有的容器:基于容器作镜像,通常就是先安装一个最小容器,而后在这个容器中安装应用程序,而后将这个安装了程序的容器作成镜像就能够了

  • 基于docker hub的自动建立功能

二、基于现有容器作镜像

第一步:启动一个busybox容器,并建立一个html页面

[root@host1 ~]# docker run --name img1 -it busybox
/ # mkdir /data/html -p
/ # echo "test page[v1.0]">>/data/html/index.html

第二步:再开一个终端,将容器制做成镜像

  • 制做镜像用命令commit

  • 要制做镜像的容器不能中止

  • 将容器制做为镜像的时候,最好让容器暂停一下,这须要用选项-p

  • 默认制做的镜像没有tag,也不属于任何的repository

[root@host1 ~]# docker commit -p img1
sha256:cd7cb2a774400c721ed71f62bd20abe2c000f1d0f7d51d3bf025db1239b86b7d
[root@host1 ~]# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
<none>                   <none>              cd7cb2a77440        6 seconds ago       1.22MB

第三步:给镜像打标签

  • 打标签用tag命令

  • 一个镜像能够有多个不一样的标签

[root@host1 ~]# docker tag cd7cb2a77440 zxhk/httpd:v1-0
[root@host1 ~]# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
zxhk/httpd               v1-0                cd7cb2a77440        2 minutes ago       1.22MB

再打个标签

[root@host1 ~]# docker tag cd7cb2a77440 zxhk/httpd:latest
[root@host1 ~]# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
zxhk/httpd               latest              cd7cb2a77440        3 minutes ago       1.22MB
zxhk/httpd               v1-0                cd7cb2a77440        3 minutes ago       1.22MB

注意:

  • 一个镜像有多个标签的话,如何进行删除,须要将这多个镜像都删除才行,相似于系统的硬连接

第四步:基于这个进行启动一个容器,并在容器中运行apache

[root@host1 ~]# docker run --name newhttpd -it zxhk/httpd:latest
/ # httpd -f -h /data/html
  • 此时,而后apache能运行,可是每次新启动一个容器,都需手动启动apache,接下来对镜像进行调整,实现启动容器后自动运行apache

第五步:升级镜像实现自动运行内部的apache

先看看咱们作的镜像的详细信息

[root@host1 ~]# docker inspect zxhk/httpd:latest

其中有一部分是Cmd,其中就是容器运行起来之后要执行的命令,以下

"Cmd": [
      "sh"
],

commit建立镜像的时候会能够经过选项来设置这些内容

  • -a:指定做者

  • -c:更改基于镜像启动后执行的命令

  • -m:描述系想你

  • -p:暂停

再从新作个镜像

[root@host1 ~]# docker commit \
> -a "zxhk<237745635@qq.com>" \
> -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' \
> -p img3 zxhk/httpd:v2.0

用这个镜像启动一个容器

[root@host1 ~]# docker run --rm --name test-httpd zxhk/httpd:v2.0

看一下容器中执行的命令

[root@host1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
243f050288bd        zxhk/httpd:v2.0     "/bin/httpd -f -h /d…"   16 seconds ago      Up 15 seconds                           test-httpd

看一下地址信息

[root@host1 ~]# docker inspect 243 | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.4",
                    "IPAddress": "172.17.0.4",

在宿主机访问测试容器中的站点

[root@host1 ~]# curl 172.17.0.4
test page[v1.0]

至此,镜像建立完成

3、将制做的镜像上传到docker hub中

一、在https://hub.docker.com/注册用户

  • 须要爬过墙头才能注册,你懂的!!!

  • 注册帐户过程-略

二、在docker hub上建立repository和registry

  image.png

  image.png

   image.png 

注意:

  • 建立的仓库名称必需要和镜像的名称一致

三、向本身的仓库中上传镜像文件

第一步:登录docker hub【个人用户名是zxhk】

[root@localhost ~]# docker login -uzxhk
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

第二步:上传镜像到hub【此处咱们上传httpd镜像的二个版本都传上去】

[root@localhost ~]# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
zxhk/httpd               v2.0                89a647171235        18 hours ago        1.22MB
zxhk/httpd               latest              cd7cb2a77440        19 hours ago        1.22MB
zxhk/httpd               v1-0                cd7cb2a77440        19 hours ago        1.22MB
[root@localhost ~]# docker push zxhk/httpd:v2.0
The push refers to repository [docker.io/zxhk/httpd]
f577c88ef366: Pushed 
eac247cb7af5: Mounted from library/busybox 
v2.0: digest: sha256:c1c3e604e37652595563b8dc2be877620c77314c925115c7ba35f9969b1a77a0 size: 734
[root@localhost ~]# docker push zxhk/httpd:v1-0

第三步:在docker hub上查看一下

image.png


第四步:使用docker hub中咱们本身的镜像

在docker hub中已经标识了镜像的使用方法,以下:

image.png

为了效果,现将本地的镜像删除

[root@localhost ~]# docker rmi 89 zxhk/httpd:v1-0
[root@localhost ~]# docker rmi 89 zxhk/httpd:v2.0
[root@localhost ~]# docker image ls 
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
busybox                  latest              b534869c81f0        2 weeks ago         1.22MB

下载镜像启动容器

[root@localhost ~]# docker pull zxhk/httpd:v2.0

[root@localhost ~]# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
zxhk/httpd               v2.0                89a647171235        19 hours ago        1.22MB
busybox                  latest              b534869c81f0        2 weeks ago         1.22MB

[root@localhost ~]# docker run --rm --name web1 89a

查看一下容器的信息

[root@localhost ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
0ec8687bb487        89a                 "/bin/httpd -f -h /d…"   16 seconds ago      Up 15 seconds                           web1
[root@localhost ~]# docker inspect 0ec | grep "IPAddr"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",
[root@localhost ~]# curl 172.17.0.2
test page[v1.0]

4、将制做的镜像上传到阿里云的镜像仓库中

一、在阿里云注册用户

    略

二、进入容器镜像仓库

    image.png

    image.png

    image.png

三、使用阿里云作镜像加速的方法

    image.png

去docker配置文件中添加一个镜像文件

[root@localhost ~]# vim /etc/docker/daemon.json
{
    "registry-mirrors": [
        "https://registry.docker-cn.com",
        "https://mzxx8xy8.mirror.aliyuncs.com"
    ]
}

重启服务

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker

四、使用阿里云建立仓库

    image.png

    image.png

    image.png

    image.png

看看镜像仓库的使用方法

    image.png

五、向阿里云仓库上传镜像

第一步:使用凭证登陆阿里云

[root@localhost ~]# sudo docker login --username=zxhk registry.cn-hangzhou.aliyuncs.com
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded

第二步:上传镜像

  • 上传到阿里云的镜像须要进行先打个标签

[root@localhost ~]# docker tag 89a registry.cn-hangzhou.aliyuncs.com/zxhk1/httpd:v2.0
[root@localhost ~]# docker push registry.cn-hangzhou.aliyuncs.com/zxhk1/httpd:v2.0

第三步:从阿里云拉取镜像

[root@localhost ~]# sudo docker pull registry.cn-hangzhou.aliyuncs.com/zxhk1/httpd:v2.0
相关文章
相关标签/搜索