13
12

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.

Dockerの中のsystemdにデーモンを起動させる

Posted at

Debian wheezyまではDockerの中で複数のデーモンを起動しようと思ったらsupervisordとかmonitを入れないといけなくてツラい感じでしたが、Debian jessieからはsystemdが入ったので試してみました。

systemctlを起動する

docker run --privileged --entrypoint /sbin/init

privilegedじゃないとD-Busの何かでエラーになります。

このイメージに docker exec -it コンテナID /bin/bash で入ってみるとsystemdが起動しているのがわかります。

root@a9f1bbc93216:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.1  0.2  28160  4228 ?        Ss   02:05   0:00 /sbin/init
root        24  0.0  0.1  32956  3096 ?        Ss   02:05   0:00 /lib/systemd/systemd-journald
root        30  0.0  0.1  38992  3160 ?        Ss   02:05   0:00 /lib/systemd/systemd-udevd
root        38  0.0  0.0  12832  1968 tty3     Ss+  02:05   0:00 /sbin/agetty --noclear tty3 linux
root        39  0.0  0.0  12832  1828 tty2     Ss+  02:05   0:00 /sbin/agetty --noclear tty2 linux
root        40  0.0  0.0  12832  1960 ?        Ss   02:05   0:00 /sbin/agetty --noclear tty1 linux
root        41  0.0  0.0  12832  1908 tty4     Ss+  02:05   0:00 /sbin/agetty --noclear tty4 linux
root        42  0.0  0.0  12832  1980 tty6     Ss+  02:05   0:00 /sbin/agetty --noclear tty6 linux
root        43  0.0  0.0  12832  1904 tty5     Ss+  02:05   0:00 /sbin/agetty --noclear tty5 linux
root        97  0.0  0.0  12652  1876 ?        Ss   02:06   0:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt102
root       188  0.0  0.0  17488  2040 ?        R+   02:07   0:00 ps aux

serviceファイルを置く

systemdにデーモンを登録する方法は、 /etc/systemd/systems/ に.serviceファイルを置いて systemctl enable すれば良いです。

root@a9f1bbc93216:/# echo "[Unit]
> Description=logger
> After=syslog.target
>
> [Service]
> ExecStart=/bin/bash -c 'while true; do logger `date`; sleep 1; done'
>
> [Install]
> WantedBy=multi-user.target
> " > /etc/systemd/system/logger.service
root@a9f1bbc93216:/# systemctl list-unit-files | grep logger
logger.service                           disabled
root@a9f1bbc93216:/# systemctl enable logger.service
Created symlink from /etc/systemd/system/multi-user.target.wants/logger.service to /etc/systemd/system/logger.service.
root@a9f1bbc93216:/# systemctl start logger.service
root@a9f1bbc93216:/# journalctl -f
-- Logs begin at Wed 2015-04-29 02:05:11 UTC. --
Apr 29 02:06:15 a9f1bbc93216 systemd[1]: Started Update UTMP about System Runlevel Changes.
Apr 29 02:06:15 a9f1bbc93216 systemd[1]: Startup finished in 1min 18.708s (kernel) + 1min 4.794s (userspace) = 2min 23.502s.
Apr 29 02:07:09 a9f1bbc93216 systemd[1]: Starting logger...
Apr 29 02:07:09 a9f1bbc93216 systemd[1]: Started logger.
Apr 29 02:07:09 a9f1bbc93216 logger[103]: Wed Apr 29 02:05:49 UTC 2015
Apr 29 02:07:10 a9f1bbc93216 logger[105]: Wed Apr 29 02:05:49 UTC 2015
Apr 29 02:07:11 a9f1bbc93216 logger[107]: Wed Apr 29 02:05:49 UTC 2015
Apr 29 02:07:12 a9f1bbc93216 logger[109]: Wed Apr 29 02:05:49 UTC 2015
Apr 29 02:07:13 a9f1bbc93216 logger[111]: Wed Apr 29 02:05:49 UTC 2015
Apr 29 02:07:14 a9f1bbc93216 logger[113]: Wed Apr 29 02:05:49 UTC 2015
Apr 29 02:07:15 a9f1bbc93216 logger[116]: Wed Apr 29 02:05:49 UTC 2015
Apr 29 02:07:16 a9f1bbc93216 logger[118]: Wed Apr 29 02:05:49 UTC 2015
Apr 29 02:07:17 a9f1bbc93216 logger[120]: Wed Apr 29 02:05:49 UTC 2015
Apr 29 02:07:18 a9f1bbc93216 logger[122]: Wed Apr 29 02:05:49 UTC 2015
Apr 29 02:07:19 a9f1bbc93216 logger[124]: Wed Apr 29 02:05:49 UTC 2015

Dockerfile化

logger.serviceというファイルを作り、

[Unit]
Description=logger
After=syslog.target

[Service]
ExecStart=/bin/bash -c 'while true; do logger `date`; sleep 1; done'

[Install]
WantedBy=multi-user.target

同じディレクトリにDockerfileを置いて、

FROM debian:jessie

COPY ./logger.service /etc/systemd/system/

# equivalent to `systemctl enable logger.service`
RUN ln -s /etc/systemd/system/logger.service /etc/systemd/system/multi-user.target.wants/logger.service

docker buildすれば良いです。

docker build -t test-logger .
13
12
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
13
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?