Chinaunix首页 | 论坛 | 博客
  • 博客访问: 201530
  • 博文数量: 111
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 790
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-29 13:41
文章分类

全部博文(111)

文章存档

2018年(10)

2017年(27)

2016年(18)

2015年(31)

2014年(25)

分类: 虚拟化

2017-11-13 16:21:51

在这里使用的OS是ubuntu16:04,所以不需要安装nsenter。

点击(此处)折叠或打开

  1. #运行一个容器
  2. # docker run -itd ubuntu:16.04 /bin/bash
  3. # docker ps
  4. a5bb8d87ceff        ubuntu:16.04        "/bin/bash"              14 minutes ago      Up 9 minutes                                 goofy_bartik
  5. #
  6. #为了连接到容器,需要找到容器的第一个进程(这里容器的第一个进程是/bin/bash)的PID
  7. # PID=$(docker inspect --format "{{ .State.Pid }}" a5bb8d87ceff)
  8. # nsenter --target $PID --mount --uts --ipc --net --pid
  9. mesg: ttyname failed: No such file or directory
  10. root@a5bb8d87ceff:/


  11. 或者下载.bashrc_docker脚本
  12. wget -~ https://github.com/yeasy/docker_practice/raw/master/_local/.bashrc_docker
  13. echo "[ -f ~/.bashrc_docker ] && . ~/.bashrc_docker" >> ~/.bashrc;source ~/.bashrc
  14. # docker inspect --format '{{.State.Pid}}' a5bb8d87ceff
  15. # docker inspect --format '{{ .NetworkSettings.IPAddress }}' a5bb8d87ceff
  16. #查看容器第一个进程的pid
  17. # echo $(docker-pid a5bb8d87ceff)
  18. #进入容器
  19. # docker-enter a5bb8d87ceff pwd

.
bashrc_docker内容如下:

点击(此处)折叠或打开

  1. # Some useful commands to use docker.
  2. # Author: yeasy@github
  3. # Created:2014-09-25

  4. #.bashrc_docker定义了docker-pid和docker-ip两个命令,为了方便查看pid和ip信息。
  5. alias docker-pid="sudo docker inspect --format '{{.State.Pid}}'"
  6. alias docker-ip="sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}'"

  7. #the implementation refs from https://github.com/jpetazzo/nsenter/blob/master/docker-enter
  8. function docker-enter() {
  9.     #if [ -e $(dirname "$0")/nsenter ]; then
  10.     #Change for centos bash running
  11.     if [ -e $(dirname '$0')/nsenter ]; then
  12.         # with boot2docker, nsenter is not in the PATH but it is in the same folder
  13.         NSENTER=$(dirname "$0")/nsenter
  14.     else
  15.         # if nsenter has already been installed with path notified, here will be clarified
  16.         NSENTER=$(which nsenter)
  17.         #NSENTER=nsenter
  18.     fi
  19.     [ -z "$NSENTER" ] && echo "WARN Cannot find nsenter" && return

  20.     if [ -z "$1" ]; then
  21.         echo "Usage: `basename "$0"` CONTAINER [COMMAND [ARG]...]"
  22.         echo ""
  23.         echo "Enters the Docker CONTAINER and executes the specified COMMAND."
  24.         echo "If COMMAND is not specified, runs an interactive shell in CONTAINER."
  25.     else
  26.         PID=$(sudo docker inspect --format "{{.State.Pid}}" "$1")
  27.         if [ -z "$PID" ]; then
  28.             echo "WARN Cannot find the given container"
  29.             return
  30.         fi
  31.         shift

  32.         OPTS="--target $PID --mount --uts --ipc --net --pid"

  33.         if [ -z "$1" ]; then
  34.             # No command given.
  35.             # Use su to clear all host environment variables except for TERM,
  36.             # initialize the environment variables HOME, SHELL, USER, LOGNAME, PATH,
  37.             # and start a login shell.
  38.             #sudo $NSENTER "$OPTS" su - root
  39.             sudo $NSENTER --target $PID --mount --uts --ipc --net --pid su - root
  40.         else
  41.             # Use env to clear all host environment variables.
  42.             sudo $NSENTER --target $PID --mount --uts --ipc --net --pid env -i $@
  43.         fi
  44.     fi
  45. }



阅读(400) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~