Docker 容器中的 Hugo 服务器无法在 Windows 10 中访问
Hugo server in Docker container not reachable in Windows 10
几天前,我开始了一个小项目:在我的 Windows 10 机器上对我的 Hugo 构建进行 Dockerizing。 Hugo 容器本身,运行 作为一个 Linux 容器,是简单的部分并且似乎可以工作(至少通过查看控制台输出
$ 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
我的 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" ]
现在对我来说最困难的部分是连接到主机系统(Windows 10 Pro)浏览器上的 运行ning Hugo 服务器。
我基本上尝试了所有方法:localhost:1313
& http://172.17.0.2:1313/
(我通过 运行ning docker inspect <container ID>
获得的容器 IP),启用和禁用防火墙,但似乎没有任何效果。
为了验证它是否应该工作,我直接在我的主机系统上 运行 hugo server --buildDrafts --watch
并且可以很好地访问服务器。我还花了几个小时阅读这个问题,但 none 的解决方案似乎适用于我的情况。
我该如何解决这个问题?
这是你的问题:
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)
Hugo 绑定到环回地址 (127.0.0.1
)在容器内。默认情况下它会这样做,因为 hugo serve
严格来说是一种开发工具,而不是用于在生产中实际提供页面服务。为了避免任何安全问题,它默认绑定到环回接口,以便您只能从本地机器连接到它。
不幸的是,在容器的上下文中,localhost
意味着 "this container"。因此,当 Hugo 绑定到容器内的 127.0.0.1
时,您将永远无法连接到它。
解决方案是使用 --bind
选项提供不同的绑定地址。您可能想修改 Dockerfile
使其看起来像:
CMD [ "hugo", "server", "--buildDrafts", "--watch", "--bind", "0.0.0.0" ]
这将导致 hugo 绑定到容器内的 "all interfaces",这应该会导致它按预期工作。
几天前,我开始了一个小项目:在我的 Windows 10 机器上对我的 Hugo 构建进行 Dockerizing。 Hugo 容器本身,运行 作为一个 Linux 容器,是简单的部分并且似乎可以工作(至少通过查看控制台输出
$ 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
我的 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" ]
现在对我来说最困难的部分是连接到主机系统(Windows 10 Pro)浏览器上的 运行ning Hugo 服务器。
我基本上尝试了所有方法:localhost:1313
& http://172.17.0.2:1313/
(我通过 运行ning docker inspect <container ID>
获得的容器 IP),启用和禁用防火墙,但似乎没有任何效果。
为了验证它是否应该工作,我直接在我的主机系统上 运行 hugo server --buildDrafts --watch
并且可以很好地访问服务器。我还花了几个小时阅读这个问题,但 none 的解决方案似乎适用于我的情况。
我该如何解决这个问题?
这是你的问题:
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)
Hugo 绑定到环回地址 (127.0.0.1
)在容器内。默认情况下它会这样做,因为 hugo serve
严格来说是一种开发工具,而不是用于在生产中实际提供页面服务。为了避免任何安全问题,它默认绑定到环回接口,以便您只能从本地机器连接到它。
不幸的是,在容器的上下文中,localhost
意味着 "this container"。因此,当 Hugo 绑定到容器内的 127.0.0.1
时,您将永远无法连接到它。
解决方案是使用 --bind
选项提供不同的绑定地址。您可能想修改 Dockerfile
使其看起来像:
CMD [ "hugo", "server", "--buildDrafts", "--watch", "--bind", "0.0.0.0" ]
这将导致 hugo 绑定到容器内的 "all interfaces",这应该会导致它按预期工作。