LoginSignup
26
30

More than 5 years have passed since last update.

SSHで接続可能なDocker用のCentOSイメージをPackerで生成する

Last updated at Posted at 2014-05-06

Dockerを使用する際、コンテナの状況を確認するのに、SSH経由で接続できると便利ですが、openssh-serverのインストールや設定、sshd_configの変更等を行う必要があります。

下記は、SSHログイン可能なDockerイメージをPackerで生成する設定です。

docker.json
{
  "builders": [
    {
      "name": "centos6-docker",
      "type": "docker",
      "image": "centos",
      "export_path": "centos6-docker.tar"
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "inline": [
        "yum -y update",
        "yum install -y openssh-server",
        "echo 'root:PASSWORD' | chpasswd",
        "ssh-keygen -q -N '' -t dsa -f /etc/ssh/ssh_host_dsa_key",
        "ssh-keygen -q -N '' -t rsa -f /etc/ssh/ssh_host_rsa_key",
        "sed -i 's/UsePAM.*/UsePAM no/g' /etc/ssh/sshd_config",
        "sed -i 's/#PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config"
      ]
    }
  ],
  "post-processors": [
    {
      "type": "docker-import",
      "repository": "centos6",
      "tag": "latest",
      "only": [
        "centos6-docker"
      ]
    }
  ]
}

上記では、rootログインをPASSWORDで許可しています。鍵等を使用する場合には、別途File Provisioner等を使用して、設定を行う必要があります。

  • 上記設定を使用してPackerでビルドします。
$ packer build docker.json
  • ビルドしたイメージcentos6をsshdを指定して実行してみます。
$ sudo docker run -p 2222:22 centos6 /usr/sbin/sshd -D
  • 下記コマンドで正しくSSHに接続できたかが確認できます。
$ ssh -p 2222 127.0.0.1
26
30
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
26
30