php 在 Docker 上重启 apache

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36869443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:58:17  来源:igfitidea点击:

Restart apache on Docker

phpapacheubuntudockerdockerfile

提问by Tyler Hilbert

I am trying to update my .htaccess file on a Docker container. After updating the file I need to restart Apache. Whenever I try to restart Apache: with the command service apache2 restartI get the following error:

我正在尝试更新 Docker 容器上的 .htaccess 文件。更新文件后,我需要重新启动 Apache。每当我尝试重新启动 Apache: 使用该命令时,service apache2 restart我都会收到以下错误:

(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down Unable to open logs Action 'start' failed. The Apache error log may have more information. ...fail!

(98)地址已在使用:make_sock:无法绑定到地址0.0.0.0:80没有可用的侦听套接字,正在关闭无法打开日志操作“启动”失败。Apache 错误日志可能包含更多信息。...失败!

When I got to the error log it doesn't have any new errors. This is what my Dockerfile looks like:

当我看到错误日志时,它没有任何新错误。这是我的 Dockerfile 的样子:

    FROM ubuntu:12.04

# Install dependencies
RUN apt-get update -y
RUN apt-get install -y git curl apache2 php5 libapache2-mod-php5 php5-mcrypt php5-mysql php5-curl vim

# Install app
RUN rm -rf /var/www/ *
ADD src /var/www

# Configure apache
RUN a2enmod rewrite
RUN chown -R www-data:www-data /var/www
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2

EXPOSE 80

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

采纳答案by joelnb

It's because you are (correctly) not starting apache as a service when you docker runthe container. The line:

这是因为当您docker run使用容器时,您(正确地)没有将 apache 作为服务启动。线路:

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

Starts apache in the foreground.

在前台启动 apache。

I'm guessing you are then using docker execto execute a shell in the container to edit the file and restart apache? If so this would explain why the second time you start apache it complains about the existing process.

我猜你然后使用docker exec在容器中执行 shell 来编辑文件并重新启动 apache?如果是这样,这将解释为什么第二次启动 apache 时它会抱怨现有进程。

I think if you are using containers in this way then you are really missing out on the benefit of containers which comes when you treat them as immutable and keep the data outside the container (either on your host or in volumes) so that you can easily replace the container.

我认为,如果您以这种方式使用容器,那么当您将它们视为不可变并将数据保留在容器之外(在您的主机上或在卷中)时,您真的会错过容器带来的好处,以便您可以轻松地更换容器。

In your case if you need to modify the .htaccess file I think it would be more normal to mount that file into the container by using a command like:

在您的情况下,如果您需要修改 .htaccess 文件,我认为使用以下命令将该文件挂载到容器中会更正常:

docker run -d --name apache -v $(pwd)/.htaccess:/path/to/.htaccess -p 80:80 image:tag

Then if you have to change the file and need to restart apache you can use:

然后,如果您必须更改文件并需要重新启动 apache,您可以使用:

docker restart apache

Although it may be worth investigating the suggestion from Charlotte Dunois that you might not even need to restart apache.

尽管可能值得研究 Charlotte Dunois 的建议,即您甚至可能不需要重新启动 apache。

回答by Rafal Kozlowski

Actually you don't need to restart Apache in order to apply changes defined in .htaccess - those are applied during runtime. If you're modifying apache config file (like virtual host definition or something in httpd.conf) you can also reload config without restarting apache using

实际上,您无需重新启动 Apache 即可应用 .htaccess 中定义的更改 - 这些更改是在运行时应用的。如果您正在修改 apache 配置文件(如虚拟主机定义或 httpd.conf 中的内容),您还可以重新加载配置而无需使用重新启动 apache

sudo /etc/init.d/apache2 reload