容器及镜像
容器
常见的容器状态
running
stopped
paused
created
deleted
注意: 查看docker状态使用的命令:
docker ps: 查看running状态的容器
docker ps -a: 查看所有状态的容器
docker ps -qa: 查看所有容器ID
docker stop $(docker ps -qa) : 停止所有容器
docker rm $(docker ps -qa) : 删除所有容器
docker run 命令的使用
常见的选项介绍:
--name : 指定容器名称
-i : 交互式运行和-t一起使用,可以合成-it使用
-t: 允许tty
-e : 设置容器启动的环境变量,例如mysql的容器启动需要指定-e MYSQL_ROOT_PASSWORD='12345'等
-d: detach,后台运行
--network: 指定网络
--restart: always,
-p: 端口映射
-v: 指定存储卷
--rm : 运行之后删除,需要与-it配合使用,但是与-d互斥
-m : 限制最大使用内存
--oom-kill-disable :禁止oom
--cpus : 限定使用的CPU容量
[root@centos7-node1 ~]# docker run --name redis -d redis:4-alpine
docker exec的使用
常见的选项介绍:
-d : 后台运行
-e : 指定环境变量
-it: 交互模式
-u: 指定用户
-w: 指定工作路径
[root@centos7-node1 ~]# docker container exec redis netstat -tanlp #对容器执行命令
[root@centos7-node1 ~]# docker exec -it redis /bin/sh
/data # redis-cli #客户端连接redis
127.0.0.1:6379> set hello my_name_is_wanghui
OK
127.0.0.1:6379> get hello
"my_name_is_wanghui"
容器的启停
[root@centos7-node1 ~]# docker stop redis # 未指定存储卷的情况下,停止容器之后数据会丢失
[root@centos7-node1 ~]# docker start redis
其他实践
[root@centos7-node1 ~]# docker image pull nginx:1.18-alpine
[root@centos7-node1 ~]# docker run --name web -it --rm nginx:1.18-alpine /bin/sh #推出交互模式就自动退出并删除容器
[root@centos7-node1 ~]# docker run --name web -d nginx:1.18-alpine
[root@centos7-node1 ~]# docker container exec web ifconfig #获取创建机器的ip
[root@centos7-node1 ~]# elinks -dump 172.17.0.3 #访问页面
[root@centos7-node1 ~]# docker logs web -f #查看Nginx 滚动日志
[root@centos7-node1 ~]# docker stats web #查看web这个容器运行时资源占用情况
[root@centos7-node1 ~]# docker top web #查看web这个容器的进程状态
[root@centos7-node1 ~]# docker run --name c2 -it centos:7
[root@f879de456c8d /]#
[root@f879de456c8d /]# [root@centos7-node1 ~]# `ctrl+p,ctrl+q`
[root@centos7-node1 ~]# docker ps #c2是运行状态的
[root@centos7-node1 ~]# docker attach c2 #进入容器
镜像
简介
docker镜像含有启动容器时所需要的文件系统及其内容,因此,其用于创建并启动容器
采用分层构建机制,最底层为bootfs,其次为rootfs
bootfs: 用于系统引导的文件系统,包括bootloader和kernel,容器启动完成后会被卸载,以此节约内存资源
rootfs: 位于bootfs之上,表现为docker容器的根文件系统
传统模式中,在系统启动时,内核挂载rootfs时会首先挂在为只读模式,完整性比肩完成之后重新挂载为读写模式
docker中,rootfs由内核挂载为只读模式,而后通过联合挂载技术,额外在挂载一个可写层
docker镜像的构建方式
位于下层的镜像称为父镜像,最底层的是基础镜像
最上层为“可读写”层,其下的均为“只读”层
镜像存放的graph driver类型(文件系统)
特点: 分层叠加,写时复制(COW)
此文件系统是建构在底层操作系统文件系统之上用于存放镜像的文件系统,建议底层支持操作系统务必使用xfs文件系统
分类如下:
Aufs: 高级多层统一文件系统
Overlay2
DeviceMapper(DM)
Aufs
用于为Linux文件系统实现联合挂载
aufs之前是unionFS的重新实现,2006年由Junjiro Okjima开发
docker最初使用aufs作为容器文件系统层,目前仍然作为存储后端之一来支持
aufs的竞品是overlayfs,后者在3.18内核之后开始,被合并到linux内核
docker的分层镜像,除了aufs,还支持btfs,devicemapper和vfs等
ubuntu下,docker默认的文件系统是aufs,而在cnetos7上,用的是devicemapper
Docker Registry
启动容器时,docker daemon会试图从本地获取相关镜像,本地镜像不存在时,其将从Registry中下载该镜像并保存到本地
docker registry的分类
Registry用于保存docker镜像,包括景象的层次结构和元数据
用户可以自建Registry,也可以使用官方的dockerhub
分类如下:
Sponsor Restory:第三方的registry,供客户和docker社区使用
Mirror Registry:第三方的registry,只供客户使用
Vendor Registry: 由发布Docker镜像的供应商提供的Registry
Private Registry:通过舍友防火墙和额外安全层的私有实体提供的Registry
Docker Registry的组成
Repostory
由特定的docker镜像的所有迭代版本组成的镜像仓库
一个registry可以存在多个Repostory
Registory可以分为顶层仓库和用户仓库
用户仓库名称更是为用户名/仓库名
每个仓库可以包含多个Tag,每个Tag只对应一个镜像
Index: 维护用户账户,镜像的校验以及公共命名空间信息,相当于为Registry提供了一个拥护认证和检索的接口
镜像的制作 与推送
docker registry中的镜像由开发或者运维人员制作,而后推送到“公共”或者“私有”Repostry上保存,供其他人使用
例如: 部署到生产环境
DockerHub的使用
注册DockerHub账户
创建myimg仓库
上传镜像到dockerHub的myimg仓库
[root@centos7-node1]# docker pull busybox:latest
[root@centos7-node1 ~]# docker run --name b1 -it -d busybox /bin/sh
/ # mkdir /data/web/html -p
/ # echo "
this is a test page @busyboxServer
" > /data/web/html/index.html[root@centos7-node1 ~]# docker container commit b1 wanghui122725501/myimg:v0.1 #另起终端打包镜像
[root@centos7-node1 ~]# docker images #查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
wanghui122725501/myimg v0.1 baf6ad6a846f About a minute ago 1.22MB
[root@centos7-node1 ~]# docker run --name mybbox1 -it wanghui122725501/myimg:v0.1 #运行容器
/ # ls /data/web/html/ #此文件已被写入文件系统
[root@centos7-node1 ~]# docker login #登陆dockerhub
[root@centos7-node1 ~]# docker push wanghui122725501/myimg:v0.1 #推送镜像
[root@centos7-node1 ~]# docker container commit -p -a "wanghui@yeecall.com" -c "CMD ['/bin/httpd -f -h /data/web/html']" b1 wanghui122725501/myimg:v0.2 #前台运行httpd
[root@centos7-node1 ~]# docker tag wanghui122725501/myimg:v0.2 wanghui122725501/myimg:latest
[root@centos7-node1 ~]# docker push wanghui122725501/myimg:v0.2 #镜像推送
-----另起终端,进入b1容器
[root@centos7-node1 ~]# docker exec -it b1 sh
/ # /bin/httpd -f -h /data/web/html/ #执行httpd
[root@centos7-node1 ~]# curl 172.17.0.5 #另起终端执行
this is a test page @busyboxServer
[root@centos7-node1 ~]# docker commit -p -a "wanghui@yeecall.com" -c 'CMD ["/bin/sh","-c","/bin/httpd -f -h /data/web/html"]' b1 wanghui122725501/myimg:v0.4
[root@centos7-node1 ~]# docker run --name mytinyweb2 -d wanghui122725501/myimg:v0.4