Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add caching for go modules in docker #1005

Merged
merged 5 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ WORKDIR /workspace/$SERVICE_NAME
RUN mkdir -p /workspace/build
# Pre-copy/cache go.mod for pre-downloading dependencies and only redownloading
COPY $SERVICE_NAME/go.mod $SERVICE_NAME/go.sum ./
RUN go mod download && go mod verify
RUN --mount=type=cache,target=/go/pkg \
go mod download && go mod verify
# Copy rest of source code
COPY $SERVICE_NAME/ .
# Build for target architecture
ARG TARGETARCH
RUN go mod tidy
RUN CGO_ENABLED=0 GOARCH=$TARGETARCH go build -a -o /workspace/build/$SERVICE_NAME main.go
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
CGO_ENABLED=0 GOARCH=$TARGETARCH \
go build -a -o /workspace/build/$SERVICE_NAME main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
11 changes: 8 additions & 3 deletions odiglet/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ COPY common/ common/
COPY procdiscovery/ procdiscovery/
WORKDIR /go/src/github.com/keyval-dev/odigos/odiglet
# Pre-copy/cache go.mod for pre-downloading dependencies and only redownloading
# them in subsequent builds if they change
COPY odiglet/go.mod odiglet/go.sum ./
RUN go mod download
RUN --mount=type=cache,target=/go/pkg \
go mod download && go mod verify
# Copy rest of source code
COPY odiglet/ .

ARG TARGETARCH
# Go does not call go generate on dependencies, so we need to do it manually
RUN cd $(go list -m -f '{{.Dir}}' "go.opentelemetry.io/auto") && make generate
RUN GOOS=linux GOARCH=$TARGETARCH go build -o odiglet cmd/main.go
RUN --mount=type=cache,target=/go/pkg \
cd $(go list -m -f '{{.Dir}}' "go.opentelemetry.io/auto") && make generate
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
GOOS=linux GOARCH=$TARGETARCH go build -o odiglet cmd/main.go

WORKDIR /instrumentations

Expand Down
11 changes: 8 additions & 3 deletions odiglet/debug.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ FROM keyval/odiglet-base:v1.0 as builder
WORKDIR /go/src/github.com/keyval-dev/odigos
COPY . .
WORKDIR ./odiglet/
RUN go mod download
RUN --mount=type=cache,target=/go/pkg \
go mod download && go mod verify
# Go does not call go generate on dependencies, so we need to do it manually
RUN cd $(go list -m -f '{{.Dir}}' "go.opentelemetry.io/auto") && make generate
RUN GOOS=linux go build -gcflags "all=-N -l" -o odiglet cmd/main.go
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
bash -c 'cd $(go list -m -f "{{.Dir}}" "go.opentelemetry.io/auto") && make generate'
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
GOOS=linux go build -gcflags "all=-N -l" -o odiglet cmd/main.go

# Install delve
RUN go install github.com/go-delve/delve/cmd/dlv@latest
Expand Down
Loading