LoginSignup
2
1

More than 3 years have passed since last update.

Docker上で自前OpenSSH(sshd)をビルドして動そうとしてコケた話

Last updated at Posted at 2019-10-11

何がしたかったか?

  • 研究関連でOpenSSH(のsshd)を自前でビルドして色々試したい事がある
  • Mac上でうまくビルドできないし,どこでもビルドできるようにDockerで動かしたい
    • つまりDocker上でビルド,サービス化,接続テストができるように
    • MacのVSCodeで編集,即座にDocker上でビルド,すぐ接続テスト.したい.
  • DockerといえばAlpine Linux!!

環境とか

  • Docker Engine: 19.03.2
  • Docker Compose: 1.24.1

  • ディレクトリ構造

tree.txt
.
├── docker-compose.yml
├── src
│   └── OpenSSHのソースコード
└── vm
    ├── Dockerfile
    └── login.sh

最初に試したこと

1. 以下のdocker-composeとDockerfileを書いてみた.

docker-compose.yml
version: '3'
services:
  sshd-build: 
    build: ./vm # vm/Dockerfileがあるのでそいつをビルド
    image: sshd-build:latest
    container_name: sshd-container
    restart: always
    volumes:
      - ./:/root/openssh-portable # ローカルで開発してDockerでビルド,検証したいので共有
    tty: true # 立てたコンテナの中で動きたいときに書くやつ
Dockerfile
FROM alpine as sshd-linux

WORKDIR /root # rootとして動くのでここを作っとく

RUN apk update && \ # ビルドに必要なパッケージの追加
    apk add --no-cache openrc openssh \ # この2つはAlpineのデーモン登録のため必要
    gcc g++ make openssl-dev zlib-dev bash # これらはビルドのため

ENTRYPOINT ["/bin/bash"]

2. コンテナへのログイン

login.sh
#!/bin/bash
docker exec -it sshd-container /bin/bash

3. ビルドとインストール

# cd openssh-portable/src
# ./configure && make && make install

問題とその解決

1. インストール後, デーモン起動を試みるも...

tty
# rc-service sshd start
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/blkio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpu/tasks: Read-
... 同様の表示
 * You are attempting to run an openrc service on a
 * system which openrc did not boot.
 * You may be inside a chroot or you may have used
 * another initialization system to boot this system.
 * In this situation, you will get unpredictable results!
 * If you really want to do this, issue the following command:
 * touch /run/openrc/softlevel
 * ERROR: sshd failed to start

起動できていない...ここは海外ニキを参考にして修正をした.海外ニキ,全体的にエスパーで解決しに来るのでつよい.
今回環境の場合はdocker-composeを使っているのでそこに追加.

2. その後も追加でエラー...

  • 後半のエラーコード(You are attempting...)は消えず.

エラーコードに従ってtouch /run/openrc/softlevelを叩いたら通った.

結論

docker-compose.yml
version: '3'
services:
  sshd-build: 
    build: ./vm
    image: sshd-build:latest
    container_name: sshd-container
    restart: always
    volumes:
      - ./:/root/openssh-portable
      - /sys/fs/cgroup
    tty: true
Dockerfile
FROM alpine as sshd-linux

WORKDIR /root

RUN apk update && \
    apk add --no-cache openssh openrc gcc g++ make openssl-dev zlib-dev bash

ENTRYPOINT ["/bin/bash"]

コンテナ起動後

# cd openssh-portable/src
# ./configure && make && make install
# touch /run/openrc/softlevel
# rc-service sshd start
ssh-keygen: generating new host keys: RSA DSA ECDSA ED25519 
 * Starting sshd ... [ok]
# rc-status
Runlevel: sysinit
Dynamic Runlevel: hotplugged
Dynamic Runlevel: needed/wanted
Dynamic Runlevel: manual
 sshd 

あとはソースコード書き換えて, make && make install && rc-service sshd restartを繰り返す.

ちなみに

イメージサイズは197MBであった.もう少し小さくしたいな...
その話はまた今度.

あとビルドが遅い.数秒で終わってほしい(願望)

2
1
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
2
1