Deploy With Nginx Image

This section introduces how to build a site image, which uses Nginx as the web server to serve Hugo generated static files.

Donate

Become a backer or sponsor to support our work.

Sponsors

Nginx Image

Size

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.

Create Dockerfile

Firstly, create the Dockerfile under site root.

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
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
  1. The build stage builds site and outputs generated files to public folder (default to /src/public).
  2. The final stage copies the generated files from build stage to Nginx site folder (default to /site for hugomods/hugo:nginx).

Test Built Image

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
  1. -t specifies the image name and tag.
  2. --build-arg HUGO_BASEURL=http://localhost:8080 overrides the baseURL.

Now the built site can be previewed on http://localhost:8080/.

Build And Deploy Image

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 .