我正在尝试创建一个将自动启动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
问题在这里:CMD service apache2 start
执行此命令进程时,apache2
将从外壳中分离出来。但是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
对我来说,最后一行与CMD是错误的:
# it helped me
CMD ["apachectl", "-D", "FOREGROUND"]
我的项目略有不同,我安装了许多其他内容,但是上面匹配的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"]
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
应该做到。
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"]
使用Apache Server运行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