6
6

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.

docker-enter (docker-attach) スクリプト

Last updated at Posted at 2014-06-28

nsenter を簡単に使えるようにするスクリプト docker-enter (docker-attch) を書きました。

そして丁度リリースされた CoreOS Alpha の最新版 361.0.0 に搭載しました。
yungsang/coreos-alpha

追記:boot2docker にも搭載しました。
yungsang/boot2docker

docker-enter
#!/bin/sh

print_usage() {
  echo "Usage: $(basename $0) <container id/name> [{command:-$SHELL} [argments...]]"
}

ID="$1"
if [ -z "$ID" ] ; then
  print_usage >&2
  exit 1
fi
shift

PID=$(docker inspect --format '{{.State.Pid}}' "$ID" 2> /dev/null)
if [ -z "$PID" -o "$PID" = "0" ] ; then
  echo "$ID: no such container is running" >&2
  exit 1
fi

echo nsenter --target $PID --mount --uts --ipc --net --pid -- "$@"
sudo nsenter --target $PID --mount --uts --ipc --net --pid -- "$@"

最後の $@ の処理がちょっと気にはなってます。
-- $* に修正しました。
sh -c 元々要らなかったのですっきりしました。

Cf.) https://github.com/YungSang/docker-attach

$ vagrant init yungsang/coreos-alpha -m
$ vagrant up
$ vagrant ssh
   ______                ____  _____
  / ____/___  ________  / __ \/ ___/
 / /   / __ \/ ___/ _ \/ / / /\__ \
/ /___/ /_/ / /  /  __/ /_/ /___/ /
\____/\____/_/   \___/\____//____/
(alpha 361.0.0)
core@localhost ~ $ docker-enter
Usage: docker-enter <container id/name> [{command:-/bin/bash} [argments...]]
core@localhost ~ $ docker run -d yungsang/busybox nc -p 8080 -l -l -e echo hello world!
core@localhost ~ $ docker ps -l
CONTAINER ID        IMAGE                     COMMAND                CREATED             STATUS              PORTS               NAMES
1a1f6106a222        yungsang/busybox:latest   nc -p 8080 -l -l -e    27 seconds ago      Up 27 seconds                           dreamy_kowalevski
core@localhost ~ $ docker-enter $(docker ps -l -q) sh
nsenter --target 636 --mount --uts --ipc --net --pid sh
/ # ps
PID   USER     COMMAND
    1 root     nc -p 8080 -l -l -e echo hello world!
    7 root     sh
    8 root     ps
/ # exit
core@localhost ~ $ exit
$ vagrant ssh -c 'docker-enter $(docker ps -l -q) sh'
nsenter --target 636 --mount --uts --ipc --net --pid sh
/ # exit
$ 
6
6
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
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?