本篇内容介绍了“Docker容器的基础管理方式”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
Docker介绍
是一种轻量级的“虚拟机”
在Linux容器里运行应用的开源工具
Docker与虚拟机的区别
Docker的使用场景
打包应用程序简化部署
可脱离底层硬件任意迁移
例:服务器从腾讯云迁移到阿里云
Docker核心概念
CentOS安装Docker的两种方式
使用curl获得docker的安装脚本进行安装
使用yum仓库来安装docker
docker的安装
设置镜像源,安装docker
[root@localhost ~]# yum install -y \
> yum-utils \ //设置源工具
> device-mapper-persistent-data \ //映射工具
> lvm2
[root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo //加载阿里云镜像源
[root@localhost ~]# yum install docker-ce -y //安装docker容器
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl start docker //开启docker容器
[root@localhost ~]# systemctl enable docker //设置开机自启动
[root@localhost ~]# ps aux | grep docker //查看docker进程是否开启
[root@localhost ~]# docker version //查看版本
[root@localhost ~]# docker search nginx //搜索公有镜像
[root@localhost ~]# docker pull nginx //下载镜像
进行镜像加速
登录阿里云网站--控制台--产品服务--容器镜像服务--镜像加速器--选择centos
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF' //进行镜像加速
> {
> "registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"]
}
[root@localhost ~]# systemctl daemon-reload //重载守护进程
[root@localhost ~]# systemctl restart docker //重启docker服务
[root@localhost ~]# docker pull nginx //下载nginx镜像
[root@localhost ~]# docker images //查看下载的镜像信息
REPOSITORY TAG IMAGE ID CREATE SIZE
nginx latest f7bb5701a33c 4 days ago 126MB
docker镜像基本操作
容器1:192.168.80.12
容器2:192.168.80.13
[root@localhost ~]# docker inspect f7bb5701a33c //查看镜像信息
[root@localhost ~]# docker tag nginx:latest nginx:web //添加新标签
[root@localhost ~]# docker images //查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f7bb5701a33c 4 days ago 126MB
nginx web f7bb5701a33c 4 days ago 126MB
//重新生成一个镜像,原有的不会消失
[root@localhost ~]# docker images | grep web //查看标签为web的镜像
nginx web f7bb5701a33c 4 days ago 126MB
[root@localhost ~]# docker rmi nginx:web //删除镜像,或者直接跟ID号
Untagged: nginx:web
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f7bb5701a33c 4 days ago 126MB
[root@localhost ~]# cd /opt/
[root@localhost opt]# docker save -o nginx nginx:latest //存出镜像
[root@localhost opt]# ls
containerd nginx rh
[root@localhost opt]# scp /opt/nginx root@192.168.80.13:/opt/ //远程复制到13服务器上
开启另一台装有docker的虚拟机(192.168.80.13)
[root@localhost opt]# docker load < nginx //载入镜像
//或者使用docker load --input 存出文件名
[root@localhost opt]# docker images //查看镜像信息
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f7bb5701a33c 4 days ago 126MB
回到原虚拟机(192.168.80.12)
[root@localhost opt]# docker tag nginx:latest nginx:web //修改标签
[root@localhost opt]# docker login //登录docker(你需要注册)
Username: //用户名
Password: //密码
[root@localhost opt]# docker push nginx:web //上传公有仓库
docker容器基本操作
[root@localhost opt]# docker create -it nginx:latest /bin/bash //基于镜像创建一个容器 -i让容器的标准输入保持打开,-t让docker分配一个伪终端
36fdfb0925ba040c094d585d70a3481bd450c7d39e6636ceeb10b5c1b9743593
[root@localhost opt]# docker ps -a //-a列出最近一次启动的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
36fdfb0925ba nginx:latest "/bin/bash" 3 seconds ago Created mystifying_dijkstra
[root@localhost opt]# docker start 36fdfb0925ba //开启容器
36fdfb0925ba
[root@localhost opt]# docker ps -a //查看此时容器状态为开启
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
36fdfb0925ba nginx:latest "/bin/bash" 16 minutes ago Up 6 seconds 80/tcp mystifying_dijkstra
操作步骤
[root@localhost opt]# docker search centos7 //查看找centos7镜像
[root@localhost opt]# docker pull paigeeworld/centos7 //下载镜像
[root@localhost opt]# docker images //查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f7bb5701a33c 4 days ago 126MB
nginx web f7bb5701a33c 4 days ago 126MB
paigeeworld/centos7 latest 4cbe7aa905e7 5 years ago 382MB
[root@localhost opt]# docker create -it paigeeworld/centos7 /bin/bash //创建容器
c48649c8cee9124cb456be4f93882e6dff16f88ba45051731138142d99293dfe
[root@localhost opt]# docker ps -a //查看容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c48649c8cee9 paigeeworld/centos7 "/bin/bash" 4 seconds ago Created relaxed_curran
36fdfb0925ba nginx:latest "/bin/bash" 24 minutes ago Exited (0) 3 minutes ago mystifying_dijkstra
[root@localhost opt]# docker start c48649c8cee9 //开启容器
c48649c8cee9
[root@localhost opt]# docker ps -a //查看容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c48649c8cee9 paigeeworld/centos7 "/bin/bash" 34 seconds ago Up 7 seconds relaxed_curran
36fdfb0925ba nginx:latest "/bin/bash" 24 minutes ago Exited (0) 3 minutes ago
容器的基本操作
[root@localhost opt]# docker run paigeeworld/centos7 /usr/bin/bash -c ls / //docker run直接下载镜像,创建容器,并开启,进入容器执行命令,退出
bin
boot
dev
etc
home
[root@localhost opt]# docker ps -a //查看容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c48649c8cee9 paigeeworld/centos7 "/bin/bash" 9 minutes ago Up 9 minutes relaxed_curran
[root@localhost opt]# docker exec -it c48649c8cee9 /bin/bash //进入容器(必须为开启状态)
bash-4.2# ls /
bin dev home lib64 media opt root sbin sys usr
boot etc lib lost+found mnt proc run srv tmp var
bash-4.2# exit //退出容器
exit
[root@localhost opt]# docker ps -a //此时容器状态还是开启的状态
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c48649c8cee9 paigeeworld/centos7 "/bin/bash" 10 minutes ago Up 10 minutes relaxed_curran
[root@localhost opt]# docker stop c48649c8cee9 //关闭容器
c48649c8cee9
[root@localhost opt]# docker ps -a //此时容器为退出状态
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c48649c8cee9 paigeeworld/centos7 "/bin/bash" 11 minutes ago Exited
[root@localhost opt]# docker run -d paigeeworld/centos7 /bin/bash -c "while true;do echo hello;done" //持续在后台执行,-d在后台进行运行
398f3d27f36b7f59a2167a71e71f61064e4e9a0808dfa13404caec0280a0b9c2
[root@localhost opt]# docker ps -a //查看容器一直是开启状态
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
398f3d27f36b paigeeworld/centos7 "/bin/bash -c 'while…" 9 seconds ago Up 7 seconds
[root@localhost opt]# docker export 36fdfb0925ba > nginx_c //容器导出
[root@localhost opt]# ls
containerd nginx nginx_c rh
[root@localhost opt]# scp /opt/nginx_c root@192.168.13.129:/opt/ //远程复制到另一台虚拟机
到另一台虚拟机上
[root@localhost opt]# ls
containerd nginx nginx_c rh
[root@localhost opt]# cat nginx_c | docker import - nginx:web //容器导入 会生成镜像而不会创建容器
sha256:1488d058197863aedd46d289eeb11dc39f19a2b855c3ecf383331a4d0bac568c
[root@localhost opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx web 1488d0581978 5 seconds ago 125MB
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost opt]# docker ps -a | awk '{print "docker rm "$1}' | bash //批量删除容器
docker资源控制
限制cpu使用速率
多任务按比例分享cpu
docker run --cpu-shares 1024 容器A
docker run --cpu-shares 1024 容器B
docker run --cpu-shares 2048 容器C
使用--cpuset-cpus选项限制cpu内核使用权
[root@localhost opt]# docker run --cpu-quota 20000 nginx:latest //设置20%限定
[root@localhost opt]# cd /sys/fs/cgroup/cpu/docker/
[root@localhost opt]# cat cpu.cfs_quota_us
-1
[root@localhost opt]# docker run -itd --name c1 --cpu-shares 512 paigeeworld/centos7 //创建容器c1设置权重,使得c1和c2的cpu资源占比为33.3%和66.7%
ec4ab03a7969eebe4746cfe67184bc2c6f9c97e81b22bc2ffab452820a78a0a7
[root@localhost opt]# docker run -itd --name c2 --cpu-shares 1024 paigeeworld/centos7
c688b014329c6a33b0d66947f4489a1a1cb6febc321090ecb4a82b68ae6df250
[root@localhost opt]# docker ps -a //查看容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c688b014329c paigeeworld/centos7 "/bin/bash" About a minute ago Up About a minute c2
ec4ab03a7969 paigeeworld/centos7 "/bin/bash" About a minute ago Up About a minute c1
[root@localhost opt]# docker run --name c3 --cpuset-cpus 0,1 paigeeworld/centos7 //限制容器使用指定的cpu
[root@localhost opt]# docker ps -a //查看容器的信息
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
751409a81682 paigeeworld/centos7 "/bin/bash" 8 seconds ago Exited (0) 7 seconds ago c3
[root@localhost opt]# docker run --name c5 -m 512m paigeeworld/centos7 //内存使用限制
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8c3101668345 paigeeworld/centos7 "/bin/bash" 5 seconds ago Exited (0) 4 seconds ago c5
对blkio限制
--device-read-bps:限制某个设备的数据量
--device-write-bps:限制写入某个设备的数据量
--device-read-iops:限制读某个设备的次数
--device-write-iops:限制写入某个设备的次数
[root@localhost opt]# docker run -d --device-write-bps /dev/sda:30mb paigeeworld/centos7
docker的数据管理
数据管理操作
两种管理方式
数据卷
数据卷容器
数据卷共享(宿主机跟容器的共享)
[root@localhost ~]# docker pull centos //下载镜像
[root@localhost ~]# docker run -v /var/www:/data1 --name web1 -it centos /bin/bash //关联宿主机和容器的共享
[root@2483bee94f1a /]# cd data1/
[root@2483bee94f1a data1]# echo "123" > test01.txt
[root@2483bee94f1a data1]# exit
exit
[root@localhost ~]# cat /var/www/test01.txt
123
数据卷容器共享(容器跟容器)
[root@localhost ~]# docker run --name web100 -v /data1 -v /data2 -it centos /bin/bash //创建一个web100容器并分别有两个卷为data1,2
[root@ba6a328c068e /]# cd data1/
[root@ba6a328c068e data1]# echo "111" > 111.txt //分别在目录中写入内容
[root@ba6a328c068e data1]# cd ../data2/
[root@ba6a328c068e data2]# echo "222" > 222.txt
[root@ba6a328c068e data2]# exit
exit
[root@localhost ~]# docker run -it --volumes-from web100 -it centos /bin/bash //将新容器挂载数据卷容器web100
[root@3f64be49dadd /]# cat data1/111.txt
111
[root@3f64be49dadd /]# cat data2/222.txt
222
“Docker容器的基础管理方式”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注天达云网站,小编将为大家输出更多高质量的实用文章!