container

Hugo Container

I am using Hugo to build this website and it made sense for me to automate it.

The first step on that journey is to create a container to use for the static site files generation.

The basis for the container will be Debian.

Container file

I came up with the following Containerfile

FROM debian:bookworm-slim

# Set hugo version, can be overridden during build
ARG HUGO_VERSION=0.148.1

# Prepare necessary packages
RUN DEBIAN_FRONTEND=noninteractive \
    apt-get update -y && apt-get upgrade -y \
    && apt-get install -y --no-install-recommends \
    ca-certificates \
    curl \
    tar \
    gzip \
    && rm -rf /var/lib/apt/lists/*

# Add hugo user
RUN groupadd -g 1000 hugo \
    && useradd -u 1000 -g hugo -r hugo \
    && mkdir -p /src \
    && chown hugo:hugo /src && chmod 775 /src

# Download and install Hugo
RUN curl -L https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_Linux-64bit.tar.gz \
    | tar -xz -C /usr/local/bin hugo

COPY --chmod=555 container-entrypoint.sh /usr/local/bin/

WORKDIR /src

USER hugo

EXPOSE 1313

ENTRYPOINT [ "container-entrypoint.sh" ]

CMD [ "hugo" ]

The container-entrypoint.sh file checks for source files and will abort if none are found.

Build image

podman build -t teknikuglen/hugo-builder:latest -f Containerfile .

Run container

The container also serves draft files when the -D switch is used.

podman run --rm -v $(pwd)/src:/src:Z -p 127.0.0.1:1313:1313 --userns=keep-id teknikuglen/hugo-builder hugo server -D

The --userns=keep-id parameter keeps the owner id inside the container the same as on the host.
The :Z parameter is to ensure selinux permissions are correct inside the container.

Generate output files

The following is the command to run to generate the final files you can use on your web server.

podman run --rm -v $(pwd)/src:/src:Z --userns=keep-id teknikuglen/hugo-builder hugo -d /src/site_build

The --userns=keep-id parameter keeps the owner id inside the container the same as on the host.
The :Z parameter is to ensure selinux permissions are correct inside the container.

Conclusion

So that’s one container ready. If you are interested in the source files they can be found in my git repo at podman-hugo