Skip to content

Commit

Permalink
Refactor Dockerfile to use cache for go modules and package installation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Oct 22, 2024
1 parent 3f349fb commit 2e4530c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 8 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@ FROM golang:1.23-bullseye AS deps

WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download

FROM golang:1.23-bullseye AS build

WORKDIR /app
COPY --from=deps /go/pkg/mod /go/pkg/mod
COPY . .
RUN apt-get update && apt-get install -y gcc g++
RUN --mount=type=cache,target=/var/cache/apt \
apt-get update && apt-get install -y gcc g++
ENV CGO_ENABLED=1 \
GOOS=linux \
GOARCH=amd64

RUN go build -o /app/bin/sentinel ./
RUN --mount=type=cache,target=/root/.cache/go-build \
go build -o /app/bin/sentinel ./

FROM debian:bullseye-slim
RUN apt-get update && apt-get install -y ca-certificates curl && rm -rf /var/lib/apt/lists/*
RUN --mount=type=cache,target=/var/cache/apt \
apt-get update && apt-get install -y ca-certificates curl && rm -rf /var/lib/apt/lists/*
ENV GIN_MODE=release
COPY --from=build /app/ /app
COPY --from=build /app/bin/sentinel /app/sentinel
Expand Down
12 changes: 8 additions & 4 deletions Dockerfile.arm64
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@ FROM golang:1.23-bullseye AS deps

WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download

FROM golang:1.23-bullseye AS build

WORKDIR /app
COPY --from=deps /go/pkg/mod /go/pkg/mod
COPY . .
RUN apt-get update && apt-get install -y gcc g++
RUN --mount=type=cache,target=/var/cache/apt \
apt-get update && apt-get install -y gcc g++
ENV CGO_ENABLED=1 \
GOOS=linux \
GOARCH=arm64

RUN go build -o /app/bin/sentinel ./
RUN --mount=type=cache,target=/root/.cache/go-build \
go build -o /app/bin/sentinel ./

FROM debian:bullseye-slim
RUN apt-get update && apt-get install -y ca-certificates curl && rm -rf /var/lib/apt/lists/*
RUN --mount=type=cache,target=/var/cache/apt \
apt-get update && apt-get install -y ca-certificates curl && rm -rf /var/lib/apt/lists/*
ENV GIN_MODE=release
COPY --from=build /app/ /app
COPY --from=build /app/bin/sentinel /app/sentinel
Expand Down

0 comments on commit 2e4530c

Please sign in to comment.