6
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

nsenterでコンテナに接続するのに便利なコマンド

Last updated at Posted at 2014-07-12

docker - Failed to get D-Bus connection: No connection to service manager.の対処 - Qiita

nsenterとは

  • nsenterを使えばdocker runした後にそのコンテナにattachして他のプログラムを起動できる。
  • なお、コンテナがstop状態ではPidが0なので接続出来ない。

やり方

コンテナIDを確認
# docker ps -a
CONTAINER ID        IMAGE             
7c50b96fccd0        tukiyo3/centos7-ja
  • 直近のはdocker ps -qでもわかる
コンテナのPidを調べる
# docker inspect 7 | grep -i pid
        "Pid": 9240,
接続してbash起動
# nsenter -t 9240 -m -u -i -n -p -- /bin/bash
nsenter
/bin/bash
bash-4.2# pwd
/
bash-4.2# exit

.bashrcに書くと便利

Pid取得
docker inspect $(d ps -q) | awk '/Pid/{print int($2)}'
上記をalias設定
alias dpid='docker inspect $(d ps -q) | awk "/Pid/{print int(\$2)}"'
aliasを使って接続
nsenter -t $(dpid) -m -u -i -n -p -- /bin/bash
コマンド一つで接続したい場合
$ alias nconnect='sudo nsenter -t $(dpid) -m -u -i -n -p -- /bin/bash'
$ nconnect
bash-4.2#

サクッとnsenterで接続したい

nsconnect() {
    DOCKER_PID=$(docker inspect --format '{{.State.Pid}}' $(docker ps -ql))
    sudo nsenter -t ${DOCKER_PID} -m -u -i -n -p /bin/sh
}
6
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?