首页 > 解决方案 > 如何在 Docker 的 sh 环境中设置好 apache2ctl 的 PATH

问题描述

这是我的 Dockerfile :

FROM debian

ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/web/log/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
ENV APACHE_RUN_DIR /var/run/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2

#RUN export DEBIAN_FRONTEND=noninteractive && apt-get update && apt-get -y -q upgrade && apt-get -y -q install apache2
RUN  apt-get update && apt-get -y -q upgrade && apt-get -y -q install apache2

EXPOSE 80 443

CMD  ["/usr/sbin/apache2ctl","-D","FOREGROUND","&"]

当我启动容器时,系统正在调用 apache2 代替 apache2ctl 并且我收到以下错误消息:

:~/strech_apache$ docker container run -ti -p 80:80 system/strech_apache
Usage: /usr/sbin/apache2 [-D name] [-d directory] [-f file]
                         [-C "directive"] [-c "directive"]
                         [-k start|restart|graceful|graceful-stop|stop]
                         [-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S] [-X]
Options:
  -D name            : define a name for use in <IfDefine name> directives
.../...
    Action '-D FOREGROUND &' failed.
    The Apache error log may have more information.

当我在交互模式下运行我的 Dockerfile 作为容器时CMD,我能够启动apache2ctl -D FOREGROUND &并且我已经让默认的 apache 页面正常工作。

更新

当我使用CMD以下行完成我的 Dockefile 时:

CMD ["apache2ctl","-D","FOREGROUND"]

我的错误信息变成了这样:

system@vmdebian:~/strech_apache$ docker container run -ti -p 80:80 system/strech_apache:strech
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
system@vmdebian:~/strech_apache$ 

如果我这样写我的 CMD 也是一样的:

CMD  ["service","apache2","restart"]

我收到了这条消息

system@vmdebian:~/strech_apache$ docker container run -ti -p 80:80 system/strech_apache:strech 
[....] Restarting Apache httpd web server: apache2AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
. ok 

看起来像apache打开/关闭,容器随着apache2的停止而停止

如何让 apache2 仍在运行?

标签: dockerapache2

解决方案


"&"您的CMD.

这里有两个潜在的问题,并且&都删除了修复:

使用CMD ["command", "arg", "..."]表单时,Docker 根本不运行 shell;它只是直接运行命令并传入它给出的参数。您使用三个参数CMD运行,、 和。 使用它给出的参数启动,但它不理解为参数,这是你得到的错误。apache2ctl-DFOREGROUND&apache2ctlapache2&

此外,您的CMD(或ENTRYPOINT)必须将其进程作为前台进程运行。如果您在 shell 中键入命令并立即返回 shell 提示符,如果您尝试将该命令作为容器中的主命令,则容器将在您启动时立即退出。你不想要这里通常的shell“在后台运行”的含义&;您希望 Apache 作为前台进程运行,并且只要 Apache 正在运行,容器就可以运行。


推荐阅读