【问题标题】:Docker Lamp Centos7: '/bin/sh -c systemctl start httpd.service' returned a non-zero code: 1Docker Lamp Centos7:'/bin/sh -c systemctl start httpd.service' 返回一个非零代码:1
【发布时间】:2015-11-16 17:28:12
【问题描述】:

我开始使用docker 来自动化环境,然后我正在尝试构建一个简单的LAMP,所以Dockerfile 如下:

FROM centos:7

ENV container=docker

RUN yum -y swap -- remove systemd-container systemd-container-libs -- install systemd systemd-libs

RUN yum -y update; yum clean all; \
(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" ]

RUN yum -y update && yum clean all
RUN yum -y install firewalld httpd mariadb-server mariadb php php-mysql php-gd php-pear php-xml php-bcmath php-mbstring php-mcrypt php-php-gettext

#Enable services
RUN systemctl enable httpd.service
RUN systemctl enable mariadb.service

#start services
RUN systemctl start httpd.service
RUN systemctl start mariadb.service

#Open firewall ports
RUN firewall-cmd --permanent --add-service=http
RUN firewall-cmd --permanent --add-service=https
RUN firewall-cmd --reload


EXPOSE 80
CMD ["/usr/sbin/init"]

所以当我构建图像时

docker build -t myimage .

然后当我运行代码时出现以下错误:

The command '/bin/sh -c systemctl start httpd.service' returned a non-zero code: 1

当我进入交互模式时(在RUN systemctl start httpd.service之后跳转命令并重新构建图像):

docker run -t -i myimage /bin/bash

在尝试手动启动服务httpd 后,出现以下错误:

Failed to get D-Bus connection: No connection to service manager.

所以,我不知道我做错了什么?

【问题讨论】:

  • 我的理解是 docker 不会自动支持 systemd,但您可以使用正确的 docker 文件使其正常工作。见hub.docker.com/r/centos/systemd。这似乎暗示需要一个特定的图像,但我不这么认为,因为我在另一个页面上看到了我应该添加书签的内容!无论如何,我自己没有尝试过,因为我要回到LXC。 Systemd 与 LXC CentOS 容器开箱即用,如果需要,您甚至可以通过 yum install openssh-server 和 SSH 进入容器。

标签: centos docker lamp devops


【解决方案1】:

这看起来更像是我的docker-systemctl-replacement 适合的完美示例。它可以轻松地解释“systemctl start httpd.service”,而无需激活 SystemD。我对一些数据库服务做了同样的事情,但不是特别是 mariadb.service - 也许你可以试一试。

【讨论】:

    【解决方案2】:

    首先,欢迎来到 Docker! :-) 大量 Docker 教程和文档是围绕 Ubuntu 容器编写的,但我也喜欢 Centos。

    好的,这里有几件事要谈:

    1. 您正在使用基于systemd 的 Docker 容器来对抗a known issue,它们似乎需要额外的权限才能运行,即使这样也需要大量额外的配置才能使它们工作。 Red Hat 团队是experimenting with some fixes (mentioned in comments),但不确定在哪里。

      如果你想尝试让它工作,these are the best instructions I've found,但我在过去几周里玩过几次,但还没有工作。

    2. 人们可能会说这里的“真正问题”是 Docker 容器不应被视为“迷你虚拟机”。 Docker 是designed to run one "root" process per container,容器系统可以很容易地将多个容器组合在一起——它们在磁盘上很小,内存使用量很小,并且易于联网。

      这是blog post from Docker which gives some background on thisDockerizing applicationsWorking with containers 上还有“Docker 基础”文档。

      因此,可以说继续您尝试在此处创建的设置的最佳方法(尽管一开始听起来可能更复杂)是将您的“堆栈”分解为您需要的服务,然后使用工具像docker-compose (introduction, documentation) 根据需要创建单一用途的 Docker 容器。

      在上述情况下,您有两个服务,一个 Web 服务器和一个数据库服务器。因此,两个 Docker 容器应该可以正常工作,通过数据库网络连接在一起。下面是一些例子:

      如果您为每个 Docker 容器运行一项服务,则无需使用 systemd 来管理它们,因为 Docker 守护进程管理每个容器就像是 Unix 进程一样。当进程死亡时,Docker 容器也会死亡,这很重要,因为 Docker 服务器会监控容器并自动重启它们,或者通知您。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 2019-02-02
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多