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