Skip to main content
KnowHow DB
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Static site generator - Hugo

Hugo is a static site generator repo.

Setup (with pull from private repo)

  1. Create rsa keys repo with content (key, key.pub)
  2. Create dockerfile
    FROM alpine AS build
    
    RUN apk add --update \
        wget \
        git \
        openssh
    
    ARG BASE_URL="https://example.com"
    ARG HUGO_VERSION="0.152.2"
    ARG PLATFORM="Linux-ARM64"
    
    RUN wget --quiet "https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_${PLATFORM}.tar.gz" && \
        tar xzf hugo_${HUGO_VERSION}_${PLATFORM}.tar.gz && \
        rm -r hugo_${HUGO_VERSION}_${PLATFORM}.tar.gz && \
        mv hugo /usr/bin
    
    COPY key .
    
    # hack to avoid caching of git clone
    ADD "https://www.random.org/cgi-bin/randbyte?nbytes=10&format=h" skipcache
    
    RUN eval $(ssh-agent) && \
        ssh-add key && \
        ssh-keyscan -H <REPO_DOMAIN> >> /etc/ssh/ssh_known_hosts && \
        git clone --branch master --single-branch --no-tags git@<REPO_DOMAIN>:blog.git
    
    WORKDIR /blog/blog
    RUN git branch
    RUN sed -i 's~<BASE_URL>~'${BASE_URL}'~g' config.toml
    
    RUN hugo --minify
    
    # uncomment to debug
    #RUN sleep 1h
    
    # serve previously build static page
    FROM httpd:alpine
    
    WORKDIR /usr/local/apache2/htdocs
    
    COPY --from=build /blog/blog/public .
    
  3. Create docker-compose.yml
    services:
        blog:
            build: 
                context: .
                args:
                    BASE_URL: "https://blog.${DOMAIN}"
            restart: "always"
            networks: ["server"]
            labels:
                - "traefik.enable=true"
                - "traefik.http.routers.blog.rule=Host(`blog.${DOMAIN}`)"
    
    networks:
        server:
        external: true