IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> docker镜像管理基础 -> 正文阅读

[系统运维]docker镜像管理基础

docker镜像管理基础

镜像的概念

镜像可以理解为应用程序的集装箱,而docker用来装卸集装箱。

docker镜像含有启动容器所需要的文件系统及其内容,因此,其用于创建并启动容器。

docker镜像采用分层构建机制,最底层为bootfs,其上为rootfs

  • bootfs:用于系统引导的文件系统,包括bootloader和kernel,容器启动完成后会被卸载以节约内存资源
  • rootfs:位于bootfs之上,表现为docker容器的根文件系统
    • 传统模式中,系统启动之时,内核挂载rootfs会首先将其挂载为“只读”模式,完整性自检完成后将其重新挂载为读写模式
    • docker中,rootfs由内核挂载为“只读”模式,而后通过“联合挂载”技术额外挂载一个“可写”层

注意:当删除容器时,这个容器自有的“可写”层会一起被删除

img

docker镜像层

img

位于下层的镜像称为父镜像(parrent image),最底层的称为基础镜像(base image);
最上层为“可读写”层,其下的均为“只读”层。

docker存储驱动

docker提供了多种存储驱动来实现不同的方式存储镜像,下面是常用的几种存储驱动:

  • AUFS
  • OverlayFS
  • Devicemapper
  • Btrfs
  • VFS

AUFS

AUFS(AnotherUnionFS)是一种Union FS,是文件级的存储驱动。AUFS是一个能透明覆盖一个或多个现有文件系统的层状文件系统,把多层合并成文件系统的单层表示。简单来说就是支持将不同目录挂载到同一个虚拟文件系统下的文件系统。这种文件系统可以一层一层地叠加修改文件。无论底下有多少层都是只读的,只有最上层的文件系统是可写的。当需要修改一个文件时,AUFS创建该文件的一个副本,使用CoW将文件从只读层复制到可写层进行修改,结果也保存在可写层。在Docker中,底下的只读层就是image,可写层就是Container。

AUFS文件系统据说有3W行代码,而ext4文件系统却只有4000-5000行左右代码,这些代码是要被整合进内核的,后来AUFS申请要被合并进内核代码的时候,linuz觉得它这代码太过臃肿,于是拒绝了。因此AUFS这个文件系统一直以来就不是linux内核中自有的文件系统,想用AUFS这个文件系统的话,必须自己向内核打补丁并去编译使用它,但redhat系列的操作系统一向以稳定著称,不会干这种出格的事,所以在redhat系列操作系统中使用AUFS并无可能。而ubuntu上的docker默认使用的就是AUFS。

OverlayFS

Overlay是Linux内核3.18后支持的,也是一种Union FS,和AUFS的多层不同的是Overlay只有两层:一个upper文件系统和一个lower文件系统,分别代表Docker的镜像层和容器层。当需要修改一个文件时,使用CoW将文件从只读的lower复制到可写的upper进行修改,结果也保存在upper层。在Docker中,底下的只读层就是image,可写层就是Container。目前最新的OverlayFS为Overlay2。

AUFS和Overlay都是联合文件系统,但AUFS有多层,而Overlay只有两层,所以在做写时复制操作时,如果文件比较大且存在比较低的层,则AUSF会慢一些。而且Overlay并入了linux kernel mainline,AUFS没有。目前AUFS已基本被淘汰。

DeviceMapper

Device mapper是Linux内核2.6.9后支持的,提供的一种从逻辑设备到物理设备的映射框架机制,在该机制下,用户可以很方便的根据自己的需要制定实现存储资源的管理策略。AUFS和OverlayFS都是文件级存储,而Device mapper是块级存储,所有的操作都是直接对块进行操作,而不是文件。Device mapper驱动会先在块设备上创建一个资源池,然后在资源池上创建一个带有文件系统的基本设备,所有镜像都是这个基本设备的快照,而容器则是镜像的快照。所以在容器里看到文件系统是资源池上基本设备的文件系统的快照,并没有为容器分配空间。当要写入一个新文件时,在容器的镜像内为其分配新的块并写入数据,这个叫用时分配。当要修改已有文件时,再使用CoW为容器快照分配块空间,将要修改的数据复制到在容器快照中新的块里再进行修改。

OverlayFS是文件级存储,Device mapper是块级存储,当文件特别大而修改的内容很小,Overlay不管修改的内容大小都会复制整个文件,对大文件进行修改显然要比小文件要消耗更多的时间,而块级无论是大文件还是小文件都只复制需要修改的块,并不是整个文件,在这种场景下,显然device mapper要快一些。因为块级的是直接访问逻辑盘,适合IO密集的场景。而对于程序内部复杂,大并发但少IO的场景,Overlay的性能相对要强一些。

docker registry

启动容器时,docker daemon会试图从本地获取相关的镜像,本地镜像不存在时,其将从Registry中下载该镜像并保存到本地。

Registry用于保存docker镜像,包括镜像的层次结构和元数据。用户可以自建Registry,亦可使用官方的Docker Hub。

docker registry的分类:

  • Sponsor Registry:第三方的Registry,供客户和Docker社区使用
  • Mirror Registry:第三方的Registry,只让客户使用
  • Vendor Registry:由发布docker镜像的供应商提供的registry
  • Private Registry:通过设有防火墙和额外的安全层的私有实体提供的registry

docker registry的组成:

  • Repository
    • 由某特定的docker镜像的所有迭代版本组成的镜像仓库
    • 一个Registry中可以存在多个Repository
      • Repository可分为“顶层仓库”和“用户仓库”
      • 用户仓库名称格式为“用户名/仓库名”
    • 每个仓库可包含多个Tag(标签),每个标签对应一个镜像
  • Index
    • 维护用户帐户、镜像的检验以及公共命名空间的信息
    • 相当于为Registry提供了一个完成用户认证等功能的检索接口

Docker Registry中的镜像通常由开发人员制作,而后推送至“公共”或“私有”Registry上保存,供其他人员使用,例如“部署”到生产环境。

在这里插入图片描述

docker镜像的制作

多数情况下,我们做镜像是基于别人已存在的某个基础镜像来实现的,我们把它称为base image。比如一个纯净版的最小化的centos、ubuntu或debian。

那么这个最小化的centos镜像从何而来呢?其实这个基础镜像一般是由Docker Hub的相关维护人员,也就是Docker官方手动制作的。这个基础镜像的制作对于Docker官方的专业人员来说是非常容易的,但对于终端用户来说就不是那么容易制作的了。

Docker Hub

Docker Hub is a cloud-based registry service which allows you to link to code repositories, build your images and test them, stores manually pushed images, and links to Docker Cloud so you can deploy images to your hosts.

It provides a centralized resource for container image discovery, distribution and change management, user and team collaboration, and workflow automation throughout the development pipeline.

img

Docker Hub provides the following major features:

  • Image Repositories
    • Find and pull images from community and official libraries, and manage, push to, and pull from private images libraries to which you have access.
  • Automated Builds
    • Automatically create new images when you make changes to a source code repository.
  • Webhooks
    • A feature of Automated Builds, Webhooks let you trigger actions after a successful push to a repository.
  • Organizations
    • Create work groups to manage access to image repositories.
  • GitHub and Bitbucket Integration
    • Add the Hub and your Docker Images to your current workflows.

docker镜像的获取

To get Docker images from a remote registry(such as your own Docker registry)and add them to your local system, use the docker pull command:

# docker pull <registry>[:<port>]/[<namespace>/]<name>:<tag>

The is a host that provides the docker-distribution service on TCP (default:5000)

Together, and identify a particular image controlled by at the registry

  • Some registries also support raw ;for those, is optional
  • When it is included, however, the additional level of hierarchy that provides is usefull to distinguish between images with the same

The additional level of hierarchy of

NamespaceExamples(/)
organizationredhat/kubernetes, google/kubernetes
login(username)Alice/application, bob/application
roledevel/database, test/database, prod/database

镜像的生成

镜像的生成途径:

  • Dockerfile
  • 基于容器制作
  • Docker Hub automated builds

img

基于容器制作镜像

Create a new image from container’s changes

Usage:

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
OptionsDefaultDescription
—author, -aAuthor (e.g., “John Hannibal Smith hannibal@a-team.com”)
-c, --change listApply Dockerfile instruction to the created image
-m, --message stringCommit message
-p, --pausetruePause container during commit

下载centos系统镜像

[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete 
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
httpd        latest    73b8cfec1155   6 days ago     138MB
centos       latest    300e315adb2f   7 months ago   209MB

使用镜像启动容器并进入容器

[root@localhost ~]# docker exec -it centos /bin/bash
[root@bedde81bdbbe /]#

安装epel源和nginx

[root@bedde81bdbbe /]# yum install -y nginx
[root@bedde81bdbbe /]# yum install -y epel-release
#安装过程省略

配置nginx为前台启动模式

[root@bedde81bdbbe /]# vi /etc/nginx/nginx.conf
daemon off;#添加以下内容

启动nginx

[root@bedde81bdbbe /]# /usr/sbin/nginx
[root@bedde81bdbbe /]# ss -antl
State         Recv-Q        Send-Q                Local Address:Port                 Peer Address:Port 
LISTEN        0             128                         0.0.0.0:80                        0.0.0.0:*    
LISTEN        0             128                            [::]:80                           [::]:*

在创建镜像时,我们不能关闭容器,必须使其处于运行状态,所以我们必须要另起一个终端,然后执行

[root@localhost ~]# docker ps -a		//查看容器
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                    PORTS     NAMES
bedde81bdbbe   centos    "/bin/bash"          34 minutes ago   Up 13 minutes                       centos
47da84658a2c   httpd     "httpd-foreground"   21 hours ago     Up 21 hours               80/tcp    quizzical_johnson
6af374ebc708   httpd     "httpd-foreground"   21 hours ago     Exited (0) 21 hours ago             happy_rhodes
8c1b7b398295   httpd     "httpd-foreground"   21 hours ago     Exited (0) 21 hours ago             great_jackson
5b0e2cdeafc1   httpd     "httpd-foreground"   21 hours ago     Created                             frosty_mirzakhani

[root@localhost ~]# docker commit -m "nginx" -a "centos-nginx" -c 'CMD ["/usr/sbin/nginx"]' bedde81bdbbe centos-nginx		//制作镜像
sha256:5e4a5f7ac13baff394d83f35075de39b091dbbdc1794cd1ca8b49379c1df5a7f
[root@localhost ~]# docker images		使用新镜像启动容器,先查看镜像
REPOSITORY     TAG       IMAGE ID       CREATED          SIZE
centos-nginx   latest    5e4a5f7ac13b   14 seconds ago   318MB
httpd          latest    73b8cfec1155   6 days ago       138MB
centos         latest    300e315adb2f   7 months ago     209MB
[root@localhost ~]# docker run --name nginx -p 8888:80 -d centos-nginx  //使用新镜像启动容器
6309c08346d3576eb0f6f0e30c76d745859bd1f74622aaac9e2c9f9a30539453

–name nginx 为容器定义名称,名称为nginx

-p 8888:80 将宿主机的8888端口映射到容器内的80端口

-d 指定该容器在后天运行

cetnos-nginx 指定使用centos-nginx镜像来创建容器

[root@localhost ~]# curl 172.17.0.3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <title>Test Page for the Nginx HTTP Server on Red Hat Enterprise Linux</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <style type="text/css">
            /*<![CDATA[*/
            body {
                background-color: #fff;
                color: #000;
                font-size: 0.9em;
                font-family: sans-serif,helvetica;
                margin: 0;
                padding: 0;
            }
            :link {
                color: #c00;
            }
            :visited {
                color: #c00;
            }
            a:hover {
                color: #f50;
            }
            h1 {
                text-align: center;
                margin: 0;
                padding: 0.6em 2em 0.4em;
                background-color: #900;
                color: #fff;
                font-weight: normal;
                font-size: 1.75em;
                border-bottom: 2px solid #000;
            }
            h1 strong {
                font-weight: bold;
                font-size: 1.5em;
            }
            h2 {
                text-align: center;
                background-color: #900;
                font-size: 1.1em;
                font-weight: bold;
                color: #fff;
                margin: 0;
                padding: 0.5em;
                border-bottom: 2px solid #000;
            }
            hr {
                display: none;
            }
            .content {
                padding: 1em 5em;
            }
            .alert {
                border: 2px solid #000;
            }

            img {
                border: 2px solid #fff;
                padding: 2px;
                margin: 2px;
            }
            a:hover img {
                border: 2px solid #294172;
            }
            .logos {
                margin: 1em;
                text-align: center;
            }
            /*]]>*/
        </style>
    </head>

    <body>
        <h1>Welcome to <strong>nginx</strong> on Red Hat Enterprise Linux!</h1>

        <div class="content">
            <p>This page is used to test the proper operation of the
            <strong>nginx</strong> HTTP server after it has been
            installed. If you can read this page, it means that the
            web server installed at this site is working
            properly.</p>

            <div class="alert">
                <h2>Website Administrator</h2>
                <div class="content">
                    <p>This is the default <tt>index.html</tt> page that
                    is distributed with <strong>nginx</strong> on
                    Red Hat Enterprise Linux.  It is located in
                    <tt>/usr/share/nginx/html</tt>.</p>

                    <p>You should now put your content in a location of
                    your choice and edit the <tt>root</tt> configuration
                    directive in the <strong>nginx</strong>
                    configuration file
                    <tt>/etc/nginx/nginx.conf</tt>.</p>

                    <p>For information on Red Hat Enterprise Linux, please visit the <a href="http://www.redhat.com/">Red Hat, Inc. website</a>. The documentation for Red Hat Enterprise Linux is <a href="http://www.redhat.com/docs/manuals/enterprise/">available on the Red Hat, Inc. website</a>.</p>

                </div>
            </div>

            <div class="logos">
                <a href="http://nginx.net/"><img
                    src="nginx-logo.png" 
                    alt="[ Powered by nginx ]"
                    width="121" height="32" /></a>
                <a href="http://www.redhat.com/"><img
                    src="poweredby.png"
                    alt="[ Powered by Red Hat Enterprise Linux ]"
                    width="88" height="31" /></a>
            </div>
        </div>
    </body>
</html>

使用浏览器访问容器内的nginx
在这里插入图片描述

将我们做好的镜像push上去

[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: bwxh404
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 images 
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
centos-nginx   latest    5e4a5f7ac13b   7 minutes ago   318MB
httpd          latest    73b8cfec1155   6 days ago      138MB
centos         latest    300e315adb2f   7 months ago    209MB
[root@localhost ~]# docker tag 5e4a5f7ac13b bwxh404/centos-nginx:0.1
[root@localhost ~]# docker images 
REPOSITORY             TAG       IMAGE ID       CREATED         SIZE
bwxh404/centos-nginx   0.1       5e4a5f7ac13b   9 minutes ago   318MB
centos-nginx           latest    5e4a5f7ac13b   9 minutes ago   318MB
httpd                  latest    73b8cfec1155   6 days ago      138MB
centos                 latest    300e315adb2f   7 months ago    209MB
[root@localhost ~]# docker push bwxh404/centos-nginx:0.1
The push refers to repository [docker.io/bwxh404/centos-nginx]
4877a3e066eb: Pushed 
2653d992f4ef: Mounted from library/centos 
0.1: digest: sha256:899f0a42cb1b333acf581f765b4a5d39a4b7707338247ca10ab0e9405ace5dab size: 741

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-07-29 14:51:25  更:2021-07-29 14:51:35 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/2 18:57:10-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码