Apache 未 运行 在 docker 上自动构建

Apache not running automatically on docker compose up

Docker文件

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

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

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

2 - 当我登录到容器并转到我的应用程序文件夹 /var/www/html/frontend 时,它的用户是 1000:1000 什么的。我希望它们位于 www-data:www-data 之下。但我不希望将此添加到我的 Docker 文件中,因为我想保留我的 docker 文件仅用于安装 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'

谢谢

从您的日志输出来看,当您执行 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 as phusion/baseimage uses a custom init system to get around 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

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

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

  1. 如果您必须将它们保存在一个卷中并且主机上的两个用户都可以访问(大概是 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 的一大好处是容器的不变性)。