LoginSignup
9
9

More than 5 years have passed since last update.

boot2docker + NFS + nsenter = yungsang/boot2docker

Last updated at Posted at 2014-06-26

docker - nsinit が上手く行かなくて困っていたら nsenter があまりにもあっさりだった件 - Qiita
は、かなり恥ずかしいエントリになってしまいましたが、気を取り直して、昨日の成果をまとめて活かしたいと思います。

  • yungsang/boot2dockernsenter を標準搭載
  • nsenter を簡単に使う為のスクリプト docker-enter も付属

以下がお試しする為の Vagrantfile になります。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "yungsang/boot2docker"

  config.vm.box_version = ">= 1.0.3"

  config.vm.provision :docker do |d|
    d.pull_images "yungsang/busybox"
    d.run "simple-echo",
      image: "yungsang/busybox",
      args: "-p 8080:8080",
      cmd: "nc -p 8080 -l -l -e echo hello world!"
  end

  config.vm.network :forwarded_port, guest: 8080, host: 8080
end

上記の Vagrantfile を保存して、boot2docker を起動します。

$ vagrant up
$ export DOCKER_HOST="tcp://localhost:2375"
$ docker ps -l
CONTAINER ID        IMAGE                     COMMAND                CREATED             STATUS              PORTS                    NAMES
5680c4535af4        yungsang/busybox:latest   nc -p 8080 -l -l -e    21 seconds ago      Up 18 seconds       0.0.0.0:8080->8080/tcp   simple-echo
$ nc localhost
hello world!

hello world! をエコーするだけのコンテナが立ち上がりました。

boot2docker に ssh して、docker-enter を使ってコンテナにログインしてみます。

$ vagrant ssh
                        ##        .
                  ## ## ##       ==
               ## ## ## ##      ===
           /""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
           \______ o          __/
             \    \        __/
              \____\______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
boot2docker: 1.0.1
             nfs+nsenter : abe1fc0 - Thu Jun 26 05:52:00 UTC 2014
docker@boot2docker:~$ docker-enter
Usage: docker-enter <container id/name> [{command:-/bin/sh} [argments...]]
docker@boot2docker:~$ docker-enter $(docker ps -l -q)
nsenter --target 958 --mount --uts --ipc --net --pid --
# ps
PID   USER     COMMAND
    1 root     nc -p 8080 -l -l -e echo hello world!
   11 root     sh
   12 root     ps
# exit
docker@boot2docker:~$ exit
$ 

ちなみに

vagrant ssh -c を使えばローカルマシンからもログインできちゃいます!

$ vagrant ssh -c 'docker-enter $(docker ps -l -q)'
nsenter --target 958 --mount --uts --ipc --net --pid --
# ps
PID   USER     COMMAND
    1 root     nc -p 8080 -l -l -e echo hello world!
    9 root     sh
   10 root     ps
# exit
$ 
9
9
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
9
9