TL; DR
- とりあえず
--privileged
をつける。つけないでいい感じで動かすのは大変そう。
- CentOSは公式でsystemd用のベースイメージを用意してくれているので、Ubuntuを使いたい人以外はそちらを使うのが良さそう
STOPSIGNAL SIGRTMIN+3
をつける
例
FROM ubuntu:18.04
RUN apt-get update \
&& apt-get install -y \
openssh-server \
&& rm -rf /var/lib/apt/lists/*
RUN mkdir -p /var/run/sshd && chmod 755 /var/run/sshd
ARG GITHUB_USER
# ADD ubuntu user, and set password and public key
ADD https://github.com/${GITHUB_USER}.keys /home/ubuntu/.ssh/authorized_keys
RUN useradd -u 1000 ubuntu \
&& usermod -s /bin/bash -G adm,sudo ubuntu \
&& echo "ubuntu ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers \
&& mkdir -p /home/ubuntu && chown -R ubuntu.ubuntu /home/ubuntu
STOPSIGNAL SIGRTMIN+3
CMD [ "/sbin/init" ]
例えばこんな感じのDockerfileでSSHサーバをsystemd経由で起動しようとすると、想定どおりに動作していません。
(ubuntu
ユーザでコンテナにSSH出来ない)
$ docker build --build-arg GITHUB_USER=${GITHUB_USER} -t kamatama41-ubuntu-systemd-test .
$ docker run -p 2222:22 -d --rm kamatama41-ubuntu-systemd-test
1faf7f57aabba3ccb935cbc3de84224f304021c87a31a7487164a2ac157e33b1
$ ssh ubuntu@localhost -p 2222
ssh_exchange_identification: Connection closed by remote host
docker runのときに --privileged
オプションを付けると想定通りの動きになります。
$ docker run -p 2222:22 -d --privileged --rm kamatama41-ubuntu-systemd-test
f4adaea692edd55bdc059536cebe3b5aac03d842af33c0b902bb79ccb1251e3a
$ ssh ubuntu@localhost -p 2222
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.9.125-linuxkit x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
This system has been minimized by removing packages and content that are
not required on a system that users do not log into.
To restore this content, you can run the 'unminimize' command.
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
ubuntu@f4adaea692ed:~$
とりあえず、テスト用途で動かす場合はこれで大丈夫そうですが、本番では使わない方が良いでしょう。ググった感じsystemdをnon-privilegedなUbuntuのコンテナで動かすのは結構大変そうです。
CentOSは公式でsystemd用のベースイメージを提供してくれているので、本番で使いたい場合はこちらを使うのが良いでしょう。
あと1点注意点として、systemdのコンテナを止めるときは SIGRTMIN+3 を使わないといけないようなので、 STOPSIGNAL
を変えてあげておきましょう。