Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hugo server in Docker container not reachable in Windows 10

A few days ago I started a little side project: Dockerizing my Hugo build on my Windows 10 machine. The Hugo container itself, which runs as a Linux container, was the easy part and seems to work (at least by looking at the console output

$ 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

My Dockerfile the I run looks like this

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

The hard part for me now is to connect to the running Hugo server on my host's systems (Windows 10 Pro) browser. I basically tried everything: localhost:1313 & http://172.17.0.2:1313/ (the container IP I get by running docker inspect <container ID>), with firewall enabled and disabled, but nothing seems to work.

To verify that it should work I ran hugo server --buildDrafts --watch directly on my host system and can access the server just fine. I also invested several hours in reading up on the issue, but none of the solutions seem to work in my case.

How can I solve this issue?

like image 223
Maximilian Lindsey Avatar asked Dec 05 '22 09:12

Maximilian Lindsey


1 Answers

Here's your problem:

Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)

Hugo is binding to the loopback address (127.0.0.1) inside the container. It does this by default because hugo serve is meant strictly as a development tool, not for actually serving pages in production. In order to avoid any security issues, it defaults to binding to the loopback interface so that you can only connect to it from the local machine.

Unfortunately, in the context of a container, localhost means "this container". So with Hugo bound to 127.0.0.1 inside a container you'll never be able to connect to it.

The solution is to provide a different bind address using the --bind option. You probably want to modify your Dockerfile so that it looks like:

CMD [ "hugo", "server", "--buildDrafts", "--watch", "--bind", "0.0.0.0" ]

This will cause hugo to bind to "all interfaces" inside the container, which should result in it working as you expect.

like image 70
larsks Avatar answered Jan 01 '23 01:01

larsks