【问题标题】:Cannot start apache automatically with docker无法使用 docker 自动启动 apache
【发布时间】:2018-09-20 17:50:52
【问题描述】:

我为 php 开发做了一个简单的自定义 docker 设置。到目前为止,一切都按预期工作。我唯一想不通的是为什么 apache2 不能自动启动。

这是我的 dockerfile:

FROM ubuntu:latest
RUN apt-get update && apt-get install -y apache2 php libapache2-mod-php php-mcrypt php-mysql php-cli php-curl php-xml php-intl php-mbstring git vim composer curl

COPY . /var/www/example
COPY vhost.conf /etc/apache2/sites-available/example.conf

RUN a2ensite example
RUN chown -R www-data:www-data /var/www/example/logs
RUN service apache2 restart

这是我的 docker-compose.yml:

version: '2'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: myexampleapp
    ports:
        - 8080:80
    tty: true

这里是输出 docker-compose up 命令:

me@mydell:~/workspace/mydockercompose$ docker-compose up -d --build
Creating network "mydockercompose_default" with the default driver
Building app
Step 1/7 : FROM ubuntu:latest
 ---> f975c5035748
Step 2/7 : RUN apt-get update && apt-get install -y apache2 php libapache2-mod-php php-mcrypt php-mysql php-cli php-curl php-xml php-intl php-mbstring git vim composer curl
 ---> Using cache
 ---> 148c3a9d928a
Step 3/7 : COPY . /var/www/example
 ---> 1fbc1dbacf1e
Step 4/7 : COPY vhost.conf /etc/apache2/sites-available/example.conf
 ---> 9c08947b09e9
Step 5/7 : RUN a2ensite example
 ---> Running in 1ef64defe747
Enabling site example.
To activate the new configuration, you need to run:
  service apache2 reload
Removing intermediate container 1ef64defe747
 ---> ca1c8e7e80fc
Step 6/7 : RUN chown -R www-data:www-data /var/www/example/logs
 ---> Running in 57b0214be7a0
Removing intermediate container 57b0214be7a0
 ---> b3b270a36bf4
Step 7/7 : RUN service apache2 restart
 ---> Running in 09d2b1d3bd91
 * Restarting Apache httpd web server apache2
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
   ...done.
Removing intermediate container 09d2b1d3bd91
 ---> 19fa9a90f9de
Successfully built 19fa9a90f9de
Successfully tagged myexampleapp:latest
Creating mydockercompose_app_1

清楚地表明apache重新启动成功。但实际上并没有:

me@mydell:~/workspace/mydockercompose$ docker exec -i -t 20add8ad9895 service apache2 status
 * apache2 is not running

我是 docker 新手,因此欢迎所有建议(即使他们没有回答这个特定问题)来改进我目前所做的工作。

谢谢

【问题讨论】:

    标签: apache ubuntu docker docker-compose dockerfile


    【解决方案1】:

    Docker 服务必须在前台运行。在您的 Dockerfile 中,RUN service apache2 restart 将启动 apache 作为后台进程。因此容器将退出。

    要在前台运行 apache,请将以下内容添加到 Dockerfile。

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

    FROM ubuntu:latest
    RUN apt-get update && apt-get install -y apache2 php libapache2-mod-php php-mcrypt php-mysql php-cli php-curl php-xml php-intl php-mbstring git vim composer curl
    
    COPY . /var/www/example
    COPY vhost.conf /etc/apache2/sites-available/example.conf
    
    RUN a2ensite example
    RUN chown -R www-data:www-data /var/www/example/logs
    CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]
    

    【讨论】:

    • 这是对我有用的命令:CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]
    • “/usr/sbin/apachectl”有效,但“apachectl”无效。
    【解决方案2】:

    上面提到的答案可能是正确的,您可以从以下内容开始:

    CMD apachectl -D FOREGROUND
    

    对于 docker,有时使用二进制文件的绝对路径是个好主意。因此,例如,也许可以这样做:

    /usr/sbin/apache2 -D FOREGROUND
    

    我在谷歌上看了一下,看看其他人在做什么。我发现了这个 dockerfile 的例子,其中那个人提到了一个脚本 start.sh: 从这里:https://github.com/jacksoncage/apache-docker/blob/master/Dockerfile

    EXPOSE 80
    ADD start.sh /start.sh
    RUN chmod 0755 /start.sh
    CMD ["bash", "start.sh"]
    

    这里是 start.sh 脚本:https://github.com/jacksoncage/apache-docker/blob/master/start.sh

    就是这样:

    #!/bin/bash
    
    # Start apache
    /usr/sbin/apache2 -D FOREGROUND
    

    无关提示: 您需要为 Ubuntu 固定版本的 dockerfile。 见:https://nickjanetakis.com/blog/docker-tip-18-please-pin-your-docker-image-versions

    如果这有帮助,请告诉我。

    【讨论】:

    • 我能够使用启动脚本使其运行,但需要进行一些调整...
    • 很高兴听到这个消息。感谢您的反馈:)
    【解决方案3】:

    如果它对您或任何人有帮助,我正在使用完整的 docker-composer 准备工作示例: https://github.com/marekz/docker-composer-example

    FROM ubuntu:18.04
    EXPOSE 80
    ENV TZ=Europe/Warsaw
    
    RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
    RUN apt-get update 
    RUN apt-get upgrade -y
    RUN apt-get install -y libapache2-mod-php7.2 php7.2 php7.2-cli php7.2-gd php7.2-intl php7.2-mbstring php7.2-mysql php7.2-xml php7.2-xsl php7.2-bcmath php7.2-zip php-apcu npm lynx
    RUN apt-get install -y mysql-client composer screen tmux vim nano iputils-ping
    
    
    ENTRYPOINT service apache2 restart && bash
    

    【讨论】:

      【解决方案4】:

      通过分析语句,我找到了解决我的错误的方法

      文件:Dockerfile

      FROM debian
      RUN apt-get update -qq >/dev/null && apt-get install -y -qq procps telnet apache2 php7.3 -qq >/dev/null
      RUN useradd --user-group --create-home --shell /bin/false app
      RUN mkdir /data && chown -R app /data && chmod 777 /data
      COPY php.conf /etc/apache2/mods-available/php7.3.conf
      RUN a2enmod userdir && a2enmod php7.3
      

      错误:

      To activate the new configuration, you need to run:
        service apache2 reload
      

      解决方案:

      FROM debian -> FROM debian:10
      

      原因是debian系统和php版本有些冲突。最新版本的 debian 有更新版本的 PHP(当时 PHP 7.4)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-26
        • 1970-01-01
        • 2023-01-28
        • 2017-03-19
        • 2013-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多