LoginSignup
7
2

More than 1 year has passed since last update.

Docker for Mac 4.18 で再び Failed to get D-Bus connection エラーが出たときの対処法

Last updated at Posted at 2023-04-08

結論

  • Dockerfile で /sys/fs/cgroup の Volume をマウント
  • docker-compose.yml で cgroup: host を設定

環境

Mac

  • OS: Mac OS X Monterey 12.6.3
  • チップ: Apple M1 Max
  • メモリ: 64GB

Docker

  • Docker Desktop for Mac 4.18.0
  • Base Docker Image: amazonlinux:2

systemctl が利用できない

Docker for Mac で開発していて 4.3 になった際に cgroup の扱いが V2 に変わり Apache が起動しない、systemctl が使えないと悩まされました。
実際に Docker にログインすると下記のエラーが出て systemctl を実行すると D-Bus の Connection が作成できないエラーが出ていました。

bash-4.2# systemctl status httpd
Failed to get D-Bus connection: No such file or directory

その時は下記記事で語られているように "deprecatedCgroupV1": true を設定することで systemctl が利用できるようになりました。
Docker for mac (ver 4.3以降) で「Failed to get D-Bus connection」エラーが出たときの対処法

しかしながら Docker for Mac 4.18 で "deprecatedCgroupV1": true を設定していても Failed to get D-Bus connection が出たのでその対処法を探しました。

Docker for Mac 4.18 での対処法

Dockerfile の変更

Docker Container の /sys/fs/cgroup を読み書き可能な Volume を Mount する

FROM amazonlinux:2

RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
  systemd-tmpfiles-setup.service ] || rm -f $i; done); \
  rm -f /lib/systemd/system/multi-user.target.wants/*;\
  rm -f /etc/systemd/system/*.wants/*;\
  rm -f /lib/systemd/system/local-fs.target.wants/*; \
  rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
  rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
  rm -f /lib/systemd/system/basic.target.wants/*;\
  rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]

こちらの記事を参考にしています。
M1 Mac centos:7 Failed to get D-Bus connection: No such file or directory

docker-compose.yml の変更

cgroup: host の設定を追加する

version: "3.3"
services:
  web:
    build: .
    ports:
      - 8080:80
      - 4430:443
    privileged: true
    platform: linux/x86_64
    cgroup: host # この行を追加

注意点

起動してすぐは D-Bus に接続することができないため Docker Container 起動後30秒(環境によると思います)ほど様子をみてください。

bash-4.2# while true; do systemctl status httpd; sleep 1; done 
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
Failed to get D-Bus connection: No such file or directory
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd.service(8)
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd.service(8)
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2023-04-08 20:00:20 JST; 843ms ago
     Docs: man:httpd.service(8)
 Main PID: 308 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─308 /usr/bin/qemu-x86_64 /usr/sbin/httpd /usr/sbin/httpd -DFOREGROUND
           ├─315 /usr/bin/qemu-x86_64 /usr/sbin/httpd /usr/sbin/httpd -DFOREGROUND
           ├─317 /usr/bin/qemu-x86_64 /usr/sbin/httpd /usr/sbin/httpd -DFOREGROUND
           ├─319 /usr/bin/qemu-x86_64 /usr/sbin/httpd /usr/sbin/httpd -DFOREGROUND
           ├─321 /usr/bin/qemu-x86_64 /usr/sbin/httpd /usr/sbin/httpd -DFOREGROUND
           └─323 /usr/bin/qemu-x86_64 /usr/sbin/httpd /usr/sbin/httpd -DFOREGROUND
7
2
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
7
2