docker介绍、安装及镜像管理
虚拟化简介

docker介绍
docker架构
传统虚拟化分层抽象和docker虚拟化分层抽象的
docker虚拟化实施有以下3个概念
- docker镜像:镜像是一个静态模板,与常见的iso镜像类似,是一个样板,不能直接修改,可以通过封装生成
- docker容器:基于docker镜像运行启动的应用或系统,称之为一个docker容器或docker虚拟机
- docker仓库:docker仓库是存放docker镜像的地方,常见分为公开仓库和私有仓库两种形式
docker体系结构
docker 安装
1、在centos7.6 1810版本安装docker,我们需要开启centos-extra的仓库,默认是开启的
2、安装yum-utils工具(提供yum-config-manager
实用程序)
[root@localhost yum.repos.d]# yum install yum-utils -y
3、添加docker的软件仓库
[root@localhost yum.repos.d]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
4、安装docker引擎
[root@localhost yum.repos.d]# yum install docker-ce docker-ce-cli containerd.io -y
如果这一步安装报错,报错原因就是docker-ce里面的runc和podman(红帽容器实现)底层的软件包在支持上冲突了,可以使用 --allowerasing参数,替换掉冲突的软件包。
[root@localhost yum.repos.d]# yum install docker-ce docker-ce-cli containerd.io --allowerasing -y
5.启动docker
[root@localhost yum.repos.d]# systemctl start docker
docker镜像的概念
docker-ce安装完成就相当于装好了docker-cli和docker-ce进程,启动docker-ce进程就算启动了docker,但是我们此时没有直接使用的docker仓库,镜像就是一个装好了特定应用的东西,镜像是静态的,容器是动态的,镜像运行起来就是容器。这个概念类似于程序和进程,程序是静态的,进程是动态的;镜像只会占用磁盘空间,容器会占用磁盘空间的同时还会消耗cpu和内存资源。
镜像一般存放在镜像仓库中,然后通过docker-cli提供的命令从镜像仓库中拉取镜像,然后将镜像保存到本地,以便后续使用。镜像仓库中的镜像都是经过压缩的,压缩的目的在于减小镜像的大小,以便在传输过程中(镜像拉取)减小带宽的压力。docker的客户端在拉取镜像后会对其进行解压。默认情况docker需要配置镜像仓库。一般生产环境下会配置私有的镜像仓库,保证镜像传输的安全和速度。
配置dockerhub镜像加速器
利用阿里云进行配置镜像加速
docker镜像管理
docker镜像
docker镜像含有启动容器所需要的文件系统和内容,因此,其用于创建并启动docker容器
docker镜像采用分层构建机制,最底层为bootfs,其之为rootfs
- bootfs:用于系统引导的文件系统,包括bootloader和kernel,容器启动完成后会被卸载以节省内存资源
- rootfs:位于rootfs上,表现为docker容器的根文件系统
传统模式中,系统启动时,内核挂载rootfs时会首先将其挂载为“只读”模式,完整性自检完成后将其重新挂载为只读模式
docker中,rootfs由内核挂在为“只读”模式,而后通过“联合挂载”技术额外挂载一个“可写”层
Docker Image Layer
位于下层的镜像称为父镜像(parent image),最底层的称之为基础镜像(Base Image)
最上层为“读写”层,其下层为“只读”层
想要联合挂载需要特定的文件系统Aufs
Aufs
advance multi-layered unification filesystem:高级多层统一文件系统
- 用于为linux文件系统实现“联合挂载”
- aufs是之前的UnionFS的重新实现,2006年由Junjiro Okajima开发
- Docker最初使用aufs作为容器文件系统层,它目前仍作为存储后端之一来支持
- aufs的竞争产品是overlayfs,后者后者自从3.18版本开始被合并到了linux内核
- docker的分层镜像,除了aufs,docker还支持btrfs,devicemapper和vfs等
在Ubuntu系统下,默认文件系统为aufs,而在CentOS7上,用的是devicemapper
Docker Registry
启动容器时,docker daemon会试图从本地获取相关镜像,本地镜像不存在时,将从Registry中下载该镜像保存至本地中
镜像拉取 docker pull 镜像名称
docker pull 默认是从dockerhub上拉取镜像的,如果指定了镜像仓库,则从仓库拉取,前提是没有配置镜像仓库的配置文件
1 | [root@node1 ~] # docker pull centos #拉取centos 镜像,默认是latest版本的,也可以指定版本[TAG]拉取 |
查看镜像 docker images
1 2 3 4 | [root@localhost docker] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 3 weeks ago 13.3kB centos latest 5d0da3dc9764 4 weeks ago 231MB |
删除镜像 docker image rm [REPOSITORY]:[TAG]
不建议通过IMAGE ID 删除镜像,有可能会有不同的REPOSITORY和TAG具有相同的IMAGE ID,可能会出现误删除,如果不指定TAG,会默认删除latest。
不能删除正在运行的docker镜像,必须要将容器停止才能删除
1 2 3 4 5 6 7 8 9 10 11 12 | [root@bogon ~] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 3 weeks ago 13.3kB centos latest 5d0da3dc9764 4 weeks ago 231MB [root@bogon ~] # docker image rm hello-world Untagged: hello-world:latest Untagged: hello-world@sha256:37a0b92b08d4919615c3ee023f7ddb068d12b8387475d64c622ac30f45c29c51 Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412 Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359 [root@bogon ~] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 4 weeks ago 231MB #hello-world 这个镜像已经删除了 |
docker镜像的导出
docker save
-o 表示输出为指定文件
1 2 3 4 5 | [root@bogon ~] # docker save centos:latest -o centos_latest_image.tar #将centos latest这个版本的镜像导出成为centos_latest_image.tar [root@bogon ~] # ls anaconda-ks.cfg centos_latest_image. tar check.sh hosts [root@bogon ~] # scp centos_latest_image.tar root@192.168.41.137:~/centos_latest_image.tar centos_latest_image. tar #通过scp命令将centos_latest_image.tar这个包传到另一台主机上100% 228MB 73.5MB/s 00:03 [root@bogon ~]# |
镜像导出适合在没有外网环境下进行安装docker镜像操作与docker load 相结合使用
docker镜像的导入
docker load
-i 表示导入指定的文件
1 2 3 4 5 6 7 8 9 | [root@node1 ~] # ls anaconda-ks.cfg centos_latest_image. tar [root@node1 ~] # docker load -i centos_latest_image.tar #将镜像文件导入到docker image 74ddd0ec08fa: Loading layer [==================================================>] 238.6MB /238 .6MB Loaded image: centos:latest [root@node1 ~] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 4 weeks ago 231MB [root@node1 ~] # |
使用docker hub创建自己的仓库
docker hub 镜像仓库结构分为仓库本身,仓库里面小仓库,还有tag,docker hub仓库名称为docker.io,例如当我们想要从docker hub上拉取Ubuntu镜像时,我们就从docker.io的大仓库中找到Ubuntu这个小仓库,找到小仓库后,我们需要从Ubuntu这个小仓库中拉取哪个tag的ubuntu 的镜像。
docker pull docker.io/library/ubuntu:latest
首先登陆docker hub网页 https://registry.hub.docker.com/repository/create?namespace=lizhifengqwe
点击 Repositories创建仓库
制作image镜像(两种方案)
第一种:使用hub仓库中已有的环境,安装自己使用的软件环境后完成image创建。
第二种:通过Dockerfile,完成镜像image的创建。
修改镜像名称标签 (以centos:latest为例)
1 2 3 4 5 | [root@node1 ~] # docker tag centos:latest lizhifengqwe/lizhifeng_public:centos-latest [root@node1 ~] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 4 weeks ago 231MB lizhifengqwe /lizhifeng_public centos-latest 5d0da3dc9764 4 weeks ago 231MB |
登陆docker hub
1 2 3 4 5 6 7 8 9 | [root@node1 ~] # 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: lizhifengqwe 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 |
上传镜像到docker hub 仓库
1 2 3 | [root@node1 ~] # docker push lizhifengqwe/lizhifeng:centos-latest The push refers to repository [docker.io /lizhifengqwe/lizhifeng ] 74ddd0ec08fa: Pushing [==> ] 13.13MB /231 .3MB |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」