一、镜像分发

1.镜像及镜像库的命名方式

指定镜像名称和标签的方法:

  • 在狗偶见镜像时
  • 通过docker tag命令
[root@bogon ~]# cd identidock/
[root@bogon identidock]# docker build -t "identidock:0.1" .
[root@bogon identidock]# docker tag "identidock:0.1" "hdlptz/identidock:0.1"
[root@bogon identidock]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hdlptz/identidock 0.1 95f825228fdb 28 seconds ago 703MB
identidock 0.1 95f825228fdb 28 seconds ago 703MB

注意:

当没有指定标签时,Docker会用latest作为默认值,但除此之外,它不具备任何特殊含义。很多仓库会把它作为最新稳定版镜像的别名,但这只是一种惯例,并非规定。你仍需要执行docker pull命令来获取最新版。

当执行docker run或docker pull时,如果指定镜像名称不带标签,那么Docker会使用带latest标签的镜像,若不存在则报错。

标签名称规则:

  • 必须由大小写字母、数字、以及.和-符号组成。
  • 长度必须为1~128个字符
  • 第一个字符不能是.或-符号

2.Docker Hub

[root@bogon identidock]# docker tag "identidock:latest" "hdlptz/identidock:0.1"
[root@bogon identidock]# docker push hdlptz/identidock:0.1

别名形式:/

3.自动构建

每当推送任何代码修改,Docker Hub就会构建新镜像,并将其保存到仓库。

自动构建是通过Hub的网页界面配置的,而不是通过命令行。

  • 点击右上角“Create”下拉菜单,选择“Create A托马特定Build”,找到你要的代码仓库
  • 选择仓库后,跳转值配置页面,仓库名称默认为代码库(可以修改)
  • 填写仓库说明
  • 第一个Tag字段为Branch,名称为master(跟踪主分支)
  • 最后点击“Create”跳转至新仓库构建页面,点击“Triger a Build”生成自动构建镜像
添加README.md
[root@bogon identidock]# cat README.md
identidock
==========
Simple identicon server based on monsterid from Kevin Gaudin. From "Using Docker" bu Adrian Mouat published by O'Reilly Media. 推送到Github
[root@bogon identidock]# git add README.md
[root@bogon identidock]# git commit -m "Added README"
[root@bogon identidock]# git push

二、私有分发

1.运行自己的寄存服务

本地运行寄存服务(使用官方镜像)
[root@bogon identidock]# docker run -d -p 5000:5000 registry:2 给镜像加标签并推送
[root@bogon identidock]# docker run -d -p 5000:5000 registry:2
9c72374fc20e044c6e0b69c0c2ac368b055832f0a39f2bdaad77e8cbd3f81c63
[root@bogon identidock]# docker tag hdlptz/identidock:0.1 localhost:5000/identidock:0.1
[root@bogon identidock]# docker push localhost:5000/identidock:0.1
The push refers to a repository [localhost:5000/identidock]
334ab24b425b: Pushed
30e2362e5771: Pushed
72811d39473b: Pushed
f6cff21775f2: Pushed
b3aa260c2eff: Pushed
2fcec228e1b7: Pushed
97d2d3bae505: Pushed
95aadeabf504: Pushed
b456afdc9996: Pushed
d752a0310ee4: Pushed
db64edce4b5b: Pushed
d5d60fc34309: Pushed
c01c63c6823d: Pushed
0.1: digest: sha256:6a35c4adb54163143af3f213e224f127d7e1b7c576e66a9b688c15b565716f2f size: 3046 测试删除后重新下载
[root@bogon identidock]# docker rmi localhost:5000/identidock:0.1
Untagged: localhost:5000/identidock:0.1
Untagged: localhost:5000/identidock@sha256:6a35c4adb54163143af3f213e224f127d7e1b7c576e66a9b688c15b565716f2f [root@bogon identidock]# docker pull localhost:5000/identidock:0.1
0.1: Pulling from identidock
Digest: sha256:6a35c4adb54163143af3f213e224f127d7e1b7c576e66a9b688c15b565716f2f
Status: Downloaded newer image for localhost:5000/identidock:0.1

寄存器为镜像生成一个摘要值(digest),这是基于镜像和它的元数据产生的一个唯一的散列值。

可使用这个值来下载镜像

[root@bogon identidock]# docker pull localhost:5000/identidock@sha256:\
> 6a35c4adb54163143af3f213e224f127d7e1b7c576e66a9b688c15b565716f2f
sha256:6a35c4adb54163143af3f213e224f127d7e1b7c576e66a9b688c15b565716f2f: Pulling from identidock
Digest: sha256:6a35c4adb54163143af3f213e224f127d7e1b7c576e66a9b688c15b565716f2f
Status: Image is up to date for localhost:5000/identidock@sha256:6a35c4adb54163143af3f213e224f127d7e1b7c576e66a9b688c15b565716f2f

使用摘要的优点是:保证下载的镜像确实是你想要的,并保证完整性。

2.远程使用寄存服务器存在的问题

(1)直接调用时会报错

[root@bogon identidock]# docker pull 192.168.1.100:5000/identidock:0.1

报错原因:

当Docker守护进程拒绝连接到远程主机时,因为它没有一个有效的传输层安全证书(Transport Layer Security,TLS)。

解决方法:

A.对将要访问寄存器的所有Docker守护进程加上--insecure-registry 192.168.1.100:5000参数,然后重启Docker守护进程;

B.在主机上安装一个来自可信的证书颁发机构签署的证书(如HTTPS访问那样);

C.在主机上安装一个自签名的证书(self-signed certificate),并同时给需要访问寄存服务器的每个Docker守护进程都安装一份。

(2)创建自签证书

在一台长期用作寄存服务器的电脑上进行

[root@bogon ~]# mkdir registry_certs

[root@bogon ~]# openssl req -newkey rsa:4096 -nodes -sha256 \
> -keyout registry_certs/domain.key -x509 -days 365 \
> -out registry_certs/domain.crt Generating a 4096 bit RSA private key
..........................................................................................++
.......................++
writing new private key to 'registry_certs/domain.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:China
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:Anzhi
Organizational Unit Name (eg, section) []:Autometa
Common Name (eg, your name or your server's hostname) []:bogon
Email Address []:hdlptz@163.com [root@bogon ~]# ls registry_certs/
domain.crt domain.key 之后将证书复制到每一台客户端
[root@bogon ~]# mkdir -p /etc/docker/certs.d/bogon:5000
[root@bogon ~]# cp registry_certs/domain.crt /etc/docker/certs.d/bogon:5000/ca.crt
[root@bogon ~]# /bin/systemctl restart docker 启动寄存服务(以数据卷方式把证书放入)
[root@bogon ~]# docker run -d -p 5000:5000 \
> -v $(pwd)/registry_certs:/certs \
> -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt \
> -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key \
> --restart=always --name registry registry:2
71d621a877b749a5f9b8dc798e78abf65faeebc2232dfa7e73dfc282d2608a0c

说明:

  • 创建一个x509的自签证书和4096位的RSA私钥,此证书使用SHA256的摘要签署,有效期365天
  • Common Name必须与用来访问的服务器名字相同,不可使用IP地址
  • crt证书与各个客户端共用,key密钥必须严密保存

测试寄存服务:

[root@bogon ~]# docker pull debian:wheezy
wheezy: Pulling from library/debian
39e552a2b1f7: Pull complete
Digest: sha256:e2a7ec190cf20118aa2a958c92cb9467d89b112a430848d3ff08461b60a0957f
Status: Downloaded newer image for debian:wheezy [root@bogon ~]# docker tag debian:wheezy bogon:5000/debian:local [root@bogon ~]# docker push bogon:5000/debian:local
The push refers to a repository [bogon:5000/debian]
dd7b8d3c2585: Pushed
local: digest: sha256:2118c3e3e3b047084bf6716f63958594111078f257ee91fd52612b3c6bdbd71d size: 529

注意以下两点:

  • 将证书复制到Docker引擎主机上的/etc/docker/certs.d/<registry_address>/ca.crt
  • 确保Docker引擎能够解析寄存服务器地址
  • 寄存服务的选项由镜像中的一个YAML文件配置,可通过数据卷将其替换,/go/src/github.com/docker/distribution/cmd/registry/config.yml,该文件仅适用于开发环境,若要用于生产,需进行大幅度优化

3.寄存服务的主要功能

(1)存储

寄存服务器的镜像默认使用文件系统驱动,所有数据和镜像将会保存在文件系统上。

例如:以下的config.yml将配置寄存服务使用文件系统驱动,并将数据保存在/var/lib/registry,而且要把它定义为一个数据卷。

storage:
filesystem:
rootdirectory: /var/lib/registry

另外,还可以用Ceph分布式对象存储,利用Redis作为内存缓存来加速镜像层的访问效率。

(2)身份验证

A.在寄存服务之前设置一个代理(nginx)负责验证用户。

https://docs.docker.com/registry/recipes/nginx

可使用docker login登录

B.使用基于JSON网络令牌(JSON Web Token)实现的令牌认证。

https://github.com/cesanta/docker_auth

(3)HTTP

配置寄存服务的HTTP接口

http:
addr: bogon:5000
secret: SAI4351-ALD346-3434-3DFG-23ADG341DF43
tls:
certificate: /certs/domain.crt
key: /certs/domain.key

secert:用来签署客户端存储的状态信息的一个随机字符串,为了防止信息被篡改。

三、缩减镜像大小

镜像是由多个层所组成的,每个镜像对应Dockerfile以及其上的所有Dockerfile的每一个命令。镜像的总大小是所有镜像层的总和。

对比两个镜像:

[root@bogon test]# cat Dockerfile
FROM debian:wheezy RUN dd if=/dev/zero of=/bigfile count=1 bs=50MB
RUN rm /bigfile
[root@bogon test]# docker build -t filetest .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM debian:wheezy
---> f47fe1c60a2f
Step 2/3 : RUN dd if=/dev/zero of=/bigfile count=1 bs=50MB
---> Running in d33340bf5012
1+0 records in
1+0 records out
50000000 bytes (50 MB) copied, 0.555286 s, 90.0 MB/s
---> 0f33a1416b1e
Removing intermediate container d33340bf5012
Step 3/3 : RUN rm /bigfile
---> Running in c00c9b9d05be
---> 7829ebda71e6
Removing intermediate container c00c9b9d05be
Successfully built 7829ebda71e6
Successfully tagged filetest:latest
[root@bogon test]# docker history filetest
IMAGE CREATED CREATED BY SIZE COMMENT
7829ebda71e6 13 seconds ago /bin/sh -c rm /bigfile 0B
0f33a1416b1e 15 seconds ago /bin/sh -c dd if=/dev/zero of=/bigfile cou... 50MB
f47fe1c60a2f Less than a second ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> Less than a second ago /bin/sh -c #(nop) ADD file:4a0b4ab0f637224... 85.1MB

可以看到,镜像比基础镜像大了50MB,而rm删除的文件并没有对大小有所影响,这是因为rm在dd之上又添加了一个镜像层,这是又Dockerfile文件所决定,因此对父层并没有影响。

[root@bogon test]# cat Dockerfile
FROM debian:wheezy RUN dd if=/dev/zero of=/bigfile count=1 bs=50MB && rm /bigfile
[root@bogon test]# docker build -t filetest .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM debian:wheezy
---> f47fe1c60a2f
Step 2/2 : RUN dd if=/dev/zero of=/bigfile count=1 bs=50MB && rm /bigfile
---> Running in 8714793afce3
1+0 records in
1+0 records out
50000000 bytes (50 MB) copied, 0.247367 s, 202 MB/s
---> e9cab407d89b
Removing intermediate container 8714793afce3
Successfully built e9cab407d89b
Successfully tagged filetest:latest
[root@bogon test]# docker images filetest
REPOSITORY TAG IMAGE ID CREATED SIZE
filetest latest e9cab407d89b 11 seconds ago 85.1MB
[root@bogon test]# docker history filetest
IMAGE CREATED CREATED BY SIZE COMMENT
e9cab407d89b 21 seconds ago /bin/sh -c dd if=/dev/zero of=/bigfile cou... 0B
f47fe1c60a2f Less than a second ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> Less than a second ago /bin/sh -c #(nop) ADD file:4a0b4ab0f637224... 85.1MB

我们修改了Dockerfile文件,使之在同一镜像层中创建文件后再删除,则文件不会被包含在镜像中。

其他示例:

RUN apt-get upfate \
&& apt-get install -y curl numactl \
&& rm -rf /var/lib/apt/lists/*

使用docker export及docker import会得到只含有一个层的镜像

[root@bogon test]# docker create identidock:latest
427acbf9d8be57bfa9e3d97320228e86d85d0e9128130c052407fc641c08692a [root@bogon test]# docker export $(docker ps -lq) |docker import -
sha256:09ca0e0f1f6b5a610e0b1e12c9c189a8ee1e7b504f01aa2d2468bab7ea8e9c8b [root@bogon test]# docker tag 09ca0e0 identidock:import [root@bogon test]# docker images identidock
REPOSITORY TAG IMAGE ID CREATED SIZE
identidock import 09ca0e0f1f6b About a minute ago 686MB
identidock 0.1 95f825228fdb 2 hours ago 703MB
identidock latest 3966bef511e5 5 hours ago 699MB [root@bogon test]# docker history identidock:import
IMAGE CREATED CREATED BY SIZE COMMENT
09ca0e0f1f6b 2 minutes ago 686MB Imported from -

缺点:

  • 需要重新处理所有未反映在文件系统里的Dockerfile指令(EXPOSE、CMD)
  • 与镜像关联的所有元数据将会丢失
  • 再也不能与其他具有相同父层的镜像共享空间

Docker容器技术-镜像分发的更多相关文章

  1. 【Docker】(9)---每天5分钟玩转 Docker 容器技术之镜像

    镜像是 Docker 容器的基石,容器是镜像的运行实例,有了镜像才能启动容器.为什么我们要讨论镜像的内部结构? 如果只是使用镜像,当然不需要了解,直接通过 docker 命令下载和运行就可以了. 但如 ...

  2. Linux 运维工作中的经典应用ansible(批量管理)Docker容器技术(环境的快速搭建)

    一 Ansible自动化运维工具 Python 在运维工作中的经典应用 ansible(批量管理操作) .安装ansible(需要bese epel 2种源) wget -O /etc/yum.rep ...

  3. 【转帖】一文看懂docker容器技术架构及其中的各个模块

    一文看懂docker容器技术架构及其中的各个模块 原创 波波说运维 2019-09-29 00:01:00 https://www.toutiao.com/a6740234030798602763/ ...

  4. docker容器技术基础入门

    目录 docker容器技术基础入门 容器(Container) 传统虚拟化与容器的区别 Linux容器技术 Linux Namespaces CGroups LXC docker基本概念 docker ...

  5. 理解docker容器和镜像(layer,ufs)和docker命令解释

    博客好文1:http://blog.csdn.net/x931100537/article/details/49633107(理解docker容器和镜像,理解简单,从原理入手,什么是layer,什么是 ...

  6. 微服务架构:基于微服务和Docker容器技术的PaaS云平台架构设计(微服务架构实施原理)

    版权声明:本文为博主原创文章,转载请注明出处,欢迎交流学习! 基于微服务架构和Docker容器技术的PaaS云平台建设目标是给我们的开发人员提供一套服务快速开发.部署.运维管理.持续开发持续集成的流程 ...

  7. Weave Scope 容器地图 - 每天5分钟玩转 Docker 容器技术(80)

    Weave Scope 的最大特点是会自动生成一张 Docker 容器地图,让我们能够直观地理解.监控和控制容器.千言万语不及一张图,先感受一下. 下面开始实践 Weave Scope. 安装 执行如 ...

  8. Prometheus 到底 NB 在哪里?- 每天5分钟玩转 Docker 容器技术(84)

    本节讨论 Prometheus 的核心,多维数据模型.我们先来看一个例子. 比如要监控容器 webapp1 的内存使用情况,最传统和典型的方法是定义一个指标 container_memory_usag ...

  9. Docker Swarm 中最重要的概念- 每天5分钟玩转 Docker 容器技术(94)

    从主机的层面来看,Docker Swarm 管理的是 Docker Host 集群.所以先来讨论一个重要的概念 - 集群化(Clustering). 服务器集群由一组网络上相互连接的服务器组成,它们一 ...

随机推荐

  1. Erlang 和 Elixir 互相调用 (转)

    lixr设计目标之一就是要确保兼容性,可以兼容Erlang和其生态系统.Elixir和Erlang 都是运行同样的虚拟机平台(Erlang Virtual Machine).不管是在Erlang使用E ...

  2. Photoshop脚本之调试

    系统:mac 创建test.scpt和test.jsx command+空格,打开 脚本编辑器(applescript) 脚本编辑器打开test.scpt 输入: tell application & ...

  3. python 动态语言 __slots__

    python 是动态语言,就是说可以动态的创建属性, 别的语言不行,再创建类的时候已经规定好了 使用__slots__,注意要用tuple定义同意绑定的属性名称,仅对当前类起作用,对继承的子类是不起作 ...

  4. dm8148 开发之---sii9022a hdmi传输器

    SiI9022A -HDMI 发送器    照相机.摄影机和便携式媒体播放器的高清解决方案   SiI9022a是一款超低功耗的HDMI发送器,集成度更高, 电源管理特性也更强,适用于手提式消费电子设 ...

  5. PPT常用操作笔记

    1. 组合就是框成一组 2. 页与页之间的播放运作及效果设置“切换”,页内的元素过滤设置“动画” 3. 转PDF:PDF打印机或直接来个金山WPS 4. 讲义打印,要点:灰度.去深色背景.多页,弄了个 ...

  6. git Staging Deleted files

    Use git rm foo to stage the file for deletion. (This will also delete the file from the file system, ...

  7. php 佛祖保佑 永无bug

    <pre name="code" class="java">/* _ooOoo_ o8888888o 88" . "88 (| ...

  8. WPF数据模板样式选择器

    在使用数据模板样式选择器时,不能设置ItemContainerStyle的属性值,如果设置了该值,那么数据模板样式选择器会失去作用. 在使用数据模板样式选择器时,首先要创建数据模板样式选择器对象,此对 ...

  9. Android UI开发第二十七篇——实现左右划出菜单

    年前就想写左右滑动菜单,苦于没有时间,一直拖到现在,这篇代码实现参考了网上流行的SlidingMenu,使用的FrameLayout布局,不是扩展的HorizontalScrollView. 程序中自 ...

  10. 《从零开始学Swift》学习笔记(Day 45)——重写方法

    原创文章,欢迎转载.转载请注明:关东升的博客 重写实例方法 在子类中重写从父类继承来的实例方法和静态方法.先介绍实例方法的重写. 下面看一个示例: class Person { var name: S ...