我想在CentOS 7容器上启动httpd服务。但是systemctl命令在容器中不起作用。在CentOS 6中,我可以简单地使用/etc/init.d/apachectl -d命令启动httpd。但是在CentOS 7中,我在/*/systemd/中找不到任何apachectl文件。

那么如何在CentOS 7容器中启动httpd服务呢?

最佳答案

最好的方法是在安装httpd的地方制作自己的centos7镜像

FROM centos:7
RUN yum -y install httpd; yum clean all; systemctl enable httpd.service
EXPOSE 80

使用docker build -t my-centos:7 .构建图像
没有SYS_ADMIN,Systemd无法运行。这就是为什么我设置以下变量。
$ docker run -it -p 80:80 -e "container=docker" --privileged=true -d --security-opt seccomp:unconfined --cap-add=SYS_ADMIN -v /sys/fs/cgroup:/sys/fs/cgroup:ro my-centos:7 bash -c "/usr/sbin/init"

验证容器正在运行:
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
967581bdf31a        my-centos:7         "bash -c /usr/sbin/in"   1 seconds ago       Up 1 seconds        0.0.0.0:80->80/tcp   gigantic_stallman

验证httpd已启动
$ docker exec -it gigantic_stallman /bin/bash -c "systemctl status httpd"
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2016-12-28 11:44:04 UTC; 2min 20s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 61 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /docker/967581bdf31a3b741a5e857720e199614d816b05a2132271f3adf910f0ed3207/system.slice/httpd.service
           ├─61 /usr/sbin/httpd -DFOREGROUND
           ├─66 /usr/sbin/httpd -DFOREGROUND
           ├─67 /usr/sbin/httpd -DFOREGROUND
           ├─68 /usr/sbin/httpd -DFOREGROUND
           ├─69 /usr/sbin/httpd -DFOREGROUND
           └─70 /usr/sbin/httpd -DFOREGROUND

Dec 28 11:44:04 967581bdf31a systemd[1]: Starting The Apache HTTP Server...
Dec 28 11:44:04 967581bdf31a httpd[61]: AH00558: httpd: Could not reliably d...e
Dec 28 11:44:04 967581bdf31a systemd[1]: Started The Apache HTTP Server.

关于apache - 在Docker CentOS 7容器中启动服务,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41318625/

10-15 13:04