在 Windows 10 中无法访问 Docker 容器中的 Hugo 服务器

Max*_*sey 4 docker hugo windows-10

几天前,我开始了一个小项目:在我的 Windows 10 机器上对我的 Hugo 构建进行 Dockerizing。作为 Linux 容器运行的 Hugo 容器本身是简单的部分并且似乎可以工作(至少通过查看控制台输出

$ docker run --rm -it -p 1313:1313/tcp hugo:latest
Building sites … 
  Replace Autoprefixer browsers option to Browserslist config.
  Use browserslist key in package.json or .browserslistrc file.

  Using browsers option cause some error. Browserslist config 
  can be used for Babel, Autoprefixer, postcss-normalize and other tools.

  If you really need to use option, rename it to overrideBrowserslist.   

  Learn more at:
  https://github.com/browserslist/browserslist#readme
  https://twitter.com/browserslist


WARN 2019/11/23 14:05:35 found no layout file for "HTML" for "section": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.

                   | DE | EN  
+------------------+----+----+
  Pages            |  9 |  7
  Paginator pages  |  0 |  0
  Non-page files   |  0 |  0
  Static files     | 25 | 25
  Processed images |  0 |  0
  Aliases          |  1 |  0
  Sitemaps         |  2 |  1
  Cleaned          |  0 |  0

Total in 680 ms
Watching for changes in /app/{assets,content,i18n,layouts,static}
Watching for config changes in /app/config.yaml
Environment: "development"
Serving pages from memory
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)
Press Ctrl+C to stop
Run Code Online (Sandbox Code Playgroud)

我运行的 Dockerfile 看起来像这样

FROM node:13-alpine
ENV VERSION 0.59.1

EXPOSE 1313
RUN apk add --no-cache git openssl py-pygments libc6-compat g++ curl 
RUN curl -L https://github.com/gohugoio/hugo/releases/download/v${VERSION}/hugo_extended_${VERSION}_Linux-64bit.tar.gz | tar -xz  \
  && cp hugo /usr/bin/hugo \
  && apk del curl \
  && hugo version
WORKDIR /app

COPY assets assets
COPY content content
COPY i18n i18n
COPY layouts layouts
COPY static static
COPY package.json package.json
COPY postcss.config.js postcss.config.js
COPY config.yaml config.yaml

RUN yarn

CMD [ "hugo", "server", "--buildDrafts","--watch" ]
Run Code Online (Sandbox Code Playgroud)

现在对我来说最困难的部分是连接到主机系统(Windows 10 专业版)浏览器上正在运行的 Hugo 服务器。我基本上尝试了一切:localhost:1313& http://172.17.0.2:1313/(我通过运行获得的容器 IP docker inspect <container ID>),启用和禁用防火墙,但似乎没有任何效果。

为了验证它是否应该工作,我hugo server --buildDrafts --watch直接在我的主机系统上运行并且可以很好地访问服务器。我还花了几个小时来阅读这个问题,但似乎没有一个解决方案对我的情况有效。

我该如何解决这个问题?

lar*_*sks 15

这是你的问题:

Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)
Run Code Online (Sandbox Code Playgroud)

Hugo 绑定到容器的回送地址 ( 127.0.0.1) 。它默认执行此操作,因为严格来说它是一种开发工具,而不是用于在生产中实际提供页面。为了避免任何安全问题,它默认绑定到环回接口,以便您只能从本地机器连接到它。hugo serve

不幸的是,在容器的上下文中,localhost意思是“这个容器”。因此,将 Hugo 绑定到127.0.0.1容器内后,您将永远无法连接到它。

解决方案是使用该--bind选项提供不同的绑定地址。您可能想要修改您的Dockerfile,使其看起来像:

CMD [ "hugo", "server", "--buildDrafts", "--watch", "--bind", "0.0.0.0" ]
Run Code Online (Sandbox Code Playgroud)

这将导致 Hugo 绑定到容器内的“所有接口”,这将导致它按预期工作。

  • 你比我快了大约60秒。:) 以下是这些标志的文档链接:https://gohugo.io/commands/hugo_server/ (4认同)