1. Introduction

Hugo is a static website generator written in Go and available across multiple platforms. Static site generators are a great choice for blogs and can replace server-side CMS like WordPress. As a matter of fact, this website is build with Hugo.

In this post, we will cover how to install, verify and use Hugo in a Docker build image. This will allow us to easily and reliably build the site on a Continuous Integration (CI) service like Gitlab CI, Travis CI or Circle CI.

2. Just show me the code

#**************** HUGO *********************************************
ENV HUGO_VERSION="0.55.5"
RUN set -ex \
&& curl -fsSLO --compressed "https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_Linux-64bit.tar.gz" \
&& curl -fsSLO --compressed "https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_checksums.txt" \
&& grep " hugo_extended_${HUGO_VERSION}_Linux-64bit.tar.gz\$" hugo_extended_${HUGO_VERSION}_checksums.txt | sha256sum -c - \
&& tar -xzf hugo_extended_${HUGO_VERSION}_Linux-64bit.tar.gz -C /usr/local/bin/ \
&& rm hugo_extended_${HUGO_VERSION}_Linux-64bit.tar.gz \
&& rm hugo_extended_${HUGO_VERSION}_checksums.txt \
&& hugo version
#**************** END HUGO *********************************************

3. More information

3.1 So why are these 12 lines of Dockerfile code special?

Because we not only download, but also validate Hugo. Furthermore, we manage this with only a single variable, namely HUGO_VERSION.

The following line is responsible to verify the provided checksum on Github with the actual sha256 hash of the downloaded archive e.g. hugo_extended_0.55.5_Linux-64bit.tar.gz.

grep " hugo_extended_${HUGO_VERSION}_Linux-64bit.tar.gz\$" hugo_extended_${HUGO_VERSION}_checksums.txt | sha256sum -c -
gohugo checksum github

3.2 Prerequisites

The packages curl, tar, grep are required for this command to work. If you’re building your Docker image upon common images like buildpack-deps, you will most likely have them installed.

FROM buildpack-deps:stretch

I’m using ubuntu:18.04 as base image and install curl manually:

FROM ubuntu:18.04

RUN set -ex \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
       ca-certificates \
       curl \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get clean

3.3 Hugo w/o Extended Version

Hugo provides binaries with and without an “extended” version. The “extended” version essentially offers additional Sass/SCSS capabilities. In my opinion, using the extended version of Hugo doesn’t really have disadvantages. But sometimes you don’t want/need the extended version.

In that case, simply remove all occurrences of _extended in the provided script.