zoukankan      html  css  js  c++  java
  • Docker Tips : get a shell on the host

    https://medium.com/lucjuggery/a-container-to-access-the-shell-of-the-host-2c7c227c64e9
    If you are using Docker on MacOS or Windows 10 Pro, chances are that you use the dedicated Docker for Mac or Docker for Windows products. Both of them use a hypervisor (xhyve / HyperV) to run a VM with the Docker daemon. The Docker client running on the host sends requests to the Docker daemon running inside the VM.
    It’s useful from time to time to get a shell directly in the VM. For instance, this can be helpful is you want to explore the /var/lib/docker folder where the Docker daemon stores all its stuff, to inspect the network interfaces / bridges created in the VM, or for other debug purposes. In this context you may already have seen / use the the following command :
    $ docker run --privileged --pid=host -it alpine:3.8
    nsenter -t 1 -m -u -n -i sh
    It basically runs a container, based on the Alpine image, and launches a nsenter command with a bunch of parameters.
    Let’s review the options used to better understand how this is working under the hood. At first, there are the options provided to the docker run command :
    --rm : removes the container after it is stopped
    -ti (or -t -i) : adds a tty and leaves the standard input opened
    --privileged : grants additional permissions to the container, it allows the container to gain access to the devices of the host (/dev)
    --pid=host : allows the containers to use the processus tree of the Docker host (the VM in which the Docker daemon is running)
    From the options above, we know the newly created container will run in the host’s process namespace and then will be able to view all the process running on the host (among them the process with PID 1).
    Let’s now check the available options of the nsenter utility, which allows to run a process in existing namespaces (the building blocks that provide isolation to containers).
    $ docker run alpine:3.8 nsenter --help
    BusyBox v1.28.4 (2018–07–17 15:21:40 UTC) multi-call binary.
    Usage: nsenter [OPTIONS] [PROG [ARGS]]
    -t PID Target process to get namespaces from
    -m[FILE] Enter mount namespace
    -u[FILE] Enter UTS namespace (hostname etc)
    -i[FILE] Enter System V IPC namespace
    -n[FILE] Enter network namespace
    -p[FILE] Enter pid namespace
    -U[FILE] Enter user namespace
    -S UID Set uid in entered namespace
    -G GID Set gid in entered namespace
    —-preserve-credentials Don’t touch uids or gids
    -r[DIR] Set root directory
    -w[DIR] Set working directory
    -F Don’t fork before exec’ing PROG
    As we can see from this list, the options provided to nsenter (-t 1 -m -u -n -i sh) allows to run the process sh in the same isolation context as the process with PID 1. The whole command will then provide an interactive sh shell in the VM.

  • 相关阅读:
    android selector下的设置背景属性值
    成功必备的15种心态
    saveInstallState参数使用详解(android activity状态保存和恢复)
    14个坏习惯可能让你丢掉工作
    如何找到好书?有什么技巧或建议?
    [转]Git详解之一 Git起步
    程序员技术练级攻略
    Sina微博OAuth2框架解密
    程序员的八个级别
    Android中的Layout_weight详解
  • 原文地址:https://www.cnblogs.com/double12gzh/p/11981815.html
Copyright © 2011-2022 走看看