【问题标题】:How to create index.html using dockerfile?如何使用 dockerfile 创建 index.html?
【发布时间】:2022-01-24 10:07:18
【问题描述】:

我正在尝试使用 nginx 创建一个简单的静态 web,并希望所有内容都由 Dockerfile 创建,问题是每当我尝试创建 index.html 文件时,它都会出现错误,我什至尝试测试它及其使用“index.htm”但格式不正确。

FROM centos:7
#update and install nginx section
RUN yum update -y
RUN yum install -y epel-release
RUN yum install -y nginx
#create path and add index.html
WORKDIR /usr/share/nginx/html

#this one working with no issue 
RUN touch index.htm
#this one will get failed
RUN touch index.html

EXPOSE 80/tcp

CMD ["nginx", "-g", "daemon off;"]

这是错误输出:

majid@DESKTOP-39CBKO0:~/nginx_simple_web$ docker build -t simple-web:v1 .
[+] Building 3.8s (11/11) FINISHED
 => [internal] load build definition from Dockerfile                                                          0.0s
 => => transferring dockerfile: 381B                                                                          0.0s
 => [internal] load .dockerignore                                                                             0.0s
 => => transferring context: 2B                                                                               0.0s
 => [internal] load metadata for docker.io/library/centos:7                                                   3.4s
 => [auth] library/centos:pull token for registry-1.docker.io                                                 0.0s
 => [1/7] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630  0.0s
 => CACHED [2/7] RUN yum update -y                                                                            0.0s
 => CACHED [3/7] RUN yum install -y epel-release                                                              0.0s
 => CACHED [4/7] RUN yum install -y nginx                                                                     0.0s
 => CACHED [5/7] WORKDIR /usr/share/nginx/html                                                                0.0s
 => CACHED [6/7] RUN touch index.htm                                                                          0.0s
 => ERROR [7/7] RUN touch index.html                                                                          0.4s
------
 > [7/7] RUN touch index.html:
#11 0.357 touch: cannot touch 'index.html': No such file or directory
------
executor failed running [/bin/sh -c touch index.html]: exit code: 1
majid@DESKTOP-39CBKO0:~/nginx_simple_web$

【问题讨论】:

  • nginx包在安装时会创建/usr/share/nginx/html/index.html,但是我不明白你为什么不能touch它。

标签: html docker dockerfile centos7


【解决方案1】:

该文件已存在并归根用户所有:

RUN ls -al index.html
 ---> Running in 27f9d0ae6240
lrwxrwxrwx 1 root root 25 Dec 23 12:08 index.html 
-> ../../doc/HTML/index.html

删除它并重新创建它:

FROM centos:7
#update and install nginx section
RUN yum update -y
RUN yum install -y epel-release
RUN yum install -y nginx
RUN yum install -y vim
#create path and add index.html
WORKDIR /usr/share/nginx/html

RUN rm index.html
RUN touch index.html

EXPOSE 80/tcp

CMD ["nginx", "-g", "daemon off;"]

请注意,一般情况下,您应该将多个 RUN 命令组合在一起。

【讨论】:

    【解决方案2】:

    您可以使用echo 创建一个新文件:

    RUN echo " " > index.htm
    

    【讨论】:

    • 不,它会失败:=> 错误 [6/6] RUN echo " " > index.html
    • 你得到了什么错误信息?
    • 这个:=> ERROR [6/6] RUN echo " " > index.html 0.4s ------ > [6/6] RUN echo " " > index.html: #10 0.375 /bin/sh: index.html: No such file or directory ------ 执行器运行失败 [/bin/sh -c echo " " > index.html]: exit code: 1
    【解决方案3】:

    你应该创建一个文件,你可以使用

    复制 index.html index.html

    在构建时向 Dockerfile 中的命令复制文件到映像中

    或使用

    回声“”> index.html 创建文件的命令

    【讨论】:

    • 我不想复制文件,一切都应该在 Dockerfile 中完成,并且 echo ""> index.html 也会失败。
    【解决方案4】:

    在您的 Dockerfile 中,您可以像在命令行中一样使用 RUN {command}

    RUN mkdir -p /home/foo/
    RUN echo "Hello World" > /home/foo/index.html
    

    【讨论】:

    • (这些文件应该归根用户所有,这样应用程序就不会意外覆盖它们。在写入文件之前,您无需触摸(1)文件。)
    • @DavidMaze 谢谢你的建议!我更新了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多