几天前,我开始了一个小项目:在Windows 10机器上对我的Hugo构建进行Docker化。作为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
我运行的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)浏览器上正在运行的Hugo服务器。
我基本上尝试了所有操作:localhost:1313
和http://172.17.0.2:1313/
(通过运行docker inspect <container ID>
获得的容器IP),启用和禁用了防火墙,但似乎无济于事。
为了验证它是否可以正常工作,我直接在主机系统上运行了hugo server --buildDrafts --watch
,并且可以正常访问服务器。我还花了几个小时来阅读有关该问题的信息,但在我看来,所有解决方案都不起作用。
我该如何解决这个问题?
答案 0 :(得分:8)
这是您的问题:
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)
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" ]
这将导致雨果绑定到容器内的“所有接口”,这将导致其按预期工作。