OSX上のdockerコンテナにssh接続する方法(boot2docker)
-
21-12-2019 - |
質問
OSX上でdockerを使用しています boot2docker.
ターミナルから実行中のコンテナに Ssh 接続を取得したいと考えています。
しかし、私にはこれができません:(
Docker が仮想マシンで実行されているためだと思います。
解決
VMで実行されているコンテナにSSHを実行できるようにする必要があります。
- コンテナ内の
sshd
をインストールして実行します(例)。コンテナは通常1つのプロセスのみを実行するため、sshd
はデフォルトではありません。 -
EXPOSE
画像を作成する部分、通常は22のポートを作成するので、コンテナを実行すると、デーモンはコンテナ内のEXPOSE
のポートに接続し、コンテナの外側に何かを露出させることができます。 - コンテナを実行すると、そのポートをマッピングする方法を決める必要があります。あなたはDockerを自動的にそれをすることも明示的にさせることができます。私は明示的なことをお勧めします。
- ポートマップをVMに追加して、ポートをホストに公開します。例えばVMが実行されていない場合は、次のようなマッピングを追加できます.
docker run -p 42222:22 ...
それからあなたのホストから、ホスト上のポート42222にSSHをSSHにしてコンテナのsshデーモンに到達することができなければなりません。
上記の手順を実行するとどうなるのか:
$ VBoxManage modifyvm "boot2docker-vm" --natpf1 "containerssh,tcp,,42222,,42222"
$ ./boot2docker start
[2014-04-11 12:07:35] Starting boot2docker-vm...
[2014-04-11 12:07:55] Started.
$ docker run -d -p 42222:22 dhrp/sshd
Unable to find image 'dhrp/sshd' (tag: latest) locally
Pulling repository dhrp/sshd
2bbfe079a942: Download complete
c8a2228805bc: Download complete
8dbd9e392a96: Download complete
11d214c1b26a: Download complete
27cf78414709: Download complete
b750fe79269d: Download complete
cf7e766468fc: Download complete
082189640622: Download complete
fa822d12ee30: Download complete
1522e919ec9f: Download complete
fa594d99163a: Download complete
1bd442970c79: Download complete
0fda9de88c63: Download complete
86e22a5fdce6: Download complete
79d05cb13124: Download complete
ac72e4b531bc: Download complete
26e4b94e5a13b4bb924ef57548bb17ba03444ca003128092b5fbe344110f2e4c
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
26e4b94e5a13 dhrp/sshd:latest /usr/sbin/sshd -D 6 seconds ago Up 3 seconds 0.0.0.0:42222->22/tcp loving_einstein
$ ssh root@localhost -p 42222
The authenticity of host '[localhost]:42222 ([127.0.0.1]:42222)' can't be established.
RSA key fingerprint is ....
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:42222' (RSA) to the list of known hosts.
root@localhost's password: screencast
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.12.1-tinycore64 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@26e4b94e5a13:~# exit
logout
.
SHSH-> Localhost 4222-> VMポート42222->コンテナポート22を表示します。
他のヒント
DockerはDocker 1.3.0にdocker exec
コマンドを追加しました。次のものを使用して実行中のコンテナに接続できます。
docker exec -it <container id> /bin/bash
.
ランニングコンテナのBASHプロンプトに接続します。
走行中のコンテナに入りたい場合は、nsenter
。これは単純なbashスクリプトです(Dockerコンテナに入るために使用できるChris Jones 。Docker-Enterと$PATH
chmod +x
のどこかに保存します。
#!/bin/bash
set-e
# Check for nsenter. If not found, install it
boot2docker ssh '[ -f /var/lib/boot2docker/nsenter ] || docker run --rm -v /var/lib/boot2docker/:/target jpetazzo/nsenter'
# Use bash if no command is specified
args=$@
if[[ $# = 1 ]]; then
args+=(/bin/bash)
fi
boot2docker ssh -t sudo /var/lib/boot2docker/docker-enter "${args[@]}"
.
その後、docker-enter 89af3d
(または入力したい構成)
入力したいコンテナを必要とするマイケルの回答のわずかに修正されたバリアント(appname):
boot2docker ssh '[ -f /var/lib/boot2docker/nsenter ] || docker run --rm -v /var/lib/boot2docker/:/target jpetazzo/nsenter'
boot2docker ssh -t sudo /var/lib/boot2docker/docker-enter $(docker ps | grep $APPNAME | awk '{ print $1 }')
. 同じ OS、Docker 18.09.2 を搭載したホスト上で実行されている Ubuntu 16.04 イメージに対してこれをテストしました。わずかな変更を加えれば boot2Docker でも動作するはずです。
イメージを構築します。バックグラウンドコンテナで実行します(あなたのユーザー ルートである可能性があります):
$ docker run -ditu <youruser> <imageId>
シェルを使用してそれにアタッチします。
$ docker exec -it <containerId> /bin/bash
openssh サーバーをインストールします (sudo
ユーザーが root ではない場合にのみ必要です。コマンドは boot2Docker では異なる場合があります)。
$ sudo apt-get install -y openssh-server
それを実行します:
$ sudo service ssh start
(次の手順はオプションです。ユーザーがパスワードを持っている場合は、それをスキップして、SSH 接続のたびにパスワードを入力できます)。
クライアント ホストで RSA キーを作成します。
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/youruser/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/youruser/.ssh/id_rsa.
Your public key has been saved in /home/youruser/.ssh/id_rsa.pub.
docker イメージ上にディレクトリを作成します $HOME/.ssh
:
$ cd
$ mkdir .ssh && cd .ssh
$ vi authorized_keys
の内容をコピーして貼り付けます $HOME/.ssh/id_rsa.pub
クライアントマシン上で authorized_keys
docker イメージ上でファイルを保存します。
(オプションのステップの終了)。
イメージの IP アドレスを書き留めます。
$ cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 63448863ac39
^^^^^^^^^^ this
これで、クライアント ホストからの接続が有効になるはずです。
$ ssh 172.17.0.2
Enter passphrase for key '/home/youruser/.ssh/id_rsa':
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.15.0-46-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Last login: Fri Apr 5 09:50:30 2019 from 172.17.0.1
もちろん、上記の手順を Dockerfile に非対話的に適用することもできます。