【问题标题】:Nginx server not running with Gatsbyjs default Docker imageNginx 服务器未使用 Gatsbyjs 默认 Docker 映像运行
【发布时间】:2018-07-19 11:35:48
【问题描述】:

我有一个工作 gatsbyjs 项目,它不是从官方 Docker 映像运行的。

这是Dockerfile

FROM gatsbyjs/gatsby:latest
ADD public/ /pub

(编译后的网站位于public/,我已确认index.html 正确呈现网站)

我也试过官方Dockerfile,还是不行:

FROM gatsbyjs/gatsby:onbuild

我从docker-compose 运行它,如下所示:

version: '3'

services:
  website:
    build: .
    ports:
      - "80:80"

Nginx 返回“500 内部服务器错误”。

我正在关注官方教程here

【问题讨论】:

    标签: docker nginx docker-compose gatsby


    【解决方案1】:

    latest 映像更新时,onbuild 映像似乎没有重建,这导致 docker 拉取了基础映像的过时版本。 您最好忽略onbuild 图像并获取最新的图像。 Here你可以找到详情。

    测试最新的图像是否有效。

    首先我尝试从镜像中运行一个容器:

    $ docker run -d -p 80:80 gatsbyjs/gatsby:latest
    

    这行得通。 Nginx 在这里运行。

    $ curl localhost
    <html>
    <head><title>404 Not Found</title></head>
    <body bgcolor="white">
    <center><h1>404 Not Found</h1></center>
    <hr><center>nginx</center>
    

    删除容器。

    我将全局安装 gatsby

    $ npm install --global gatsby-cli
    

    现在我从 here 克隆了一个基本的 gatsbyjs 站点。

    $ git clone https://github.com/gatsbyjs/gatsby-starter-hello-world.git
    $ cd gatsby-starter-hello-world\
    

    我将建立网站(如您的文档中所述)。 $ npm 安装 $ gatsby 构建

    现在我有一个public/ 存储库。这是带有:onbuild 标签的图像所期望的。

    LICENSE  README.md  node_modules/  package-lock.json  package.json  public/  src/
    

    现在我可以在 git repo 中编写我的 Dockerfile。我将使用最新的标签(不是 onbuild),这意味着我必须编写自己的 COPYADD 步骤。它包含:

    FROM gatsbyjs/gatsby:latest
    COPY public/ /pub
    

    我将构建 docker 映像

    $ docker build -t my-site .
    Sending build context to Docker daemon  2.791MB
    Step 1/2 : FROM gatsbyjs/gatsby:latest
     ---> 21fc487ad83e
    Step 2/2 : COPY public/ /pub
     ---> 87f5ea1018ee
    Removing intermediate container fd35cace6ef0
    Successfully built 87f5ea1018ee
    Successfully tagged my-site:latest
    

    最后我可以从镜像中启动一个 docker 容器。

    $ docker run -d -p 80:80 my-site
    

    现在我可以curl我的localhost:80

    $ curl localhost
    $<!DOCTYPE html><html><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="ie=edge"/><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/><link rel="preload" href="/component---src-pages-index-js-3a9c8c6504e39c44af75.js" as="script"/><link rel="preload" href="/path---index-a0e39f21c11f6a62c5ab.js" as="script"/><link rel="preload" href="/app-fcd7d98e3197e34ce021.js" as="script"/><link rel="preload" href="/commons-eef92a68af65d2662677.js" as="script"/><style id="gatsby-inlined-css"></style></head><body><div id="___gatsby"><div data-reactroot="" data-reactid="1" data-react-checksum="-122217258"><div data-reactid="2">Hello world!</div></div></div><script id="webpack-manifest">/*<![CDATA[*/window.webpackManifest={"231608221292675":"app-fcd7d98e3197e34ce021.js","35783957827783":"component---src-pages-index-js-3a9c8c6504e39c44af75.js","142629428675168":"path---index-a0e39f21c11f6a62c5ab.js"}/*]]>*/</script><script>/*<![CDATA[*/!function(e,t,r){function n(){for(;d[0]&&"loaded"==d[0][f];)c=d.shift(),c[o]=!i.parentNode.insertBefore(c,i)}for(var..
    

    【讨论】:

    • 嗨@lvthillo,非常感谢详细的答案。不幸的是,如您所见,我已经尝试过这种方法,即使使用./public,我仍然得到 404。
    • 我已经用 docker 镜像尝试过这种方法,问题肯定出在docker-compose 设置中。对此有什么想法吗?
    • 好吧,我尝试使用您的 docker-compose 文件,它可以工作。确保 Dockerfile 是正确的。我的 dockerfile 和上面的一样,我开始 docker-compose : docker-compose up -d --build
    • 菜鸟错误 - 在 docker-compose up 之前忘记重新构建!
    • 感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 2020-04-27
    • 1970-01-01
    • 2020-09-29
    • 2017-03-22
    • 1970-01-01
    相关资源
    最近更新 更多