【问题标题】:How to start apache2 automatically in a ubuntu docker container?如何在 ubuntu docker 容器中自动启动 apache2?
【发布时间】:2017-06-05 20:07:41
【问题描述】:

我正在尝试创建一个将自动启动 apache 的 Dockerfile。没有任何效果。但是如果我登录到容器并运行service apache2 start,它就可以工作。为什么我不能从我的 Dockerfile 运行该命令?

FROM ubuntu

# File Author / Maintainer
MAINTAINER rmuktader

# Update the repository sources list
RUN apt-get update

# Install and run apache
RUN apt-get install -y apache2 && apt-get clean

#ENTRYPOINT ["/usr/sbin/apache2", "-k", "start"]


#ENV APACHE_RUN_USER www-data
#ENV APACHE_RUN_GROUP www-data
#ENV APACHE_LOG_DIR /var/log/apache2

EXPOSE 80
CMD service apache2 start

【问题讨论】:

    标签: apache ubuntu docker dockerfile


    【解决方案1】:

    问题出在这里:CMD service apache2 start 当你执行这个命令时,apache2 将与 shell 分离。但 Docker 仅在主进程处于活动状态时工作。

    解决方案是在前台运行 Apache。 Dockerfile 必须如下所示:(仅更改了最后一行)。

    FROM ubuntu
    
    # File Author / Maintainer
    MAINTAINER rmuktader
    
    # Update the repository sources list
    RUN apt-get update
    
    # Install and run apache
    RUN apt-get install -y apache2 && apt-get clean
    
    #ENTRYPOINT ["/usr/sbin/apache2", "-k", "start"]
    
    
    #ENV APACHE_RUN_USER www-data
    #ENV APACHE_RUN_GROUP www-data
    #ENV APACHE_LOG_DIR /var/log/apache2
    
    EXPOSE 80
    CMD apachectl -D FOREGROUND
    

    【讨论】:

    • 它不起作用。我复制并粘贴了你的 Dockerfile,运行了 docker build -t my_img .,运行了 docker run -ti -p80:80 --name 'test2' my_img /bin/bash,但 apache 没有运行。
    • 我尝试了这种方法,它对我有用。尝试从您的命令中删除 /bin/bash
    • 如果您尝试在一个容器中运行多个服务,它将无法正常工作。 Docker方式是一种服务的容器。请解释您的问题,我会尽力帮助您用docker真正的方式解决它。
    • 我正在尝试从生产服务器上下载 redmine 1.3 的实时安装,并使用 docker 在我的本地计算机上运行它。我需要拥有相同版本的所有内容。我没想到会这么艰难。也许 docker 不是解决此类问题的正确解决方案。
    • 这对我有用,经过数小时的辛勤工作,其他一些绝对不起作用的事情。谢谢你,@BukharovSergey!!!今天给你一千个互联网点。
    【解决方案2】:

    对我来说,最后一行 CMD 是错误的:

    # it helped me
    CMD ["apachectl", "-D", "FOREGROUND"]
    

    【讨论】:

      【解决方案3】:

      我的项目在我安装了一堆其他东西的地方略有不同,但上面的 apache 启动部分匹配。一旦我构建了这个图像并使用它,我的服务器就可以正常启动了。

      FROM ubuntu:latest
      
      #install all the tools you might want to use in your container
      RUN apt-get update
      RUN apt-get install curl -y
      RUN apt-get install vim -y
      #the following ARG turns off the questions normally asked for location and timezone for Apache
      ARG DEBIAN_FRONTEND=noninteractive
      RUN apt-get install apache2 -y
      
      #change working directory to root of apache webhost
      WORKDIR var/www/html
      
      #copy your files, if you want to copy all use COPY . .
      COPY index.html index.html
      
      #now start the server
      CMD ["apachectl", "-D", "FOREGROUND"]
      

      【讨论】:

      • ARG DEBIAN_FRONTEND=noninteractive 不问任何人选项?
      【解决方案4】:
      FROM ubuntu
      
      RUN apt update
      
      ARG DEBIAN_FRONTEND=noninteractive
      RUN apt install apache2 -y && apt-get clean
      
      ENV APACHE_RUN_USER www-data
      ENV APACHE_RUN_GROUP www-data
      ENV APACHE_LOG_DIR /var/log/apache2
      
      EXPOSE 80
      CMD ["apachectl", "-D",  "FOREGROUND"]
      

      添加ARG DEBIAN_FRONTEND=noninteractive 应该可以解决问题。

      【讨论】:

        【解决方案5】:

        使用 apache 服务器运行 ubuntu 的简单 docker 文件

        RUN apt-get update
        ARG DEBIAN_FRONTEND=noninteractive  # for remove time zone
        RUN apt-get -y install apache2
        ADD . /var/www/html
        ENTRYPOINT apachectl -D FOREGROUND
        ENV name Vishal                   # anything you can give 
        

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
        【解决方案6】:
        FROM ubuntu
        RUN apt-get update
        ARG DEBIAN_FRONTEND=noninteractive
        RUN apt-get -y install apache2
        ADD index.html /var/www/html
        CMD ["apachectl", "-D", "FOREGROUND"]
        

        【讨论】:

        • 请编辑您的答案以添加一些解释。
        • 见“Explaining entirely code-based answers”。虽然这在技术上可能是正确的,但它并不能解释为什么它可以解决问题或应该是选择的答案。除了帮助解决问题,我们还应该进行教育。
        猜你喜欢
        • 2017-09-11
        • 2014-11-22
        • 2016-12-17
        • 2015-08-07
        • 1970-01-01
        • 2021-05-19
        • 2013-08-05
        • 2017-05-26
        • 1970-01-01
        相关资源
        最近更新 更多