amazonlinux:2 systemctl 无法获得 D-Bus 连接

bin*_*ngo 5 amazon-ec2 docker systemctl

我想使用此图像https://hub.docker.com/_/amazonlinux来编写我的 EC2 机器的脚本

\n\n

这是我的文档:

\n\n
FROM amazonlinux:2\n\nMAINTAINER "xxxxx" <xxxxx@xxxx.xx>\n\nRUN yum -y update\nRUN yum -y install httpd\n\nWORKDIR /var/www/html\n\nCMD [ "/usr/sbin/httpd","-D","FOREGROUND" ]\n
Run Code Online (Sandbox Code Playgroud)\n\n

systemctl 不起作用:

\n\n
bash-4.2# systemctl list-units\nFailed to get D-Bus connection: Operation not permitted\n
Run Code Online (Sandbox Code Playgroud)\n\n
bash-4.2# systemctl status httpd.service\nFailed to get D-Bus connection: Operation not permitted\n
Run Code Online (Sandbox Code Playgroud)\n\n

我无法重新启动

\n\n
bash-4.2# systemctl restart httpd.service\nFailed to get D-Bus connection: Operation not permitted\n
Run Code Online (Sandbox Code Playgroud)\n\n

/etc/os-release

\n\n
bash-4.2# cat /etc/os-release\nNAME="Amazon Linux"\nVERSION="2"\nID="amzn"\nID_LIKE="centos rhel fedora"\nVERSION_ID="2"\nPRETTY_NAME="Amazon Linux 2"\nANSI_COLOR="0;33"\nCPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"\nHOME_URL="https://amazonlinux.com/"\n
Run Code Online (Sandbox Code Playgroud)\n\n

在真正的 ec2 上没有问题:

\n\n
[ec2-user@ip-xxxxxxxxx]$ cat /etc/os-release\nNAME="Amazon Linux"\nVERSION="2"\nID="amzn"\nID_LIKE="centos rhel fedora"\nVERSION_ID="2"\nPRETTY_NAME="Amazon Linux 2"\nANSI_COLOR="0;33"\nCPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"\nHOME_URL="https://amazonlinux.com/"\n\n[ec2-user@ip-xxxxxxxxx]$ systemctl status httpd.service\n\xe2\x97\x8f httpd.service - The Apache HTTP Server\n   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)\n  Drop-In: /usr/lib/systemd/system/httpd.service.d\n           \xe2\x94\x94\xe2\x94\x80php-fpm.conf\n   Active: active (running) since mer. 2020-05-20 16:36:19 UTC; 1h 50min ago\n     Docs: man:httpd.service(8)\n Main PID: 3047 (httpd)\n   Status: "Total requests: 7; Idle/Busy workers 100/0;Requests/sec: 0.00106; Bytes served/sec:  20 B/sec"\n   CGroup: /system.slice/httpd.service\n           \xe2\x94\x9c\xe2\x94\x803047 /usr/sbin/httpd -DFOREGROUND\n           \xe2\x94\x9c\xe2\x94\x803075 /usr/sbin/httpd -DFOREGROUND\n           \xe2\x94\x9c\xe2\x94\x803076 /usr/sbin/httpd -DFOREGROUND\n           \xe2\x94\x9c\xe2\x94\x803077 /usr/sbin/httpd -DFOREGROUND\n           \xe2\x94\x9c\xe2\x94\x803078 /usr/sbin/httpd -DFOREGROUND\n           \xe2\x94\x9c\xe2\x94\x803079 /usr/sbin/httpd -DFOREGROUND\n           \xe2\x94\x94\xe2\x94\x803495 /usr/sbin/httpd -DFOREGROUND\n\nmai 20 16:36:19 xxxxxxxxx.internal systemd[1]: Starting The Apache HTTP Server...\nmai 20 16:36:19 xxxxxxxxx.internal systemd[1]: Started The Apache HTTP Server.\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是一张著名的图片,有人找到解决方案吗?

\n

Gui*_*eim 0

“httpd”作为容器的 PID-1 运行。

如果你执行“docker stop(容器)”,那么 httpd 将收到一个 SIGTERM。如果您执行“docker restart(容器)”,那么它将循环通过 docker-stop / docker-start 并重新创建 httpd 守护进程。

https://docs.docker.com/engine/reference/commandline/restart/

这里不需要在容器中使用 systemd。如果你真的想这样做,那么有多种选择。可能是在容器中运行完整的 systemd 或使用另一个服务管理器。

https://github.com/gdraheim/docker-systemctl-images/blob/master/centos-httpd.dockerfile