Create Docker Images for Hugo Sites

Donate

Become a backer or sponsor to support our work.

Sponsors

The article describes how to create a Docker image for Hugo site, then you’re able push it to container registry, and deploy on self-hosted server, Docker Swarm, k8s cluster and so on.

Principle

This example uses Nginx as web server to serve Hugo generated static files.

Create Dockerfile

Create the Dockerfile and tweak it as need, such as the fallback 404 page.

Dockerfile
 1###############
 2# Build Stage #
 3###############
 4FROM hugomods/hugo:exts as builder
 5# Base URL
 6ARG HUGO_BASEURL=
 7ENV HUGO_BASEURL=${HUGO_BASEURL}
 8# Build site
 9COPY . /src
10RUN hugo --minify --gc --enableGitInfo
11# Set the fallback 404 page if defaultContentLanguageInSubdir is enabled, please replace the `en` with your default language code.
12# RUN cp ./public/en/404.html ./public/404.html
13
14###############
15# Final Stage #
16###############
17FROM hugomods/hugo:nginx
18COPY --from=builder /src/public /site

Test Dockerfile

1docker build \
2  -t user/my-site:test \
3  --build-arg HUGO_BASEURL=http://localhost:8080 \
4  .
1docker run -p 8080:80 user/my-site:test

Now you’re able to test the build image via https://localhost:8080.

Build Docker Image

1docker build -t user/my-site .
razonyang
Sunday, December 1, 2024 Friday, April 14, 2023