Apache 未在 docker 上自动运行 compose up

Rah*_*eel 5 php apache docker dockerfile docker-compose

文件

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"]
Run Code Online (Sandbox Code Playgroud)

docker-composer.yaml

version: '2'
services:
  frontend:
    build: ./frontend
    ports:
     - "80:80"
    volumes:
     - ./frontend:/var/www/html/frontend
Run Code Online (Sandbox Code Playgroud)

我正在尝试在 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'
Run Code Online (Sandbox Code Playgroud)

谢谢

joe*_*lnb 5

从您的日志输出来看,当您运行时正在运行的图像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
Run Code Online (Sandbox Code Playgroud)

我认为,鉴于您正在使用的映像,以这种方式启动 apache 并不是最佳实践 - 推荐的方法是使用服务文件,因为phusion/baseimage使用自定义 init 系统来解决Docker 的一些潜在问题

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

#! /bin/sh

exec /usr/sbin/apache2ctl -D FOREGROUND
Run Code Online (Sandbox Code Playgroud)

并将您的 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
Run Code Online (Sandbox Code Playgroud)

然后它将使用提供的初始化系统。

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

  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
    
    Run Code Online (Sandbox Code Playgroud)

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