我开始使用 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! :-) 大量 Docker 教程和文档是围绕 Ubuntu 容器编写的,但我也喜欢 Centos。
好的,这里有几件事要谈:
你正在与 a known issue 比赛使用基于
systemd
的 Docker 容器,它们似乎需要额外的权限才能运行,甚至需要大量额外的配置才能让它们正常工作。 Red Hat 团队是 experimenting with some fixes (mentioned in comments)但不确定它在哪里。如果您想尝试让它工作,these are the best instructions I've found ,但在过去的几周里我已经玩过几次了,但还没有成功。
人们可能会说这里的“真正问题”是 Docker 容器不应被视为“迷你虚拟机”。 Docker 是 designed to run one "root" process per container ,并且容器系统使得将多个容器组合在一起变得容易 - 它们占用的磁盘空间小,内存使用量小并且易于联网。
这是一个 blog post from Docker which gives some background on this .在 Dockerizing applications 上还有“Docker 基础知识”文档和 Working with containers .
因此,可以说,继续您尝试在此处创建的设置的最佳方法(尽管一开始听起来可能更复杂)是将您的“堆栈”分解为您需要的服务,然后使用工具像
docker-compose
( introduction , documentation ) 来根据需要创建单一用途的 Docker 容器。在你上面的例子中,你有两个服务,一个网络服务器和一个数据库服务器。因此,两个 Docker 容器应该可以正常工作,通过数据库网络连接连接在一起。以下是一些示例:
如果您为每个 Docker 容器运行一项服务,则无需使用
systemd
来管理它们,因为 Docker 守护进程管理每个容器,就像它是一个 Unix 进程一样。当进程终止时,Docker 容器也会终止,这很重要,因为 Docker 服务器会监控容器并可以自动重启它们,或者通知您。
关于centos - docker 灯 Centos7 : '/bin/sh -c systemctl start httpd.service' returned a non-zero code: 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32162840/