boot2docker 内のコンテナに ssh で接続する
イントロ
最近ようやく docker を触り始めています。
にわか丸出しなので、【翻訳】Dockerコンテナ内でSSHDを実行してはいけない理由 | POSTD のような記事を見かけたのにも関わらず、ssh で接続しようとしています。
理由は serverspec
使ってコンテナのテストしたいと考えたからです。
(ssh
接続しないとできない... はず)
方法
Dockerfile
FROM ubuntu:14.04 RUN apt-get update RUN apt-get install -y openssh-server RUN mkdir /var/run/sshd RUN echo 'root:root' | chpasswd RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config EXPOSE 22 CMD /usr/sbin/sshd -D
build
% docker build -t sshd . % docker port $(docker run -d -P openssh) 22 #=> 0.0.0.0:49155 % docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5d215294c5cf sshd:latest "/bin/sh -c '/usr/sb 17 seconds ago Up 16 seconds 0.0.0.0:49155->22/tcp boring_hodgkin
無事コンテナが作れた。
docker run
に -P
オプションを渡さないと expose
された port が公開されないらしく、若干ハマった。(https://docs.docker.com/reference/run/#expose-incoming-ports)
docker port
コマンド実行結果の port 部分を覚えておく。
接続してみる
-p
の引数は先ほどメモった docker port
コマンド実行結果を渡す。
% ssh root@$(boot2docker ip) -p 49155 The authenticity of host '[192.168.59.103]:49155 ([192.168.59.103]:49155)' can't be established. RSA key fingerprint is 6c:52:de:f7:85:8f:29:69:8c:5c:78:d4:ec:43:3d:69. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '[192.168.59.103]:49155' (RSA) to the list of known hosts. root@192.168.59.103's password: Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.2.0-76-generic x86_64) * Documentation: https://help.ubuntu.com/ The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. root@5d215294c5cf:~#
入れたー
おまけ
boot2docker
で試した理由は、docker
周辺の仕組みを理解したかったからであって、boot2docker
でないといけない理由はありません。
後、観測範囲では、何故か boot2docker
で試した例が見当たらなかったから。