概要
[phusion/baseimage-docker] (https://github.com/phusion/baseimage-docker)で公開されてるUbuntuベースのDockerイメージを元に、sshdを有効にしたdockerイメージを作成してみます。
環境
下記の環境で動作を確認しました。
- Windows7 (64bit)
- Docker 1.8.1
- phusion/baseimage 0.9.17
参考
下記のサイトを参考にさせて頂きました。
- [phusion/baseimage-docker] (https://github.com/phusion/baseimage-docker)
- [Docker 初心者は phusion/baseimage-docker を使おう] (http://blog.kakipo.com/use-phusion-baseimage-docker/)
- [Docker上にSSH接続できるコンテナを作成する] (http://flexibleinfla.blogspot.jp/2015/01/dockerssh.html)
- [How to connecting to your Docker container with SSH and putty] (http://tteckie.blogspot.jp/2014/08/how-to-connecting-to-your-docker.html)
Dockerイメージの作成
認証鍵の作成
Windows側でPUTTYGEN.EXEを使用して認証鍵を作成します。
パスフレーズは設定しました。
ファイル名は下記のようにしました。
- 公開鍵: id_rsa.pub
- 秘密鍵: id_rsa.ppk
ベースのimageを取得
docker仮想マシンにログインして最新バージョンのイメージを取得します。
$ docker pull phusion/baseimage:0.9.17
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
phusion/baseimage 0.9.17 e9f50c1887ea 5 weeks ago 237.7 MB
確認のためイメージからコンテナを作成してみます。
$ docker run --rm -it phusion/baseimage:0.9.17 /sbin/my_init -- bash -l
...省略...
*** Running bash -l...
root@48f64351272e:/# Aug 20 11:46:39 48f64351272e syslog-ng[17]: syslog-ng starting up; version='3.5.3'
root@48f64351272e:/#
Dockerfileを作成
imageディレクトリを作成し、そこへDockerfileを作成します。
$ mkdir /home/docker/image
Dockerfile
[phusion/baseimage-docker] (https://github.com/phusion/baseimage-docker)の説明を元にDockerfileを作成しました。
このDockerfileでは
- rootユーザーのパスワードを変更します。
- ubuntuユーザーを追加します。
- この2ユーザーで同じ公開鍵を使用します。(検証目的なので端折りました)
# Use phusion/baseimage as base image. To make your builds reproducible, make
# sure you lock down to a specific version, not to `latest`!
# See https://github.com/phusion/baseimage-docker/blob/master/Changelog.md for
# a list of version numbers.
FROM phusion/baseimage:0.9.17
# Use baseimage-docker's init system.
CMD ["/sbin/my_init"]
# ...put your own build instructions here...
RUN rm -f /etc/service/sshd/down
# Regenerate SSH host keys. baseimage-docker does not contain any, so you
# have to do that yourself. You may also comment out this instruction; the
# init system will auto-generate one during boot.
RUN /etc/my_init.d/00_regen_ssh_host_keys.sh
## Install an SSH of your choice.
ADD id_rsa.pub /tmp/id_rsa.pub
RUN ssh-keygen -i -f /tmp/id_rsa.pub >> /root/.ssh/authorized_keys && chmod 600 /root/.ssh/authorized_keys && rm -f /tmp/id_rsa.pub
# change root password
RUN echo 'root:root' | chpasswd
# Add user
RUN adduser --disabled-password --gecos "" ubuntu && echo 'ubuntu:ubuntu' | chpasswd && gpasswd -a ubuntu sudo
RUN mkdir -m 700 /home/ubuntu/.ssh && chown ubuntu:ubuntu /home/ubuntu/.ssh
# from windows putty
ADD id_rsa.pub /tmp/id_rsa.pub
RUN ssh-keygen -i -f /tmp/id_rsa.pub >> /home/ubuntu/.ssh/authorized_keys && chown ubuntu:ubuntu /home/ubuntu/.ssh/authorized_keys && chmod 600 /home/ubuntu/.ssh/authorized_keys && rm -f /tmp/id_rsa.pub
EXPOSE 22
# Upgrading the operating system inside the container
RUN apt-get update && apt-get upgrade -y -o Dpkg::Options::="--force-confold"
# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
公開鍵の配置
Windows側で作成した公開鍵を共有フォルダを経由してDockerfileと同じディレクトリに置きます。
$ ls
Dockerfile id_rsa.pub
ビルド
$ docker build -t rubytomato/baseimage:0.1 .
Sending build context to Docker daemon 7.68 kB
Step 0 : FROM phusion/baseimage:0.9.17
---> e9f50c1887ea
...省略...
Successfully built 80d8eaf8203c
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rubytomato/baseimage 0.1 80d8eaf8203c 2 minutes ago 295.8 MB
phusion/baseimage 0.9.17 e9f50c1887ea 5 weeks ago 237.7 MB
コンテナの実行
作成したイメージからコンテナを実行します。
docker仮想マシン側の10022ポートをコンテナ側の22ポートへマッピングしました。
$ docker run --name tomato -p 10022:22 -d rubytomato/baseimage:0.1
32dbce490824b2babbce1850d2b93c60cd0669afe11892ffa847cb5dc418640a
コンテナの情報を取得
IDを確認します。
$ docker ps --format="{{.ID}}\\t{{.Status}}\\t{{.Ports}}"
32dbce490824 Up 19 seconds 0.0.0.0:10022->22/tcp
portを確認します。
$ docker port 32dbce490824 22
0.0.0.0:10022
IPを確認します。
$ docker inspect -f "{{ .NetworkSettings.IPAddress }}" 32dbce490824
172.17.0.80
sshでログインします。
$ ssh root@172.17.0.80
The authenticity of host '172.17.0.80 (172.17.0.80)' can't be established.
ECDSA key fingerprint is 96:ea:6f:9f:31:d2:0e:c2:3e:c4:b1:ac:e0:22:99:4f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.80' (ECDSA) to the list of known hosts.
root@172.17.0.80's password:
root@32dbce490824:~#
ログインできない場合はexecコマンドでコンテナに入りログなどを確認します。
$ docker exec -it tomato bash
root@32dbce490824:/#
puttyでログインする
設定
Session
- Host Name (or IP address) : 192.168.99.100
- Port : 10022
IPアドレスにdocker仮想マシンのIP、PortにコンテナのPort22にマッピングした10022を指定します。
Connection/SSH/Auth
- Private key file for authentication : D:\key\id_rsa.ppk
Private Key file for authenticationに作成した秘密鍵を指定します。
接続
上記の設定が終了したら、Openボタンをクリックして接続します。
初回は"PuTTY Security Alert"が表示されると思いますが"はい"を押して続けます。
ログイン画面が表示されるのでrootユーザーでログインします。
login as: root
Authenticating with public key "rsa-key-20150820"
Passphrase for key "rsa-key-20150820":
Last login: Thu Aug 20 11:59:23 2015 from 172.17.42.1
root@32dbce490824:~#
メモ
puttygenで認証鍵を作成する際にパスフレーズを設定しなかった場合、"ssh server refused our key"というメッセージが出てputtyでの認証に失敗しました。
原因はまだわからないのですが、回避手段として"Save Public Key"ボタンではなく、画面上部のデータで公開鍵ファイルを作成しました。


