重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
docker容器是什么?一般大家对docker容器的了解可能停留在概念的层面上,而对于docker容器的应用场景和使用方法了解相对较少。今天就跟大家聊聊docker容器的具体使用。
主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、成都响应式网站建设公司、程序开发、微网站、微信平台小程序开发等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的成都网站建设、成都网站制作、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体,具备承接不同规模与类型的建设项目的能力。
[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
[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服务器上
[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
[root@localhost opt]# docker tag nginx:latest nginx:web //修改标签
[root@localhost opt]# docker login //登录docker(你需要注册)
Username: //用户名
Password: //密码
[root@localhost opt]# docker push nginx:web //上传公有仓库
[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 //批量删除容器
[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
--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
[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容器介绍和使用的详细内容了,看完之后是否有所收获呢?如果想了解更多相关内容,欢迎关注创新互联行业资讯!