【问题标题】:Apache not running automatically on docker compose upApache 没有在 docker compose up 上自动运行
【发布时间】:2016-07-09 08:43:34
【问题描述】:

Dockerfile

FROM phusion/baseimage:0.9.16
MAINTAINER Raheel <raheelwp@gmail.com>

# Apache
RUN apt-get update
RUN apt-get -y install apache2

# PHP
RUN apt-get -y install python-software-properties
RUN add-apt-repository ppa:ondrej/php
RUN apt-get update
RUN apt-get -y install php7.0
RUN apt-get -y install libapache2-mod-php7.0 php7.0-curl php7.0-json

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

docker-composer.yaml

version: '2'
services:
  frontend:
    build: ./frontend
    ports:
     - "80:80"
    volumes:
     - ./frontend:/var/www/html/frontend

我正在尝试在 docker 中运行我的 laravel 应用程序。我面临两个问题

1 - 当我运行 docker-compose up 时,容器内的 apache 服务器不会自动启动。每次我必须登录容器并执行service apache2 start。根据我的搜索,我发现我在 Dockerfile 中编写的 CMD 命令是我们启动 apache 的方式,但在我的情况下不起作用

2 - 当我登录到容器并转到我的应用程序文件夹 /var/www/html/frontend 时,它的用户是 1000:1000 的东西。我希望它们位于 www-data:www-data 下。但我不希望将其添加到我的 Dockerfile 中,因为我只想保留我的 dockerfile 用于安装 apache 和 php。我怎样才能通过使用docker-compose.yaml 或任何其他方式来实现这一点。

更新:Docker 容器日志

*** Running /etc/my_init.d/00_regen_ssh_host_keys.sh...
*** Running /etc/rc.local...
*** Booting runit daemon...
*** Runit started as PID 9
Jul 10 08:27:33 6d5c09e83a98 syslog-ng[15]: syslog-ng starting up; version='3.5.3'

谢谢

【问题讨论】:

  • 如果该命令没有启动apache,那它​​是做什么的呢?没有日志和命令输出很难调试。
  • 如何查看日志?我在 ubuntu 16
  • docker-compose logs frontend
  • 这是说没有服务前端,但是我试图查看容器的日志。请查看我粘贴的有问题的更新部分。

标签: php apache docker docker-compose dockerfile


【解决方案1】:

从您的日志输出判断,当您执行docker-compose up 时正在运行的映像没有使用您指定的CMD。这可能是因为它是在您第一次运行 docker-compose up 时构建的,并且随后没有重新构建。要解决此问题,请尝试使用 docker-compose up --build 运行。

当我构建并运行您的图像(使用您提供的 docker-compose.yml)时,我确实启动了 apache - 我的输出如下:

Successfully built d65dabcc2595
Creating apachenotrunningautomaticallyondockercomposeup38280007_frontend_1
Attaching to apachenotrunningautomaticallyondockercomposeup38280007_frontend_1
frontend_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.25.0.2. Set the 'ServerName' directive globally to suppress this message

我认为考虑到您正在使用的图像,以这种方式启动 apache 并不是最佳实践 - 推荐的方式是使用 service file,因为 phusion/baseimage 使用自定义初始化系统来绕过 some potential issues with Docker

您可以按照他们推荐的方式为 apache 创建一个服务文件。如果您(在您的前端文件夹中)创建一个名为 apache.sh 的脚本(确保它是 chmod +x),其中包含以下内容:

#! /bin/sh

exec /usr/sbin/apache2ctl -D FOREGROUND

并将您的 Dockerfile 更改为如下所示:

FROM phusion/baseimage:0.9.16
MAINTAINER Raheel <raheelwp@gmail.com>

# Apache
RUN apt-get update
RUN apt-get -y install apache2

# PHP
RUN apt-get -y install python-software-properties
RUN add-apt-repository ppa:ondrej/php
RUN apt-get update
RUN apt-get -y --force-yes install php7.0
RUN apt-get -y --force-yes install libapache2-mod-php7.0 php7.0-curl php7.0-json

RUN mkdir /etc/service/apache
ADD apache.sh /etc/service/apache/run
RUN chmod +x /etc/service/apache/run

然后它将使用提供的init系统。

在回答您的第二个问题时,我认为您有两个主要选择:

  1. 如果您必须将它们保存在 VOLUME 中并且主机上的两个用户都可以访问(大概是 uid 和 gid 为 1000 的用户),那么您可以确保 apache 运行您的应用程序的用户有与主机上的用户相同的 uid 和 gid(创建另一个用户并告诉 apache 在您的 apache 配置中使用该用户)。这会起作用,但对于主机上的用户不同的系统来说,移植性要差得多。

  2. 将此添加到您的 Dockerfile 并从 docker-compose.yml 中删除卷选项:

    RUN mkdir -p /var/www/html/frontend/
    COPY . /var/www/html/frontend/
    RUN chown -R www-data:www-data /var/www/html/frontend
    

如果是我,我会选择选项 1 用于开发环境(因为可移植性可能不是问题),但选项 2 用于任何类型的生产部署(因为 docker 的一大好处是容器的不变性)。

【讨论】:

  • 尝试了您的解决方案,它给了我frontend_1 | runsv apache: fatal: unable to start ./run: access denied。我已经给了 chmod +x 来运行.sh
  • 很奇怪。我保留了我的文件名 start.sh 而不是 apache.sh 它现在无法正常工作我确切地命名为 apache.sh 并再次运行它可以工作。为什么会这样……
  • 发生这种情况的原因是因为您创建的新文件没有设置可执行权限(如果您运行ls -lh &lt;directory-with-scripts&gt;,您会看到权限有所不同)。我已经编辑添加了一行,以确保文件在容器中被标记为可执行。我很高兴这个解释很有帮助:)
猜你喜欢
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
  • 2021-06-13
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
相关资源
最近更新 更多