From 9360749e29ac5f6dad8d28250cdc9bc8a616dc99 Mon Sep 17 00:00:00 2001 From: 0xtr Date: Fri, 27 Oct 2023 19:39:10 +0200 Subject: [PATCH] Add Tailwind bundling step to Dockerfile Refactors the Dockerfile into multiple stages. First stage is to build the minified Tailwind CSS bundle. Next it builds the final Go binary. In the last step, it builds the Docker image that will run the application. Alpine has minimal with dependencies which gives us a smaller attack surface. --- Dockerfile | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 316c0bf..355f71f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,24 +1,46 @@ -FROM golang:1.21.2 as builder +#### Tailwind CSS build stage +FROM node:20 as tailwindbuilder +# Set a temporary work directory +WORKDIR /app/tailwind + +# Copy in the project files +COPY . . + +# Install Tailwind CLI +RUN npm install tailwindcss + +# Generate minified Tailwind CSS bundle +RUN npx tailwind -i tailwind.css -o tailwind-bundle.min.css --minify + +#### Go build stage +FROM golang:1.21.2 as gobuilder + +# Set a temporary work directory WORKDIR /app +# Add necessary go files COPY go.mod go.sum ./ RUN go mod download COPY . . +# Copy minified Tailwind CSS bundle +COPY --from=tailwindbuilder /app/tailwind/tailwind-bundle.min.css ./static/tailwind-bundle.min.css + +# Build the go binary RUN CGO_ENABLED=0 GOOS=linux go build -o main . -#### - -FROM alpine:latest +#### Build final image +FROM alpine:latest RUN apk --no-cache add ca-certificates WORKDIR /root/ -COPY --from=builder /app/main . - -CMD ["./main"] +# Copy Go binary +COPY --from=gobuilder /app/main . +# Run the application +CMD ["./main"]