Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
10
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

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

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
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
10
Help us understand the problem. What are the problem?