牛刀小试之docker容器系列(一)

牛刀小试之docker容器的连接方式

牛刀小试之docker容器系列(一)

牛刀小试之docker容器的连接方式

docker容器的连接方式:

	容器有三种连接方式,attach exec nsenter工具,其他工具比较简单我们重点讲解下nsenter工具。 

	1.首先我们已后台的方式登陆到容器。
    [root@docker ~]# docker run -idt ubuntu
    7d3d35cb9c40388aa799ba922f92e6f54f27c3a889a8c28d0feb42bf060766f4
    You have new mail in /var/spool/mail/root
    [root@docker ~]#
    2.查看容器是不是启动
    [root@docker ~]# docker ps
    CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
    7d3d35cb9c40        ubuntu:14.04        "/bin/bash"            8 seconds ago       Up 7 seconds                            elegant_brattain
    6aa9905ab9bc        ubuntu:14.04        "/bin/bash -c 'while   10 minutes ago      Up 5 minutes                            jovial_elion
    [root@docker ~]# docker stop 6aa
    6aa
    [root@docker ~]#

exec用法:

    docker 1.3 版本之后自带命令,可以在容器直接运行命令,启动一个bash,
    exec工具相对比较简单,
    [root@docker ~]# docker exec -ti 7d3d35cb9c40 /bin/bash
    root@7d3d35cb9c40:/#

nsenter工具安装使用

    1.首先编译安装
    [root@test-devops ~]# wget https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz
    [root@test-devops ~]# tar xf util-linux-2.24.tar.gz
    [root@test-devops ~]# cd util-linux-2.24
    [root@test-devops util-linux-2.24]# ./configure --without-ncurses
    [root@test-devops util-linux-2.24]# make nsenter
    [root@test-devops util-linux-2.24]# cp nsenter /usr/local/bin/
    
    2.把如下内容复制到“/usr/local/bin/docker-enter”下面。
	[root@test-devops ~]# cat /usr/local/bin/docker-enter
    #!/bin/sh

    if [ -e $(dirname "$0")/nsenter ]; then
        # with boot2docker, nsenter is not in the PATH but it is in the same folder
        NSENTER=$(dirname "$0")/nsenter
    else
        NSENTER=nsenter
    fi

    if [ -z "$1" ]; then
        echo "Usage: `basename "$0"` CONTAINER [COMMAND [ARG]...]"
        echo ""
        echo "Enters the Docker CONTAINER and executes the specified COMMAND."
        echo "If COMMAND is not specified, runs an interactive shell in CONTAINER."
    else
        PID=$(docker inspect --format "u"{% raw %}"{{.State.Pid}}" "$1") #可以去掉u"{% raw %}"
        if [ -z "$PID" ]; then
            exit 1
        fi
        shift

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

        if [ -z "$1" ]; then
            # No command given.
            # Use su to clear all host environment variables except for TERM,
            # initialize the environment variables HOME, SHELL, USER, LOGNAME, PATH,
            # and start a login shell.
            "$NSENTER" $OPTS su - root
        else
            # Use env to clear all host environment variables.
            "$NSENTER" $OPTS env --ignore-environment -- "$@"
        fi
    fi
	[root@test-devops ~]#

    3.授权测试登陆
    [root@test-devops util-linux-2.24]# chmod +x /usr/local/bin/docker-enter
    [root@test-devops util-linux-2.24]# docker-enter 4a5 登陆

    4.如下是一个完成的登录测试方式,后台输出启动docker容器。
    [root@docker tmp]# docker run -idt ubuntu
    a457769fdd921165c956b99e47d86952a3b0dbc50067ce535b6e81f24c4ef21c
    [root@docker tmp]# docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    a457769fdd92        ubuntu:14.04        "/bin/bash"         10 seconds ago      Up 10 seconds                           backstabbing_fermi
    7d3d35cb9c40        ubuntu:14.04        "/bin/bash"         16 minutes ago      Up 16 minutes                           elegant_brattain
    [root@docker tmp]#
    5.使用docker-enter就可以连接了
    [root@docker tmp]# docker-enter a457769fdd92
    root@a457769fdd92:/# ls
    bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    root@a457769fdd92:/# echo "Hello world!"
    Hello world!
    root@a457769fdd92:/# exit
    [root@docker tmp]#
docker 

See also