| 1 |
# GitHub: https://github.com/gohugoio |
| 2 |
# Twitter: https://twitter.com/gohugoio |
| 3 |
# Website: https://gohugo.io/ |
| 4 |
|
| 5 |
ARG GO_VERSION="1.27" |
| 6 |
ARG ALPINE_VERSION="3.24" |
| 7 |
ARG DART_SASS_VERSION="1.79.3" |
| 8 |
|
| 9 |
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.5.0 AS xx |
| 10 |
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS gobuild |
| 11 |
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS gorun |
| 12 |
|
| 13 |
|
| 14 |
FROM gobuild AS build |
| 15 |
|
| 16 |
RUN apk add clang lld |
| 17 |
|
| 18 |
# Set up cross-compilation helpers |
| 19 |
COPY --from=xx / / |
| 20 |
|
| 21 |
ARG TARGETPLATFORM |
| 22 |
RUN xx-apk add musl-dev gcc g++ |
| 23 |
|
| 24 |
# Optionally set HUGO_BUILD_TAGS to "none" or "withdeploy" when building like so: |
| 25 |
# docker build --build-arg HUGO_BUILD_TAGS=withdeploy . |
| 26 |
# |
| 27 |
# We build the extended version by default. |
| 28 |
ARG HUGO_BUILD_TAGS="extended" |
| 29 |
ENV CGO_ENABLED=1 |
| 30 |
ENV GOPROXY=https://proxy.golang.org |
| 31 |
ENV GOCACHE=/root/.cache/go-build |
| 32 |
ENV GOMODCACHE=/go/pkg/mod |
| 33 |
ARG TARGETPLATFORM |
| 34 |
|
| 35 |
WORKDIR /go/src/github.com/gohugoio/hugo |
| 36 |
|
| 37 |
# For --mount=type=cache the value of target is the default cache id, so |
| 38 |
# for the go mod cache it would be good if we could share it with other Go images using the same setup, |
| 39 |
# but the go build cache needs to be per platform. |
| 40 |
# See this comment: https://github.com/moby/buildkit/issues/1706#issuecomment-702238282 |
| 41 |
RUN --mount=target=. \ |
| 42 |
--mount=type=cache,target=/go/pkg/mod \ |
| 43 |
--mount=type=cache,target=/root/.cache/go-build,id=go-build-$TARGETPLATFORM <<EOT |
| 44 |
set -ex |
| 45 |
xx-go build -tags "$HUGO_BUILD_TAGS" -ldflags "-s -w -X github.com/gohugoio/hugo/common/hugo.vendorInfo=docker" -o /usr/bin/hugo |
| 46 |
xx-verify /usr/bin/hugo |
| 47 |
EOT |
| 48 |
|
| 49 |
# dart-sass downloads the dart-sass runtime dependency |
| 50 |
FROM alpine:${ALPINE_VERSION} AS dart-sass |
| 51 |
ARG TARGETARCH |
| 52 |
ARG DART_SASS_VERSION |
| 53 |
ARG DART_ARCH=${TARGETARCH/amd64/x64} |
| 54 |
WORKDIR /out |
| 55 |
ADD https://github.com/sass/dart-sass/releases/download/${DART_SASS_VERSION}/dart-sass-${DART_SASS_VERSION}-linux-${DART_ARCH}.tar.gz . |
| 56 |
RUN tar -xf dart-sass-${DART_SASS_VERSION}-linux-${DART_ARCH}.tar.gz |
| 57 |
|
| 58 |
FROM gorun AS final |
| 59 |
|
| 60 |
COPY --from=build /usr/bin/hugo /usr/bin/hugo |
| 61 |
|
| 62 |
# libc6-compat are required for extended libraries (libsass, libwebp). |
| 63 |
RUN apk add --no-cache \ |
| 64 |
libc6-compat \ |
| 65 |
git \ |
| 66 |
runuser \ |
| 67 |
nodejs \ |
| 68 |
npm \ |
| 69 |
openssh-client \ |
| 70 |
tar |
| 71 |
|
| 72 |
RUN mkdir -p /var/hugo/bin /cache && \ |
| 73 |
addgroup -Sg 1000 hugo && \ |
| 74 |
adduser -Sg hugo -u 1000 -h /var/hugo hugo && \ |
| 75 |
chown -R hugo: /var/hugo /cache && \ |
| 76 |
# For the Hugo's Git integration to work. |
| 77 |
runuser -u hugo -- git config --global --add safe.directory /project && \ |
| 78 |
# See https://github.com/gohugoio/hugo/issues/9810 |
| 79 |
runuser -u hugo -- git config --global core.quotepath false |
| 80 |
|
| 81 |
USER hugo:hugo |
| 82 |
VOLUME /project |
| 83 |
WORKDIR /project |
| 84 |
ENV HUGO_CACHEDIR=/cache |
| 85 |
ENV PATH="/var/hugo/bin:$PATH" |
| 86 |
|
| 87 |
COPY scripts/docker/entrypoint.sh /entrypoint.sh |
| 88 |
COPY --from=dart-sass /out/dart-sass /var/hugo/bin/dart-sass |
| 89 |
|
| 90 |
# Update PATH to reflect the new dependencies. |
| 91 |
# For more complex setups, we should probably find a way to |
| 92 |
# delegate this to the script itself, but this will have to do for now. |
| 93 |
# Also, the dart-sass binary is a little special, other binaries can be put/linked |
| 94 |
# directly in /var/hugo/bin. |
| 95 |
ENV PATH="/var/hugo/bin/dart-sass:$PATH" |
| 96 |
|
| 97 |
# Expose port for live server |
| 98 |
EXPOSE 1313 |
| 99 |
|
| 100 |
ENTRYPOINT ["/entrypoint.sh"] |
| 101 |
CMD ["--help"] |