博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Docker基础 - 04镜像及仓库

Posted on 2021-11-04 21:26  Kingdomer  阅读(164)  评论(0编辑  收藏  举报

Docker基础 - 04镜像及仓库

一、Docker镜像

1.1 镜像

Docker镜像含有启动容器所需要的文件系统及其内容,用于创建并启动docker容器。
  • 采用分层构建机制,最底层为bootfs,上一层为rootfs
  • bootfs: 用于系统引导的文件系统,包括bootloader和kernel,容器启动完成后会被卸载以节约内存资源。
  • rootfs: 位于bootfs之上,表现为docker容器的根文件系统。 Base Image
    • 传统模式中,系统启动时,内核挂载rootfs时会首先将其挂载为"只读"模式,完整性自检完成后将其重新挂载为读写模式。
    • Docker中, rootfs由内核挂载为"只读"模式, 而后通过"联合挂载"技术额外挂载一个"可写"层。

       

1.2 Docker Image Layer

位于下层的镜像称为父镜像(parent image),最底层的称为基础镜像(base image)

最上层为"可读写"层, 其下的均为"只读"层。

          

1.3 Aufs

  • advanced multi-layered unification filesystem: 高级多层统一文件系统
  • 用于为Linux文件系统实现"联合挂载"
  • aufs是之前的UnionFS的重新实现,2006年由 Junjiro Okajima开发
  • Docker 最初使用aufs作为容器文件系统层,目前仍作为存储后端之一来支持
  • aufs的竞争产品是overlayfs, overlayfs自从3.18版本开始被合并到Linux内核
  • docker的分层镜像,除了aufs, docker还支持 btrfs, devicemapper和vfs等
    • 在Ubuntu系统下,docker默认Ubuntu的aufs;而在CentOS7上,用的是devicemapper

1.4 Devicemapper

(1.4.1) Device Mapper是 Linux 2.6内核中支持逻辑卷管理的通用设备映射机制,它为实现用于存储资源管理的块设备驱动提供了一个高度模块化的内核架构。

                          

(1.4.2) 在内核中通过一个一个模块化的target driver插件实现对IO请求的过滤或者重新定向等工作,

    当前已经实现的target driver插件包括软raid、软加密、逻辑卷条带、多路径、镜像、快照等。

  • liner、mirror、snapshot、multipath 表示的就是target driver。
  • 在这些插件中,Thin Provisioning Snapshot,Docker使用Thin Provisioning的Snapshot的技术实现了类似aufs的分层镜像。

                                      

二、Docker Registry

2.1 镜像拉取

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

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

2.2 Registry 分类

Registry用于保存docker镜像, 包括镜像的层次结构和元数据

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

2.3 Registry 结构

Repository

  • 由某特定的docker镜像的所有迭代版本组成的镜像仓库
  • 一个Registry中可以存在多个Repository
    • Repository可分为"顶层仓库"和"用户仓库"
    • 用户仓库名称格式为"用户名/仓库名"
  • 每个仓库可以包含多个Tag(标签),每个标签对应一个镜像

Index

  • 维护用户账户、镜像的校验以及公共命名空间的信息
  • 相当于为Registry提供了一个完成用户认证等功能的检索接口

2.4 镜像推送

Docker Registry中的镜像通常由开发人员制作,而后推送至"公共"或"私有"Registry上保存,供其他人员使用。

 

[root@component ~]# docker push --help

Usage:  docker push [OPTIONS] NAME[:TAG]

Push an image or a repository to a registry

Options:
  -a, --all-tags                Push all tagged images in the repository
      --disable-content-trust   Skip image signing (default true)
  -q, --quiet                   Suppress verbose output

 

2.5 Docker Hub

Docker Hub is a cloud-based registry service which allows you to link to code repositories, build you images and test them,

stores manually pushed images, and links to Docker Cloud so you can deploy images to you hosts.

major features: Image Repositories | Automated Builds | Webhooks | Origanizations | GitHub and Bitbucket Integration

2.6 Quay.io

RedHat   coreos/flannel

 

三、镜像操作

3.1 镜像生成

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

3.2 基于容器制作镜像

# 使用 busybox 制作一个httpd的web服务镜像
[root@cl-server ~]# docker pull busybox
[root@cl-server ~]# docker run -it --name b1 busybox
/ # ls /
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # mkdir -p /data/html
/ # vi /data/html/index.html
/ # cat /data/html/index.html
<h1>Hello, Busybox httpd Web Server.</h1>

[root@cl-server ~]# docker commit -p d689609f9c15
sha256:b680392e10f2253c55e8a3616c1c9cdf9c52a59d741ce058a251f2ce133fa623
[root@cl-server ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
<none>       <none>    b680392e10f2   4 seconds ago   1.23MB
busybox      latest    388056c9a683   2 weeks ago     1.23MB
[root@cl-server ~]# docker tag b680392e10f2 kunking/httpd:v0.1-1
[root@cl-server ~]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED          SIZE
kunking/httpd   v0.1-1    b680392e10f2   56 seconds ago   1.23MB
busybox         latest    388056c9a683   2 weeks ago      1.23MB

 

在kunking/httpd:v0.1-1基础上,生成新的镜像

--author,-a   Author

--change,-c   Apply Dockerfile instruction to the created image

--message,-m  Commit message

--pause, -p   Pause container during commit, default=true

docker commit -a "Bearpx <bearpx@kunking.com>" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p b1 kunking/httpd:v0.2

  

3.3 为镜像设定标签: docker tag

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
[root@cl-server ~]# docker tag --help
Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

[root@cl-server ~]# docker tag 5a38ea0db06f kunking/httpd:v0.1-3

  

3.4 登录DockerHub仓库或阿里云镜像仓库

# docker login
[root@cl-server ~]# docker login --help
Usage:  docker login [OPTIONS] [SERVER]

Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.

Options:
  -p, --password string   Password
      --password-stdin    Take the password from stdin
  -u, --username string   Username

# docker logout
[root@cl-server ~]# docker logout --help
Usage:  docker logout [SERVER]

Log out from a Docker registry.
If no server is specified, the default is defined by the daemon.

 

[root@cl-server ~]# docker login --username=157****4250 registry.cn-hangzhou.aliyuncs.com
[root@cl-server .docker]# pwd
/root/.docker
[root@cl-server .docker]# cat config.json 
{
	"auths": {
		"registry.cn-hangzhou.aliyuncs.com": {
			"auth": "MTU3MjUyNTQyNTA6emhhaTg5Mzg5aW1hZ2Vz"
		}
	}
}

 

3.5 镜像导入和导出 

docker save -o myimages.tar.gz  kunking/httpd:v0.2  kunking/tm:v0.2
docker load -i myimages.tar.gz

 

[root@cl-server ~]# docker save --help
Usage:  docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
  -o, --output string   Write to a file, instead of STDOUT

[root@cl-server ~]# docker load --help
Usage:  docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
  -i, --input string   Read from tar archive file, instead of STDIN
  -q, --quiet          Suppress the load output

  

3.6 image管理

[root@component ~]# docker image --help

Usage:  docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.

 

docker 镜像命令  

  • docker images
  • docker images -a / docker images -q / docker images --digests / docker images --digests --no-trunc
  • docker search XXX : 从 docker Hub 查找
  • docker search -s 30 tomcat
  • docker search -s 30 --no-trunc tomcat
  • docker pull tomcat | docker pull tomcat:latest
  • docker rmi hello-world
  • docker rmi -f hello-world
  • docker rmi -f $(docker images -q)
  • docker history XXX:    查看镜像的各镜像层的大小

 

[root@component ~]# docker history nginx
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
87a94228f133   3 weeks ago   /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B        
<missing>      3 weeks ago   /bin/sh -c #(nop) COPY file:09a214a3e07c919a…   4.61kB    
<missing>      3 weeks ago   /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7…   1.04kB    
<missing>      3 weeks ago   /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0…   1.96kB    
<missing>      3 weeks ago   /bin/sh -c #(nop) COPY file:65504f71f5855ca0…   1.2kB     
<missing>      3 weeks ago   /bin/sh -c set -x     && addgroup --system -…   64MB      
<missing>      3 weeks ago   /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster     0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  ENV NJS_VERSION=0.6.2        0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  ENV NGINX_VERSION=1.21.3     0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B        
<missing>      3 weeks ago   /bin/sh -c #(nop) ADD file:910392427fdf089bc…   69.3MB