attach命令

#docker attach 容器名。attach类似于vnc,操作会在每个容器界面显示

进入到正在运行的容器命令操作

exec命令

执行单次命令并进入容器,exit退出后容器继续运行

[[email protected] ~]# docker exec -it nginx-test-port2 /bin/bash

nsenter命令

nsenter命令需要通过pid进入到容器内部

[[email protected] ~]# yum install util-linux -y   #安装nsenter命令

[[email protected] ~]# docker inspect -f "{{.NetworkSettings.IPAddress}}" 51d1e9fc93fa   #根据CONTAINER ID获取IP地址
172.17.0.8

[[email protected] ~]# docker inspect -f "{{.State.Pid}}" nginx-test-port2     #根据NAMES获取PID
12278

[[email protected] ~]# nsenter -t 12278 -m -u -i -n -p  
[email protected]:/# exit

#脚本执行

[[email protected] ~]# cat docker-in.sh 
#!/bin/bash
#Cookie 2020/2/20
docker_in(){
   NAMES=$1
   PID=$(docker inspect -f "{{.State.Pid}}" ${NAMES})
   nsenter -t ${PID} -m -u -i -n -p

}
docker_in $1

[[email protected] ~]# ./docker-in.sh nginx-test-port2
[email protected]:/# exit
logout
[[email protected] ~]# ./docker-in.sh nginx-test-port2
[email protected]:/# 
[email protected]:/# exit
logout
[[email protected] ~]#

 

[[email protected] ~]# docker stop $(docker ps -a -q)    #批量关闭所有正在运行的容器

[[email protected] ~]# docker kill $(docker ps -a -q)     #批量强制关闭所有正在运行的容器

[[email protected] ~]# docker rm -f `docker ps -aq -f status=exited`  #批量删除已退出容器

 

指定容器DNS

[[email protected] ~]# docker run -it --rm --dns 223.6.6.6 centos bash
[[email protected] /]# cat /etc/resolv.conf 
nameserver 223.6.6.6
[[email protected] /]# 

相关文章:

  • 2022-12-23
  • 2021-11-16
  • 2022-12-23
  • 2021-08-17
  • 2021-12-26
  • 2021-08-23
  • 2021-08-14
猜你喜欢
  • 2022-12-23
  • 2021-05-16
  • 2022-01-03
  • 2022-12-23
  • 2022-12-23
  • 2022-03-04
  • 2022-12-23
相关资源
相似解决方案