小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。
1. Docker 介绍
1.1 什么是容器?
1.2 容器的前世今生
FreeBASE jail -> Linux vserver
-
chroot:完整的根文件系统(FHS)标准的
-
namespaces:UTS Mount IPC PID user network
-
cgroup:资源的分配和监控
通过比较复杂的代码开发的过程,调用以上三种技术,实现容器的创建、管理、销毁
1.3 传统虚拟化技术和容器对比
1.4 容器的今生
实现隔离能力!
LXC -> Docker
LXC(LinuXContainer),对于原有的常用功能进行了封装,方便我们做容器的生命周期管理
2. Docker 的安装
2.1 环境准备
curl http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
复制代码
2.2 安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
yum list docker-ce.x86_64 --showduplicates | sort -r
复制代码
2.3 安装 docker-ce
yum install -y --setopt=obsoletes=0 \
docker-ce-17.03.2.ce-1.el7.centos.x86_64 \
docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch
复制代码
2.4 启动 docker 服务
systemctl daemon-reload
systemctl restart docker
docker version
docker info
复制代码
2.5 配置镜像加速
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://68rmyzg7.mirror.aliyuncs.com"]
}
复制代码
3. Docker 体系结构
Registry:
-
用户验证
-
镜像索引
-
镜像存储
4. Docker 的镜像基础管理
4.1 获取镜像
docker search centos
docker pull centos:6.9
docker pull centos:7.5.1804
docker pull nginx
复制代码
4.2 查询镜像
4.2.1 查看镜像
[root@VM-0-3-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 35c43ace9216 7 days ago 133 MB
centos 6.9 2199b8eb8390 23 months ago 195 MB
centos 7.5.1804 cf49811e3cdb 23 months ago 200 MB
复制代码
REPOSITORY
表示镜像名称。
有的镜像名称是 xxx/centos,没带 /
表示的是官方发布的镜像,带有 /
表示的是个人或者其他公司发布的镜像。选择时就选择 star 多的就好了。
TAG
表示版本号。
REPOSITORY:TAG
能够标识镜像唯一。
IMAGE ID
是镜像的 ID,是 sha256:64 位号码
,默认只截取 12 位。
使用 docker image ls --no-trunc
可以查看完整镜像 ID
[root@VM-0-3-centos ~]# docker image ls --no-trunc
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest sha256:35c43ace9216212c0f0e546a65eec93fa9fc8e96b25880ee222b7ed2ca1d2151 7 days ago 133 MB
centos 6.9 sha256:2199b8eb8390197d175b1dd57ed79f53ed92ffa58e23826ada54113262237e56 23 months ago 195 MB
centos 7.5.1804 sha256:cf49811e3cdb94cbdfd645f3888d7add06a315449cf2c7ca7b81c312f1e46c63 23 months ago 200 MB
复制代码
4.2.2 单独查询镜像 ID
[root@VM-0-3-centos ~]# docker images -q
35c43ace9216
2199b8eb8390
cf49811e3cdb
复制代码
4.2.3 镜像信息详细查看
docker image inspect nginx:latest
复制代码
4.3 镜像的导入和导出
docker image save nginx:latest > /tmp/nginx.tar
docker image load -i /tmp/nginx.tar
复制代码
4.4 镜像的删除
// 删除镜像
docker image rm [IMAGE ID]
// 删除所有镜像
docker rmi `docker images -q`
docker rmi $(docker images -q)
// 强制删除 -f
docker image rm -f 35c43ace9216
复制代码
4.5 重新设置标签
准备工作:
// 导出镜像,使用镜像 ID
[root@VM-0-3-centos tmp]# docker image save 35c43ace9216 > /tmp/nginx1.va
// 导入镜像
[root@VM-0-3-centos tmp]# docker image load -i /tmp/nginx1.var
// 查看镜像
[root@VM-0-3-centos tmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 35c43ace9216 7 days ago 133 MB
centos 6.9 2199b8eb8390 23 months ago 195 MB
centos 7.5.1804 cf49811e3cdb 23 months ago 200 MB
// 对比发现,使用 REPOSITORY:TAG 导出的镜像,再导入时同样有 REPOSITORY:TAG,而使用 IMAGE ID 导出的镜像,再导入时不包含 REPOSITORY:TAG
// 手动添加 REPOSITORY:TAG
[root@VM-0-3-centos tmp]# docker image tag 35c43ace9216 nginx:v1
[root@VM-0-3-centos tmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v1 35c43ace9216 7 days ago 133 MB
centos 6.9 2199b8eb8390 23 months ago 195 MB
centos 7.5.1804 cf49811e3cdb 23 months ago 200 MB
// 我们再拉取一个 nginx 最新镜像,可以发现只是两个镜像的 tag 不一样
[root@VM-0-3-centos tmp]# docker pull nginx
[root@VM-0-3-centos tmp]# docker image ls --no-trunc
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest sha256:35c43ace9216212c0f0e546a65eec93fa9fc8e96b25880ee222b7ed2ca1d2151 7 days ago 133 MB
nginx v1 sha256:35c43ace9216212c0f0e546a65eec93fa9fc8e96b25880ee222b7ed2ca1d2151 7 days ago 133 MB
centos 6.9 sha256:2199b8eb8390197d175b1dd57ed79f53ed92ffa58e23826ada54113262237e56 23 months ago 195 MB
centos 7.5.1804 sha256:cf49811e3cdb94cbdfd645f3888d7add06a315449cf2c7ca7b81c312f1e46c63 23 months ago 200 MB
// 我们再对 nginx:latest 镜像修改 tag,发现其实只是多了一个链接
[root@VM-0-3-centos tmp]# docker image tag 35c43ace9216 nginx:v2
[root@VM-0-3-centos tmp]# docker image ls --no-trunc
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest sha256:35c43ace9216212c0f0e546a65eec93fa9fc8e96b25880ee222b7ed2ca1d2151 7 days ago 133 MB
nginx v1 sha256:35c43ace9216212c0f0e546a65eec93fa9fc8e96b25880ee222b7ed2ca1d2151 7 days ago 133 MB
nginx v2 sha256:35c43ace9216212c0f0e546a65eec93fa9fc8e96b25880ee222b7ed2ca1d2151 7 days ago 133 MB
centos 6.9 sha256:2199b8eb8390197d175b1dd57ed79f53ed92ffa58e23826ada54113262237e56 23 months ago 195 MB
centos 7.5.1804 sha256:cf49811e3cdb94cbdfd645f3888d7add06a315449cf2c7ca7b81c312f1e46c63 23 months ago 200 MB
复制代码