CentOS7のコンテナでsystemctlが使えない件
個人的にCentOS7の環境を作って遊んでいたところ、systemctl
が以下のエラーで実行できなくて困ったので対処法をまとめておく。
Failed to get D-Bus connection: Operation not permitted
そもそもD-Busってなに?
D-Busはアプリケーション間でやり取りを行う際に使われIPC (プロセス間通信) 実装の一つ。エラーログ的にこのプロセス間通信でパーミッションエラーが起きてる模様。
状況再現 & 対応方法
状況再現方法
今回の状況は以下のようにコンテナを起動した場合に発生した。
$ docker container run --itd dev centos:7 $ docker container attach dev [root@da8a3babfbcc /]# systemctl Failed to get D-Bus connection: Operation not permitted
対処方法
対応方法としては↓。
--priviledge
を指定しつつ、/sbin/init
を起動する- 起動後に
exec
する
$ docker container run --privileged -d --name centos7 centos:7 /sbin/init $ docker exec -it centos7 /bin/bash [root@70b667635965 /]# systemctl ...
そもそもなぜsystemctlが使えないのか?
以下のリンクに記載がある通り、コンテナイメージ側でsystemdが無効化されている。
Systemd is now included in both the centos:7 and centos:latest base containers, but it is not active by default. In order to use systemd, you will need to include text similar to the example Dockerfile below:
今回は/usr/sbin/init
を事前に実行して、その後にコンテナにアタッチする形式を取ったが、「Dockerfile for systemd base image」にあるような感じでDockerfileを作ってやるのでもいいかも。