3-Docker镜像介绍

时间:2021-07-13
本文章向大家介绍3-Docker镜像介绍,主要包括3-Docker镜像介绍使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1、Docker镜像介绍

Docker镜像含有启动容器所需要的文件系统及其内容,因此docker images是用于创建并启动docker容器。docker镜像采用分层构建机制,最底层为bootfs,其他为rootfs。

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

2、Docker分层镜像文件系统

  • 高级多层统一文件系统
  • 用于为linux文件系统实现“联合挂载”
  • aufs是之前的UnionFS的实现,但从来没有编译到linux内核中
  • docker最初使用aufs作为容器的文件系统层
  • aufs的竞争产品是overlayfs,overlayfs从3.18版本后被合并到linux内核
  • docker的分层镜像,除了aufs、还支持btrfs、devicemapper和vfs等
    • 在Ubuntu,默认使用aufs,在centos7上默认使用devicemapper。

3、Docker Registry

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

用户可以自建Registry,也可以使用官方的Docker Hub

docker Registry中的镜像通常由开发人员制作,然后推送到公共或者私有仓库保存。供其他人员使用,例如部署到生产环境。

一个registry一般由两部分组成

  • Repository
    • 由特定的docker镜像的所有迭代版本组成的镜像长裤
    • 一个registry中可以存在多个Repository
    • 每个仓库可以包含多个Tag,每个Tag对应一个镜像
  • Index
    • 维护用户账户、镜像校验、以及公共命名空间的信息
    • 相当于为registry提供一个完成用户认证等功能的索引接口。

4、Docker镜像制作

docker镜像有以下生成途径

  • Dockerfile

  • 基于容器制作

    • 在容器的可写层做变更操作,完成后把当前运行的容器commit为新的容器
    • 方式一:交互式镜像
    # 启动容器
    [root@localhost ~]# docker run --name b1 -it busybox:latest
    # 修改容器,创建个Http服务
    / # mkdir -p /data/html
    / # echo 'hello docker' > /data/html/index.html 
    # 制作新的镜像,注意基于容器制作镜像时要保持容器启动状态
    [root@localhost ~]# docker commit -p b1  
    # -p表示暂停容器状态,防止有新的内容写入容器导致镜像不完整。运行完成后会生成镜像id
    sha256:210ce5042f58544e198c3cff6e592877ea42051d42d76c58ddff329d6d429bd7
    # 查看生成的镜像
    [root@localhost ~]# docker image ls
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    <none>              <none>              210ce5042f58        3 minutes ago       1.22MB
    # 上面生成的镜像是没有名称和tag的。可通过tag指令指定。
    [root@localhost ~]# docker tag 210ce5042f58 yull/bbox_http:v0.1
    [root@localhost ~]# docker image ls
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    yull/bbox_http      v0.1                210ce5042f58        4 minutes ago       1.22MB
    # 通过生成镜像启动容器,查看自定义的镜像是否生效
    [root@localhost ~]# docker run --name b2 -it yull/bbox_http:v0.1 /bin/sh
    WARNING: IPv4 forwarding is disabled. Networking will not work.
    / # cat /data/html/index.html 
    hello docker
    # 查看容器情况,在CMD栏中会显示默认启动的命令,比如sh表示默认启动sh环境
    [root@localhost ~]# docker inspect b2
    ...
                "Cmd": [
                    "/bin/sh"
                ],
    ...
    
    • 方式二:非交互式镜像
    # 启动容器
    [root@localhost ~]# docker run --name b1 -it busybox:latest
    # 修改容器,创建个Http服务
    / # mkdir -p /data/html
    / # echo 'hello docker' > /data/html/index.html 
    # 制作新的镜像,注意基于容器制作镜像时要保持容器启动状态
    [root@localhost ~]# docker commit -a "larry.yu<larry.yu@gamil.com>" -c 'CMD ["/bin/httpd","-f","-h","/data/www"]' -p b3 yull/bbox_http:v0.2
    sha256:005a20ba36bbea35f988fcc512fca868be50c56a021ca86ea517ba7178e82e17
    [root@localhost ~]# docker image ls
    REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
    yull/bbox_http      v0.2                005a20ba36bb        5 seconds ago        1.22MB
    # 基于自定义镜像创建容器
    [root@localhost ~]# docker run --name b4 yull/bbox_http:v0.2
    WARNING: IPv4 forwarding is disabled. Networking will not work.
    
    # 启动后没有任何提示,是因为启动的为非交互式容器,通过curl访问容器的http服务验证
    [root@localhost ~]# curl http://172.17.0.3
    hello busybox http
    
    # 查看容器信息
    [root@localhost ~]# docker inspect b4 
    ...
                "Cmd": [
                    "/bin/httpd",
                    "-f",
                    "-h",
                    "/data/www"
                ],
    ...
                "IPAddress": "172.17.0.3",
    ...
    # 查看镜像列表
    [root@localhost ~]# docker image ls
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    yull/bbox_http      v0.2                005a20ba36bb        6 days ago          1.22MB
    yull/bbox_http      v0.1                210ce5042f58        6 days ago          1.22MB
    busybox             latest              19485c79a9bb        2 weeks ago         1.22MB
    nginx               stable-alpine       8587e8f26fc1        3 weeks ago         21.2MB
    redis               latest              f7302e4ab3a8        5 weeks ago         98.2MB
    
  • docker hub automated builds

5、将镜像push到阿里云

  • 创建镜像仓库

创建完成后点击管理,可以查看到仓库信息

  • 创建仓库登录密码
  • 终端登录阿里云docker registry
[root@localhost ~]# docker login --username=yu_liaxxx@126.com registry.cn-shanghai.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

# 查看需要push的镜像
[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
yull/bbox_http      v0.2                005a20ba36bb        6 days ago          1.22MB
# 创建阿里云专用tag
[root@localhost ~]# docker tag 005a20ba36bb registry.cn-shanghai.aliyuncs.com/larry-yu/busybox-httpd:v0.2
[root@localhost ~]# docker image ls
REPOSITORY                                                 TAG                 IMAGE ID            CREATED             SIZE
registry.cn-shanghai.aliyuncs.com/larry-yu/busybox-httpd   v0.2                005a20ba36bb        6 days ago          1.22MB
yull/bbox_http                                             v0.2                005a20ba36bb        6 days ago          1.22MB
# push镜像到阿里云仓库
[root@localhost ~]# docker push registry.cn-shanghai.aliyuncs.com/larry-yu/busybox-httpd:v0.2
The push refers to repository [registry.cn-shanghai.aliyuncs.com/larry-yu/busybox-httpd]
04f9d779f461: Pushed 
6c0ea40aef9d: Pushed 
v0.2: digest: sha256:ecee3411fa4ef0f8da07b237f04561549add36a70edfc5176d246831641ee806 size: 734
# 成功后就可以在镜像版本中看到
# 下载镜像可以使用
docker pull registry.cn-shanghai.aliyuncs.com/larry-yu/busybox-httpd:v0.2

6、Docker镜像导入导出

  • 镜像导出/保存
[root@localhost ~]# docker save -o busybox-httpd.gz yull/bbox_http:v0.2
[root@localhost ~]# ls
anaconda-ks.cfg  busybox-httpd.gz
  • 镜像导入
[root@localhost ~]# docker load -i busybox-httpd.gz

原文地址:https://www.cnblogs.com/mingxu520/p/15007378.html