docker (镜像分层、阿里云镜像推送/拉去)-day02

一、镜像概念

Docker 镜像是 Docker 容器的基础,它提供了一种可重复使用的、跨平台的部署方式,使得应用程序的部署和运行变得简单和高效。

把应用程序和配置依赖打包好形成一个可交付的运行环境(包括代码、运行时需要的库、环境变量和配置文件等),打包好的运行环境就是image镜像文件。

 二、分层镜像

2.1 镜像的底层原理(联合文件系统)

Docker 镜像的底层原理是联合文件系统(UnionFS)。联合文件系统是一种分层、轻量级并且高性能的文件系统,它支持对文件系统的修改作为一次提交来一层一层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下。

Docker 镜像实际上由一层一层的文件系统组成,这种层级的文件系统就是联合文件系统。在 Docker 镜像中,最底层是引导文件系统(bootfs),它主要包含引导加载器和内核。当引导加载完成之后,整个内核就都在内存中了,此时内存使用权已由 bootfs 转交给内核,此时系统也会卸载 bootfs。

在 bootfs 之上的层级是 rootfs(root file system),它包含的是典型 Linux 系统中的 /dev、/proc、/bin、/etc 等标准目录和文化。rootfs 就是各种不同的操作系统发行版,比如 Ubuntu、Centos 等。

联合文件系统使得 Docker 镜像可以分层进行继承,基于基础镜像,可以制作各种具体的应用镜像。这种分层的好处是共享资源、方便复制迁移,从而能够提高效率,减少空间占用,方便维护更新。

2.2 为什么docker镜像使用分层结构

Docker镜像层都是只读的,容器层是可写的。当容器启动时,一个新的可写层被加载到镜像的顶部。 这一层通常被称作“容器层”,“容器层”之下的都叫“镜像层”。

所有对容器的改动 。无论添加、删除、还是修改文件都只会发生在容器层中。只有容器层是可写的,容器层下面的所有镜像层都是只读的。
 

  三、docker镜像commit操作案例(重点)

 由此看出docker镜像分层,支持通过现有的镜像,生成新的镜像,在原有基础上扩展。

docker commit 提交容器副本使之成为一个新的镜像:

docker commit -m="提交的描述信息" -a="作者" 容器ID 要创建的目标镜像名:[标签名]

示范:
docker commit -m="vim install ok" -a="syf" f49f1addd673  syf/mybuntun1.0

 如图:ubuntu 容器中,没有vim 命令

[root@syf ~]# docker run -it --name myubuntu ubuntu /bin/bash
root@f49f1addd673:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@f49f1addd673:/# vim a.txt
bash: vim: command not found

安装vim 

更新ubuntu 包管理工具
apt-get update
安装vim
apt-get -y install vim

 commit 我们安装vim 的ubuntu镜像

[root@syf ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS         PORTS     NAMES
f49f1addd673   ubuntu    "/bin/bash"   44 minutes ago   Up 5 minutes             myubuntu
[root@syf ~]# docker commit -m="vim install ok" -a="syf" f49f1addd673  syf/mybuntun1.0

查看结果

[root@syf ~]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED              SIZE
syf/mybuntun1.0   latest    5166bfe283d8   About a minute ago   189MB
tomcat            latest    fb5657adc892   23 months ago        680MB
redis             latest    7614ae9453d1   23 months ago        113MB
ubuntu            latest    ba6acccedd29   2 years ago          72.8MB

 四、阿里云镜像仓库创建

4.1今日阿里云,左上角菜单栏鼠标放上去展开,搜索容器镜像服务

 4.2创建个人实例

4.3   创建命名空间

4.4在创建好的空间下,新建仓库 

4.5 选择本地仓库

最后阿里云会生成一堆命令,copy使用就行。

五、阿里云镜像推送和拉去(重点)

5.1 先登录:docker login --username=测试账号 registry.cn-shanghai.aliyuncs.com

[root@syf ~]# docker login --username=测试账号 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

5.2  打个tag

docker tag [ImageId] registry.cn-shanghai.aliyuncs.com/ceshi_resposity/myubuntu:[镜像版本号]

[root@syf ~]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED         SIZE
syf/mybuntun1.0   latest    5166bfe283d8   2 hours ago     189MB
tomcat            latest    fb5657adc892   23 months ago   680MB
redis             latest    7614ae9453d1   23 months ago   113MB
ubuntu            latest    ba6acccedd29   2 years ago     72.8MB
[root@syf ~]# docker tag 5166bfe283d8 registry.cn-shanghai.aliyuncs.com/ceshi_resposity/myubuntu:1.0

 5.3  推送到阿里云仓库

docker push registry.cn-shanghai.aliyuncs.com/ceshi_resposity/myubuntu:[镜像版本号]

[root@syf ~]# docker push registry.cn-shanghai.aliyuncs.com/ceshi_resposity/myubuntu:1.0

5.4从阿里云上拉去镜像 

docker pull registry.cn-shanghai.aliyuncs.com/ceshi_resposity/myubuntu:[镜像版本号]

[root@syf ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
tomcat       latest    fb5657adc892   23 months ago   680MB
redis        latest    7614ae9453d1   23 months ago   113MB
ubuntu       latest    ba6acccedd29   2 years ago     72.8MB
[root@syf ~]# docker pull registry.cn-shanghai.aliyuncs.com/ceshi_resposity/myubuntu:1.0
1.0: Pulling from syf_resposity/myubuntu
7b1a6ab2e44d: Already exists 
1ec782c8d5e1: Pull complete 
Digest: sha256:c6d759583840a80c82c0704858b8c0f84e1daf5883db9521446f6cc8c1cc9ec0
Status: Downloaded newer image for registry.cn-shanghai.aliyuncs.com/syf_resposity/myubuntu:1.0
registry.cn-shanghai.aliyuncs.com/ceshi_resposity/myubuntu:1.0
[root@syf ~]# docker images
REPOSITORY                                                 TAG       IMAGE ID       CREATED         SIZE
registry.cn-shanghai.aliyuncs.com/syf_resposity/myubuntu   1.0       5166bfe283d8   2 hours ago     189MB
tomcat                                                     latest    fb5657adc892   23 months ago   680MB
redis                                                      latest    7614ae9453d1   23 months ago   113MB
ubuntu                                                     latest    ba6acccedd29   2 years ago     72.8MB

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值