我试图从头开始构建一个nginx图像(而不是使用官方的nginx图像)

FROM ubuntu
RUN apt-get update
RUN apt-get install -y nginx    
RUN rm -v /etc/nginx/nginx.conf
ADD nginx.conf /etc/nginx/
RUN echo "daemon off;" >> /etc/nginx/nginx.conf

EXPOSE 80

COPY ./files/ /var/www/html/

CMD service nginx start

这是我在当前目录下的nginx.conf文件.

server {

    root /var/www/html

    location / {
        index.html
    }

}

和我的虚拟index.html文件在./files文件夹下

我运行这个命令

docker build -t hello-world .

docker run -p 80:80 hello-world

但是我说错了

 * Starting nginx nginx
   ...fail!

可能是什么问题?
最佳答案
不要使用“service xyz start”

要在容器内运行服务器,请不要使用service命令.这是一个脚本,它将在后台运行请求的服务器,然后退出.当脚本退出时,容器将停止(因为该脚本是主要进程).

而是直接运行服务脚本为您启动的命令.除非它退出或崩溃,否则容器应继续运行.

CMD ["/usr/sbin/nginx"]

nginx.conf缺少事件部分

这是必需的.就像是:

events {
    worker_connections 1024;
}

server指令不是顶级元素

您在nginx.conf的顶层有服务器{},但它必须在协议定义(如http {})内才有效.

http {
    server {
        ...

nginx指令以分号结尾

在root语句和index.html行的末尾缺少这些内容.

缺少“索引”指令

要定义索引文件,请使用index,而不仅仅是文件名.

index index.html;

没有HTML元素“p1”

我假设您打算使用< p>这里.

最后结果

Dockerfile:

FROM ubuntu
RUN apt-get update
RUN apt-get install -y nginx
RUN rm -v /etc/nginx/nginx.conf
ADD nginx.conf /etc/nginx/
RUN echo "daemon off;" >> /etc/nginx/nginx.conf

EXPOSE 80

COPY ./files/ /var/www/html/

CMD ["/usr/sbin/nginx"]

nginx.conf:

http {
    server {

        root /var/www/html;

        location / {
            index index.html;
        }
    }
}
events {
    worker_connections 1024;
}

dawei

【声明】:丽水站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。