Docker介绍
- 是一种轻量级的“虚拟机”
- 在Linux容器里运行应用的开源工具
Docker与虚拟机的区别
- 虚拟机是在一台物理机器上,利用虚拟化技术,虚拟出来多个操作系统,每个操作系统之间是隔离的。Docker是开源的应用容器引擎,依然需要先在电脑上安装操作系统,然后安装Docker容器的管理器,才可以。虚拟机是在硬件级别进行虚拟化,而Docker是在操作系统的层面虚拟化;虚拟机是通过模拟硬件搭建操作系统
Docker的使用场景
- 打包应用程序简化部署
- 可脱离底层硬件任意迁移
- 例:服务器从腾讯云迁移到阿里云
Docker核心概念
- 镜像
- 容器
- 仓库
CentOS安装Docker的两种方式
- 使用curl获得docker的安装脚本进行安装
- 使用yum仓库来安装docker
docker的安装
设置镜像源,安装docker
[[email protected] ~]# yum install -y > yum-utils \ //设置源工具
> device-mapper-persistent-data \ //映射工具
> lvm2
[[email protected] ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo //加载阿里云镜像源
[[email protected] ~]# yum install docker-ce -y //安装docker容器
[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# setenforce 0
[[email protected] ~]# systemctl start docker //开启docker容器
[[email protected] ~]# systemctl enable docker //设置开机自启动
[[email protected] ~]# ps aux | grep docker //查看docker进程是否开启
[[email protected] ~]# docker version //查看版本
[[email protected] ~]# docker search nginx //搜索公有镜像
[[email protected] ~]# docker pull nginx //下载镜像
进行镜像加速
-
登录阿里云网站--控制台--产品服务--容器镜像服务--镜像加速器--选择centos
[[email protected] ~]# tee /etc/docker/daemon.json <<-‘EOF‘ //进行镜像加速 > { > "registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"] > } > EOF { "registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"] } [[email protected] ~]# systemctl daemon-reload //重载守护进程 [[email protected] ~]# systemctl restart docker //重启docker服务 [[email protected] ~]# docker pull nginx //下载nginx镜像 [[email protected] ~]# 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
[[email protected] ~]# docker inspect f7bb5701a33c //查看镜像信息 [[email protected] ~]# docker tag nginx:latest nginx:web //添加新标签 [[email protected] ~]# docker images //查看镜像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 4 days ago 126MB nginx web f7bb5701a33c 4 days ago 126MB //重新生成一个镜像,原有的不会消失 [[email protected] ~]# docker images | grep web //查看标签为web的镜像 nginx web f7bb5701a33c 4 days ago 126MB [[email protected] ~]# docker rmi nginx:web //删除镜像,或者直接跟ID号 Untagged: nginx:web [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 4 days ago 126MB [[email protected] ~]# cd /opt/ [[email protected] opt]# docker save -o nginx nginx:latest //存出镜像 [[email protected] opt]# ls containerd nginx rh [[email protected] opt]# scp /opt/nginx [email protected]:/opt/ //远程复制到13服务器上
开启另一台装有docker的虚拟机(192.168.80.13)
[[email protected] opt]# docker load < nginx //载入镜像 //或者使用docker load --input 存出文件名 [[email protected] opt]# docker images //查看镜像信息 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 4 days ago 126MB
回到原虚拟机(192.168.80.12)
[[email protected] opt]# docker tag nginx:latest nginx:web //修改标签 [[email protected] opt]# docker login //登录docker(你需要注册) Username: //用户名 Password: //密码 [[email protected] opt]# docker push nginx:web //上传公有仓库
docker容器基本操作
[[email protected] opt]# docker create -it nginx:latest /bin/bash //基于镜像创建一个容器 -i让容器的标准输入保持打开,-t让docker分配一个伪终端 36fdfb0925ba040c094d585d70a3481bd450c7d39e6636ceeb10b5c1b9743593 [[email protected] opt]# docker ps -a //-a列出最近一次启动的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 36fdfb0925ba nginx:latest "/bin/bash" 3 seconds ago Created mystifying_dijkstra [[email protected] opt]# docker start 36fdfb0925ba //开启容器 36fdfb0925ba [[email protected] 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
操作步骤
[[email protected] opt]# docker search centos7 //查看找centos7镜像 [[email protected] opt]# docker pull paigeeworld/centos7 //下载镜像 [[email protected] 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 [[email protected] opt]# docker create -it paigeeworld/centos7 /bin/bash //创建容器 c48649c8cee9124cb456be4f93882e6dff16f88ba45051731138142d99293dfe [[email protected] 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 [[email protected] opt]# docker start c48649c8cee9 //开启容器 c48649c8cee9 [[email protected] 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
容器的基本操作
[[email protected] opt]# docker run paigeeworld/centos7 /usr/bin/bash -c ls / //docker run直接下载镜像,创建容器,并开启,进入容器执行命令,退出 bin boot dev etc home [[email protected] 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 [[email protected] 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 [[email protected] 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 [[email protected] opt]# docker stop c48649c8cee9 //关闭容器 c48649c8cee9 [[email protected] opt]# docker ps -a //此时容器为退出状态 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c48649c8cee9 paigeeworld/centos7 "/bin/bash" 11 minutes ago Exited [[email protected] opt]# docker run -d paigeeworld/centos7 /bin/bash -c "while true;do echo hello;done" //持续在后台执行,-d在后台进行运行 398f3d27f36b7f59a2167a71e71f61064e4e9a0808dfa13404caec0280a0b9c2 [[email protected] 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 [[email protected] opt]# docker export 36fdfb0925ba > nginx_c //容器导出 [[email protected] opt]# ls containerd nginx nginx_c rh [[email protected] opt]# scp /opt/nginx_c [email protected]:/opt/ //远程复制到另一台虚拟机
到另一台虚拟机上
[[email protected] opt]# ls containerd nginx nginx_c rh [[email protected] opt]# cat nginx_c | docker import - nginx:web //容器导入 会生成镜像而不会创建容器 sha256:1488d058197863aedd46d289eeb11dc39f19a2b855c3ecf383331a4d0bac568c [[email protected] opt]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx web 1488d0581978 5 seconds ago 125MB [[email protected] opt]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [[email protected] opt]# docker ps -a | awk ‘{print "docker rm "$1}‘ | bash //批量删除容器
docker资源控制
限制cpu使用速率
- 通过--cpu-quota选项来限制cpu的使用率
- 通过修改配置文件cpu.cfs_quota_us实现
多任务按比例分享cpu
- docker run --cpu-shares 1024 容器A
- docker run --cpu-shares 1024 容器B
- docker run --cpu-shares 2048 容器C
使用--cpuset-cpus选项限制cpu内核使用权
[[email protected] opt]# docker run --cpu-quota 20000 nginx:latest //设置20%限定
[[email protected] opt]# cd /sys/fs/cgroup/cpu/docker/
[[email protected] opt]# cat cpu.cfs_quota_us
-1
[[email protected] opt]# docker run -itd --name c1 --cpu-shares 512 paigeeworld/centos7 //创建容器c1设置权重,使得c1和c2的cpu资源占比为33.3%和66.7%
ec4ab03a7969eebe4746cfe67184bc2c6f9c97e81b22bc2ffab452820a78a0a7
[[email protected] opt]# docker run -itd --name c2 --cpu-shares 1024 paigeeworld/centos7
c688b014329c6a33b0d66947f4489a1a1cb6febc321090ecb4a82b68ae6df250
[[email protected] 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
[[email protected] opt]# docker run --name c3 --cpuset-cpus 0,1 paigeeworld/centos7 //限制容器使用指定的cpu
[[email protected] 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
[[email protected] opt]# docker run --name c5 -m 512m paigeeworld/centos7 //内存使用限制
[[email protected] 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:限制写入某个设备的次数
[[email protected] opt]# docker run -d --device-write-bps /dev/sda:30mb paigeeworld/centos7
docker的数据管理
数据管理操作
- 方便查看容器内产生的数据
- 多容器间实现数据共享
两种管理方式
- 数据卷
- 数据卷容器
数据卷
- 数据卷是一个提供容器使用的特殊目录
数据卷容器
- 数据卷容器就是一个普通的容器
数据卷共享(宿主机跟容器的共享)
[[email protected] ~]# docker pull centos //下载镜像
[[email protected] ~]# docker run -v /var/www:/data1 --name web1 -it centos /bin/bash //关联宿主机和容器的共享
[[email protected] /]# cd data1/
[[email protected] data1]# echo "123" > test01.txt
[[email protected] data1]# exit
exit
[[email protected] ~]# cat /var/www/test01.txt
123
数据卷容器共享(容器跟容器)
[[email protected] ~]# docker run --name web100 -v /data1 -v /data2 -it centos /bin/bash //创建一个web100容器并分别有两个卷为data1,2
[[email protected] /]# cd data1/
[[email protected] data1]# echo "111" > 111.txt //分别在目录中写入内容
[[email protected] data1]# cd ../data2/
[[email protected] data2]# echo "222" > 222.txt
[[email protected] data2]# exit
exit
[[email protected] ~]# docker run -it --volumes-from web100 -it centos /bin/bash //将新容器挂载数据卷容器web100
[[email protected] /]# cat data1/111.txt
111
[[email protected] /]# cat data2/222.txt
222
原文地址:https://blog.51cto.com/14473285/2469886
时间: 2024-10-08 00:33:55