3
4

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.

phusion/baseimageを元にsshdが有効なdockerイメージを作成する

Posted at

概要

[phusion/baseimage-docker] (https://github.com/phusion/baseimage-docker)で公開されてるUbuntuベースのDockerイメージを元に、sshdを有効にしたdockerイメージを作成してみます。

環境

下記の環境で動作を確認しました。

  • Windows7 (64bit)
  • Docker 1.8.1
  • phusion/baseimage 0.9.17

参考

下記のサイトを参考にさせて頂きました。

Dockerイメージの作成

認証鍵の作成

Windows側でPUTTYGEN.EXEを使用して認証鍵を作成します。
パスフレーズは設定しました。

ファイル名は下記のようにしました。

  • 公開鍵: id_rsa.pub
  • 秘密鍵: id_rsa.ppk

ベースのimageを取得

docker仮想マシンにログインして最新バージョンのイメージを取得します。

pull
$ docker pull phusion/baseimage:0.9.17
images
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
phusion/baseimage   0.9.17              e9f50c1887ea        5 weeks ago         237.7 MB

確認のためイメージからコンテナを作成してみます。

run
$ 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ユーザーで同じ公開鍵を使用します。(検証目的なので端折りました)
Dockerfile
# 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
$ ls
Dockerfile  id_rsa.pub

ビルド

build
$ 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
iamges
$ 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ポートへマッピングしました。

run
$ docker run --name tomato -p 10022:22 -d rubytomato/baseimage:0.1
32dbce490824b2babbce1850d2b93c60cd0669afe11892ffa847cb5dc418640a

コンテナの情報を取得

IDを確認します。

ps
$ docker ps --format="{{.ID}}\\t{{.Status}}\\t{{.Ports}}"
32dbce490824    Up 19 seconds   0.0.0.0:10022->22/tcp

portを確認します。

port
$ docker port 32dbce490824 22
0.0.0.0:10022

IPを確認します。

inspect
$ docker inspect -f "{{ .NetworkSettings.IPAddress }}" 32dbce490824
172.17.0.80

sshでログインします。

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コマンドでコンテナに入りログなどを確認します。

exec
$ docker exec -it tomato bash
root@32dbce490824:/#

puttyでログインする

設定

Session

p1.png

  • Host Name (or IP address) : 192.168.99.100
  • Port : 10022

IPアドレスにdocker仮想マシンのIP、PortにコンテナのPort22にマッピングした10022を指定します。

Connection/SSH/Auth

p2.png

  • Private key file for authentication : D:\key\id_rsa.ppk

Private Key file for authenticationに作成した秘密鍵を指定します。

接続

上記の設定が終了したら、Openボタンをクリックして接続します。

初回は"PuTTY Security Alert"が表示されると思いますが"はい"を押して続けます。
ログイン画面が表示されるのでrootユーザーでログインします。

login
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"ボタンではなく、画面上部のデータで公開鍵ファイルを作成しました。

p0.png

3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?