Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Windows 10中无法访问Docker容器中的Hugo服务器_Docker_Windows 10_Hugo - Fatal编程技术网

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

在Windows 10中无法访问Docker容器中的Hugo服务器,docker,windows-10,hugo,Docker,Windows 10,Hugo,几天前,我开始了一个小项目:在我的Windows10机器上对接我的Hugo构建。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

几天前,我开始了一个小项目:在我的Windows10机器上对接我的Hugo构建。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" ]
现在对我来说,最困难的部分是连接到主机系统(Windows10Pro)浏览器上正在运行的Hugo服务器。 我基本上什么都试过了:
localhost:1313
&
http://172.17.0.2:1313/
(我通过运行
docker inspect获得的容器IP
),启用和禁用防火墙,但似乎什么都不起作用

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

如何解决此问题?

这是您的问题:

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" ]

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

您比我快了大约60秒。:)下面是这些标志的文档链接:非常感谢!我知道这很容易,但我就是想不出来。现在,我可以在本地主机下的本地浏览器中访问我的容器:1313。我将其设置为
hugo server--bind 0.0.0
,它将其打印并绑定到
0.0.0
,但仍然无法从Windows主机连接。