This section introduces how to build a site image, which uses Nginx as the web server to serve Hugo generated static files.
Become a backer or sponsor to support our work.
We offer a built-in Nginx image, hugomods/hugo:nginx
, which contains a default configuration. You can totally create your own Nginx image if it doesn’t meet your needs, but in this section, we take hugomods/hugo:nginx
as the example.
Firstly, create the Dockerfile
under site root.
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
10# Replace below build command at will.
11RUN hugo --minify --enableGitInfo
12# Set the fallback 404 page if defaultContentLanguageInSubdir is enabled,
13# please replace the `en` with your default language code.
14# RUN cp ./public/en/404.html ./public/404.html
15
16#####################################################################
17# Final Stage #
18#####################################################################
19FROM hugomods/hugo:nginx
20# Copy the generated files to keep the image as small as possible.
21COPY --from=builder /src/public /site
/src/public
)./site
for hugomods/hugo:nginx
).1docker build \
2 -t user/my-site:test \
3 --build-arg HUGO_BASEURL=http://localhost:8080 \
4 .
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
-t
specifies the image name and tag.--build-arg HUGO_BASEURL=http://localhost:8080
overrides the baseURL
.Now the built site can be previewed on http://localhost:8080/.
Once you’re satisfied with the test result, it’s time to build the production image, and then push it to container registry, and then deploy it to server, K8s cluster, etc.
1docker build -t user/my-site .