April 9, 2021

Dockerize my hugo based site

This page is created using static site generator Hugo. Hugo generates HTML pages ready to deploy to any webserver. To make the deployment process a little bit easier, I want to deploy the generated site as a Docker container on my virtual host.

First, it needs a proper Dockerfile to build an image. I use a multi-stage build. In the first stage, Hugo generates a static site from sources. The second stage uses the generated web pages to create a deployable Nginx-based unit.

# Dockerfile for multi-stage build to generate static site with Hugo (https://gohugo.io/)

# Stage 1: Generate site from sources
FROM alpine as hugo-build

RUN apk add --update \
    wget
    
ARG HUGO_VERSION="0.82.0"
RUN wget --quiet "https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_Linux-64bit.tar.gz" && \
    tar xzf hugo_${HUGO_VERSION}_Linux-64bit.tar.gz && \
    rm -r hugo_${HUGO_VERSION}_Linux-64bit.tar.gz && \
    mv hugo /usr/bin

COPY ./ /site
WORKDIR /site
RUN hugo

# Stage 2: Copy static files to Nginx
FROM nginx:alpine
COPY --from=hugo-build /site/public /usr/share/nginx/html

WORKDIR /usr/share/nginx/html

Now we are ready to build a Docker image with site content. I decided to tag images with the build date. The newest version gets also the latest tag. After all, we just run a container in the newest image version.

docker build . \
       -t <my-image-name>:$(date +"%Y%m%d") \
       -t <my-image-name>:latest

docker run -d -p 8888:80 --name static-site <my-image-name>

That’s it.

© Ilyas Yambay, 2014-2025 | Kiss theme for Hugo