Docker容器

一.容器运行

1.基于镜像启动一个容器,并打印当月目录

[root@master ~]# docker run centos cal

2.用ps命令查看运行中的容器

[root@master ~]# docker ps -a

3.通过指定参数,启动bash交互端

[root@master ~]# docker run -it centos /bin/bash
[root@2b70db942d10 /]# cal
[root@2b70db942d10 /]# exit
[root@master ~]# docker ps -a

4.启动一个容器,使用终端进行操作

[root@master ~]# docker run -it centos
[root@0c81ff0de186 /]# ls

5.运行一个容器,为其设置环境变量

[root@master ~]# docker run -d -it -e key=1000 centos

6.查看创建容器的环境变量

7.自动重启的容器

(1)正常运行一个容器,使用exit命令退出,容器也将处于终止状态

[root@master ~]# docker run -it centos
[root@147e8ef5ca69 /]# exit
[root@master ~]# docker ps -a

(2)为运行代码添加参数,退出后容器重启,处于运行状态

[root@master ~]# docker run -it --restart=always centos
[root@c672119903fa /]# exit
[root@master ~]# docker ps -a

8.自定义名称的容器

[root@master ~]# docker run -d --name=text centos
[root@master ~]# docker ps -a

9.开启端口的容器

(1)创建一个开启80端口的容器

[root@master ~]# docker run -d -p 80:80 nginx
[root@master ~]# docker ps -a

(2)使用curl工具访问容器端口

[root@master ~]# curl -I 192.168.88.141:80

(3)将容器终止,并再次访问端口

[root@master ~]# docker stop 19e
[root@master ~]# curl -I 192.168.88.141:80

10.与宿主机共享目录的容器

(1)在宿主机上创建需要共享的目录与文件

[root@master ~]# mkdir test
[root@master ~]# touch /root/test/a.txt /root/test/b.txt
[root@master ~]# ls /root/test/

(2)创建共享这两个文件的容器

[root@master ~]# docker run -it -v /root/test/:/root/test/ --privileged docker.io/nginx /bin/bash
root@b7572cde6cf4:/# ls /root/test/

二.进入容器

1.容器的3种状态

(1)运行状态

运行一个容器并查看状态

[root@master ~]# docker run -d -p 80:80 --name test-nginx docker.io/nginx
[root@master ~]# docker ps -a

(2)暂停状态

通过命令使容器进入暂停状态

[root@master ~]# docker pause test-nginx
[root@master ~]# docker ps -a

用curl对容器进行访问测试

[root@master ~]# curl -I 192.168.88.141

无法访问,但没有拒绝连接,说明暂停容器本质是暂停容器中的服务

使用docker unpasue命令使暂停的容器停止暂停状态

[root@master ~]# docker unpause b669dc555c12
[root@master ~]# docker ps -a

再次使用curl对容器进行访问测试

[root@master ~]# curl -I 192.168.88.141

(3)终止状态

当不需要一个业务运行时,可以通过命令将容器终止

[root@master ~]# docker stop test-nginx
[root@master ~]# docker ps -a

使用curl对终止的容器进行访问测试

[root@master ~]# curl -I 192.168.88.141

客户端请求被拒接,终止状态的容器会给客户端发送拒绝回应

使用docker start 唤醒终止的容器

[root@master ~]# docker start test-nginx
[root@master ~]# docker ps -a

再次使用curl对容器进行访问测试

[root@master ~]# curl -I 192.168.88.141

2.docker attach 与 docker exec

(1)docker attach

任意创建一个容器

[root@master ~]# docker run -it -d centos /bin/bash

使用docker attach进入容器

[root@master ~]# docker attach 238
[root@238e717e99db /]# ls

退出容器查看容器是否被终止

[root@238e717e99db /]# exit
[root@master ~]# docker ps -a

使用Ctrl+P+Q组合键退出并查看容器状态

[root@master ~]# docker start 238
[root@master ~]# docker ps
[root@master ~]# docker attach 238
[root@238e717e99db /]# ls
[root@238e717e99db /]# read escape sequence
[root@master ~]# docker ps -a

(2)docker exec

使用docker exec 在宿主机上向运行的容器传输命令

[root@master ~]# docker exec 238 ls

创建一个新容器,使用命令对容器进行操作

[root@master ~]# docker exec -it 238 /bin/bash
[root@238e717e99db /]# w
[root@238e717e99db /]# date

退出容器查看容器状态

[root@238e717e99db /]# exit
[root@master ~]# docker ps -a

注:docker attach 使用exit退出将终止容器,docker exec 则不会终止容器,只会退出bash端

三.停止和删除容器

1.停止容器

docker kill 命令强制终止容器

[root@master ~]# docker kill b66
[root@master ~]# docker ps -a

查看运行状态的ID号

[root@master ~]# docker ps -q

使用正则表达式根据运行状态容器ID号关闭正在运行的容器

[root@master ~]# docker stop `docker ps -q`

该命令还有另一张方式

[root@master ~]# docker stop `docker ps -a | grep Up | awk '{print $1}'`

2.删除容器

(1)方法一

删除处于终止状态容器

[root@master ~]# docker rm `docker ps -a | grep Exited | awk '{print $1}'`

(2)方法二

删除终止状态容器

[root@master ~]# docker rm `docker ps -a -q`

强制删除容器(可删除运行状态容器)
 

[root@master ~]# docker rm -f `docker ps -a -q`

(3)方法三

筛选出处于终止状态的容器并删除

[root@master ~]# docker rm $(docker ps -q -f status=exited)

(4)方法四

删除所有处于终止状态的容器

[root@master ~]# docker container prune

四.容器资源限制

 1.限制容器内存资源

使用progrium/stress镜像来模拟压力测试

[root@master ~]# docker run -it -m 300M --memory-swap=400M progrium/stress --vm 1 --vm-bytes 308M

内存超出限额情况

[root@master ~]# docker run -it -m 300M --memory-swap=400M progrium/stress --vm 1 --vm-bytes 450M

2.限制容器CPU资源

运行两个容器test01与test02,并设置CPU权重

[root@master ~]# docker run -it -d -c 1000 --name test01 docker.io/progrium/stress --cpu 2
[root@master ~]# docker run -it -d -c 2000 --name test02 docker.io/progrium/stress --cpu 2

查看容器占用CPU情况

将test02容器暂停再查看CPU使用情况

[root@master ~]# docker ps  -a
[root@master ~]# docker pause test02
[root@master ~]# docker stats

使用--cpuset-cpus参数设置CPU资源限制,指定某一颗CPU将其用满,使用镜像agileek、cpuset-test进行测试

[root@master ~]# docker run -tid --name test -h test --cpuset-cpus=0 centos
[root@master ~]# top

3.限制容器Block I/O

运行两个容器test01与test02,test01读写磁盘的宽带是test02的两倍

[root@master ~]# docker run -d --name test001 --blkio-weight 800 centos
[root@master ~]# docker run -d --name test002 --blkio-weight 400 centos

运行一个容器,限制对、dev/sda 写入速率不高于20MB/s,并查看该容器的写入速率

[root@master ~]# docker run -it --device-write-bps /dev/sda:20MB centos
[root@a56d8d676e30 /]# time dd if=/dev/zero of=test bs=1M count=800 oflag=direct

运行一个不限制写入速率的容器进行比较

[root@master ~]# docker run -it centos
[root@7187053b03c3 /]# time dd if=/dev/zero of=test bs=1M count=800 oflag=direct

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值