【问题标题】:amazonlinux:2 systemctl Failed to get D-Bus connectionamazonlinux:2 systemctl 未能获得 D-Bus 连接
【发布时间】:2020-09-07 05:38:57
【问题描述】:

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

这是我的 Dockefile :

FROM amazonlinux:2

MAINTAINER "xxxxx" <xxxxx@xxxx.xx>

RUN yum -y update
RUN yum -y install httpd

WORKDIR /var/www/html

CMD [ "/usr/sbin/httpd","-D","FOREGROUND" ]

systemctl 不起作用:

bash-4.2# systemctl list-units
Failed to get D-Bus connection: Operation not permitted
bash-4.2# systemctl status httpd.service
Failed to get D-Bus connection: Operation not permitted

我无法重启

bash-4.2# systemctl restart httpd.service
Failed to get D-Bus connection: Operation not permitted

/etc/os-release

bash-4.2# cat /etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"

在真正的 ec2 上没问题:

[ec2-user@ip-xxxxxxxxx]$ cat /etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"

[ec2-user@ip-xxxxxxxxx]$ systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/httpd.service.d
           └─php-fpm.conf
   Active: active (running) since mer. 2020-05-20 16:36:19 UTC; 1h 50min ago
     Docs: man:httpd.service(8)
 Main PID: 3047 (httpd)
   Status: "Total requests: 7; Idle/Busy workers 100/0;Requests/sec: 0.00106; Bytes served/sec:  20 B/sec"
   CGroup: /system.slice/httpd.service
           ├─3047 /usr/sbin/httpd -DFOREGROUND
           ├─3075 /usr/sbin/httpd -DFOREGROUND
           ├─3076 /usr/sbin/httpd -DFOREGROUND
           ├─3077 /usr/sbin/httpd -DFOREGROUND
           ├─3078 /usr/sbin/httpd -DFOREGROUND
           ├─3079 /usr/sbin/httpd -DFOREGROUND
           └─3495 /usr/sbin/httpd -DFOREGROUND

mai 20 16:36:19 xxxxxxxxx.internal systemd[1]: Starting The Apache HTTP Server...
mai 20 16:36:19 xxxxxxxxx.internal systemd[1]: Started The Apache HTTP Server.

这是一个著名的图像,有没有人找到解决方案?

【问题讨论】:

  • 是的,作为一般规则 systemctl 在 Docker 中不起作用(同样service 和直接运行 /etc/init.d 脚本不会达到您的预期)。容器 HTTP 守护进程;您无需尝试运行 systemd 命令、在其中获取 shell 或在完整的 Linux VM 上执行的其他类似任务。

标签: docker amazon-ec2 systemctl


【解决方案1】:

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

如果您执行“docker stop (container)”,那么 httpd 将获得一个 SIGTERM。如果您执行“docker restart (container)”,那么它将循环通过 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

【讨论】:

    猜你喜欢
    • 2017-11-03
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 2018-10-27
    相关资源
    最近更新 更多