2017年1月20日金曜日

sshログインできるテスト環境をDockerで作る (Dokerfile編)

sshログインできるテスト環境を Docker で作る」 では、手作業で Docker コンテナ を構築しました。

ここでは、Dockerfile で構築を自動化してみます。


1. Dockerfile の作成


テスト環境を Docker で作る」 で構築したDockerコンテナと同じになるように、Dockerfile を作成します。
内容は以下のとおり。
[root@centos0702 docker]# cat Dockerfile
# centos6 のイメージを取得
FROM centos:6

# Dockerfile 作成者
MAINTAINER blue21

# 作業ディレクトリ
WORKDIR /tmp

# EC2(CentOS6) にパッケージをあわせる
RUN yum -y install cloud-init iptables-ipv6 openssh-server openssh-clients sudo

# 日本語 locale インストール
RUN yum -y reinstall glibc-common

# wheelグループはパスなしsudoを使用可能にする
RUN sed -i -e 's/^# \(%wheel.ALL=(ALL).NOPASSWD: ALL\)$/\1/' /etc/sudoers

# centos ユーザ追加
RUN useradd -d /home/centos -s /bin/bash -G wheel centos
RUN echo "centos:p@ssw0rd" | chpasswd

# sshd 初期化
RUN service sshd start
RUN service sshd stop

# SELinux dummy
COPY selinux_config /etc/selinux/config

# SSH PUBLIC KEY
RUN mkdir /home/centos/.ssh
RUN chown centos.centos /home/centos/.ssh
RUN chmod 700 /home/centos/.ssh
COPY authorized_keys /home/centos/.ssh/authorized_keys
RUN chown centos.centos /home/centos/.ssh/authorized_keys
RUN chmod 700 /home/centos/.ssh/authorized_keys

# ssd 起動 (forground)
CMD /usr/sbin/sshd -D

Dockerfile と同じディレクトリに、selinux_config と authorized_keys を配置します。
selinux_config は、コンテナの /etc/selinux/config にコピーします。
authorized_keys は、/home/centos/.ssh/authorized_keys にコピーします。
[root@centos0702 docker]# ls
Dockerfile  README  authorized_keys  selinux_config


2. Docker イメージの作成


Docker イメージを作成します。
Dockerfile があるディレクトリに移動して以下を実行します。
[root@centos0702 docker]# docker build -t centos6_sshd:1 .
Sending build context to Docker daemon  7.68 kB
Step 1 : FROM centos:6
 ---> 8315978ceaaa
Step 2 : MAINTAINER blue21
 ---> Running in aef758119653
 ---> a56f9c7254ed
Removing intermediate container aef758119653
Step 3 : WORKDIR /tmp
 ---> Running in c52b22f55571
 ---> ad534d64b408
~省略~
Step 18 : CMD /usr/sbin/sshd -D
 ---> Running in bc29f94ffb42
 ---> 40119178acab
Removing intermediate container bc29f94ffb42
Successfully built 40119178acab

作成したDockerイメージがローカルに保存されていることを確認します。
[root@centos0702 docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6_sshd        1                   7eb9929a5863        29 minutes ago      657.4 MB
docker.io/centos    6                   8315978ceaaa        10 weeks ago        194.6 MB


3. Dockerコンテナの動作確認


Dockerコンテナを起動します。
[root@centos0702 docker]# docker run -d centos6_sshd:1
5dd7d27e69ad0c4a57ddff1ca385ee406a6d89d37f1cdf0b303610934144597c
[root@centos0702 docker]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
5dd7d27e69ad        centos6_sshd:1      "/bin/sh -c '/usr/sbi"   8 seconds ago       Up 7 seconds                            big_borg

Docker コンテナのIPアドレスを確認します。
[root@centos0702 docker]# docker inspect 5dd7d27e69ad | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",

ssh でログインとsudoできるか確かめます。
[root@centos0702 docker]# ssh -l centos -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null 172.17.0.2
Warning: Permanently added '172.17.0.2' (RSA) to the list of known hosts.
Last login: Wed Jan 18 03:42:04 2017 from 172.17.0.1
[centos@5dd7d27e69ad ~]$ sudo id
uid=0(root) gid=0(root) 所属グループ=0(root)
[centos@5dd7d27e69ad ~]$ exit
logout
Connection to 172.17.0.2 closed.