Skip to content

Commit bc7cf90

Browse files
sitoleCode42Cate
authored andcommitted
Speedup docker build with cached deps, remove need for dot folders (#1365)
1 parent 4b0aaf8 commit bc7cf90

File tree

10 files changed

+84
-87
lines changed

10 files changed

+84
-87
lines changed

packages/api/Dockerfile

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,38 @@ FROM golang:${GOLANG_VERSION}-alpine${ALPINE_VERSION} AS builder
55

66
RUN apk add --no-cache make
77

8-
# Shared
8+
# Cached golang dependencies
99
WORKDIR /build/shared
10-
11-
COPY .shared/go.mod .shared/go.sum ./
10+
COPY ./shared/go.mod ./shared/go.sum ./
1211
RUN go mod download
1312

14-
COPY .shared/pkg pkg
15-
16-
# Clickhouse
1713
WORKDIR /build/clickhouse
18-
19-
COPY .clickhouse/go.mod .clickhouse/go.sum ./
14+
COPY ./clickhouse/go.mod ./clickhouse/go.sum ./
2015
RUN go mod download
2116

22-
COPY .clickhouse/pkg pkg
23-
24-
# DB
2517
WORKDIR /build/db
18+
COPY ./db/go.mod ./db/go.sum ./
19+
RUN go mod download
2620

27-
COPY .db/go.mod .db/go.sum ./
21+
WORKDIR /build/api
22+
COPY ./api/go.mod ./api/go.sum ./
2823
RUN go mod download
2924

30-
COPY .db .
25+
# Copy source code
26+
WORKDIR /build
3127

32-
# API
33-
WORKDIR /build/api
28+
COPY ./clickhouse/pkg ./clickhouse/pkg
29+
COPY ./shared/pkg ./shared/pkg
30+
COPY ./db/client ./db/client
31+
COPY ./db/dberrors ./db/dberrors
32+
COPY ./db/queries ./db/queries
33+
COPY ./db/types ./db/types
3434

35-
COPY go.mod go.sum Makefile ./
36-
RUN go mod download
35+
COPY ./api/internal ./api/internal
36+
COPY ./api/main.go ./api/main.go
37+
COPY ./api/Makefile ./api/Makefile
3738

38-
COPY internal internal
39-
COPY main.go main.go
39+
WORKDIR /build/api
4040

4141
ARG COMMIT_SHA
4242
ARG EXPECTED_MIGRATION_TIMESTAMP
@@ -50,4 +50,5 @@ COPY --from=builder /build/api/bin/api .
5050

5151
# Set Gin server to the production mode
5252
ENV GIN_MODE=release
53+
5354
ENTRYPOINT [ "./api"]

packages/api/Makefile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,8 @@ profiler:
6969
build-and-upload:
7070
$(eval COMMIT_SHA := $(shell git rev-parse --short HEAD))
7171
$(eval EXPECTED_MIGRATION_TIMESTAMP := $(expectedMigration))
72-
@rm -rf .shared/ .db/ .clickhouse/
73-
@cp -r ../shared .shared/
74-
@cp -r ../db .db/
75-
@cp -r ../clickhouse .clickhouse/
7672
@docker buildx install # sets up the buildx as default docker builder (otherwise the command below won't work)
77-
@docker build --platform linux/amd64 --tag "$(GCP_REGION)-docker.pkg.dev/$(GCP_PROJECT_ID)/$(IMAGE)" --push --build-arg COMMIT_SHA="$(COMMIT_SHA)" --build-arg EXPECTED_MIGRATION_TIMESTAMP="$(EXPECTED_MIGRATION_TIMESTAMP)" .
78-
@rm -rf .shared/ .db/ .clickhouse/
73+
@docker build --platform linux/amd64 --tag "$(GCP_REGION)-docker.pkg.dev/$(GCP_PROJECT_ID)/$(IMAGE)" --push --build-arg COMMIT_SHA="$(COMMIT_SHA)" --build-arg EXPECTED_MIGRATION_TIMESTAMP="$(EXPECTED_MIGRATION_TIMESTAMP)" -f ./Dockerfile ..
7974

8075
.PHONY: test
8176
test:

packages/client-proxy/Dockerfile

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,32 @@ FROM golang:${GOLANG_VERSION}-alpine${ALPINE_VERSION} AS builder
55

66
RUN apk add --no-cache make
77

8+
# Cached golang dependencies
89
WORKDIR /build/shared
10+
COPY ./shared/go.mod ./shared/go.sum ./
11+
RUN go mod download
912

10-
COPY .shared/go.mod .shared/go.sum ./
13+
WORKDIR /build/client-proxy
14+
COPY ./client-proxy/go.mod ./client-proxy/go.sum ./
1115
RUN go mod download
1216

13-
COPY .shared/pkg pkg
17+
# Copy source code
18+
WORKDIR /build
1419

15-
WORKDIR /build/proxy
20+
COPY ./shared/pkg ./shared/pkg
1621

17-
COPY go.mod go.sum Makefile ./
18-
RUN go mod download
22+
COPY ./client-proxy/internal ./client-proxy/internal
23+
COPY ./client-proxy/main.go ./client-proxy/main.go
24+
COPY ./client-proxy/Makefile ./client-proxy/Makefile
1925

20-
COPY internal/ ./internal/
21-
COPY main.go main.go
26+
WORKDIR /build/client-proxy
2227

2328
ARG COMMIT_SHA
2429
RUN --mount=type=cache,target=/root/.cache/go-build make build COMMIT_SHA=${COMMIT_SHA}
25-
RUN chmod +x /build/proxy/bin/client-proxy
30+
RUN chmod +x /build/client-proxy/bin/client-proxy
2631

2732
FROM alpine:${ALPINE_VERSION}
2833

29-
COPY --from=builder /build/proxy/bin/client-proxy .
34+
COPY --from=builder /build/client-proxy/bin/client-proxy .
3035

31-
ENTRYPOINT [ "./client-proxy"]
36+
ENTRYPOINT ["./client-proxy"]

packages/client-proxy/Makefile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ build-debug:
1717
.PHONY: build-and-upload
1818
build-and-upload:
1919
$(eval COMMIT_SHA := $(shell git rev-parse --short HEAD))
20-
@rm -rf .shared/
21-
@cp -r ../shared .shared/
2220
@docker buildx install # sets up the buildx as default docker builder (otherwise the command below won't work)
23-
@docker build --platform linux/amd64 --tag "$(GCP_REGION)-docker.pkg.dev/$(GCP_PROJECT_ID)/$(IMAGE)" --push --build-arg COMMIT_SHA="$(COMMIT_SHA)" .
24-
@rm -rf .shared/
21+
@docker build --platform linux/amd64 --tag "$(GCP_REGION)-docker.pkg.dev/$(GCP_PROJECT_ID)/$(IMAGE)" --push --build-arg COMMIT_SHA="$(COMMIT_SHA)" -f ./Dockerfile ..
2522

2623
openapi := ../../spec/openapi-edge.yml
2724
codegen := go tool github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen

packages/db/Dockerfile

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,31 @@ ARG ALPINE_VERSION=3.22
44
# Builder stage
55
FROM golang:${GOLANG_VERSION}-alpine${ALPINE_VERSION} AS builder
66

7-
# Shared
7+
# Cached golang dependencies
88
WORKDIR /build/shared
9+
COPY ./shared/go.mod ./shared/go.sum ./
10+
RUN go mod download
911

10-
COPY .shared/go.mod .shared/go.sum ./
12+
WORKDIR /build/db
13+
COPY ./db/go.mod ./db/go.sum ./
1114
RUN go mod download
1215

13-
COPY .shared/pkg pkg
16+
# Copy source code
17+
WORKDIR /build
1418

15-
#
16-
WORKDIR /build/db
19+
COPY ./shared/pkg ./shared/pkg
1720

18-
COPY go.mod go.sum ./
19-
RUN go mod download
21+
COPY ./db/scripts/migrator.go ./db/scripts/migrator.go
22+
COPY ./db/migrations/ ./db/migrations
2023

21-
COPY scripts/migrator.go .
24+
WORKDIR /build/db
2225

23-
RUN go build -o ./migrator ./migrator.go
24-
RUN chmod +x ./migrator
26+
RUN go build -o ./bin/migrator ./scripts/migrator.go
27+
RUN chmod +x ./bin/migrator
2528

26-
# Final stage
2729
FROM alpine:${ALPINE_VERSION}
2830

29-
COPY --from=builder /build/db/migrator /usr/local/bin/migrator
30-
31-
WORKDIR /app
32-
COPY /migrations ./migrations
31+
COPY --from=builder /build/db/bin/migrator .
32+
COPY --from=builder /build/db/migrations ./migrations
3333

34-
ENTRYPOINT ["migrator"]
34+
ENTRYPOINT ["./migrator"]

packages/db/Makefile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,4 @@ generate:
4747

4848
.PHONY: build-and-upload
4949
build-and-upload:
50-
@rm -rf .shared/
51-
@cp -r ../shared .shared/
52-
@docker build --platform linux/amd64 --tag "$(GCP_REGION)-docker.pkg.dev/$(GCP_PROJECT_ID)/$(IMAGE)" --push .
53-
@rm -rf .shared/
50+
@docker build --platform linux/amd64 --tag "$(GCP_REGION)-docker.pkg.dev/$(GCP_PROJECT_ID)/$(IMAGE)" --push -f ./Dockerfile ..

packages/docker-reverse-proxy/Dockerfile

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,25 @@ FROM golang:${GOLANG_VERSION}-alpine${ALPINE_VERSION} AS builder
55

66
RUN apk add --no-cache make
77

8+
# Cached golang dependencies
89
WORKDIR /build/shared
10+
COPY ./shared/go.mod ./shared/go.sum ./
11+
RUN go mod download
912

10-
COPY .shared/go.mod .shared/go.sum ./
13+
WORKDIR /build/docker-reverse-proxy
14+
COPY ./docker-reverse-proxy/go.mod ./docker-reverse-proxy/go.sum ./
1115
RUN go mod download
1216

13-
COPY .shared/pkg pkg
17+
# Copy source code
18+
WORKDIR /build
1419

15-
WORKDIR /build/docker-reverse-proxy
20+
COPY ./shared/pkg ./shared/pkg
1621

17-
COPY go.mod go.sum Makefile ./
18-
RUN go mod download
22+
COPY ./docker-reverse-proxy/internal ./docker-reverse-proxy/internal
23+
COPY ./docker-reverse-proxy/main.go ./docker-reverse-proxy/main.go
24+
COPY ./docker-reverse-proxy/Makefile ./docker-reverse-proxy/Makefile
1925

20-
COPY main.go main.go
21-
COPY internal internal
26+
WORKDIR /build/docker-reverse-proxy
2227

2328
ARG COMMIT_SHA
2429
RUN --mount=type=cache,target=/root/.cache/go-build make build COMMIT_SHA=${COMMIT_SHA}
@@ -29,4 +34,4 @@ FROM alpine:${ALPINE_VERSION}
2934

3035
COPY --from=builder /build/docker-reverse-proxy/bin/docker-reverse-proxy .
3136

32-
ENTRYPOINT [ "./docker-reverse-proxy" ]
37+
ENTRYPOINT ["./docker-reverse-proxy"]

packages/docker-reverse-proxy/Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ build-debug:
1616
.PHONY: build-and-upload
1717
build-and-upload:
1818
$(eval COMMIT_SHA := $(shell git rev-parse --short HEAD))
19-
@cp -r ../shared .shared/
2019
@docker buildx install # sets up the buildx as default docker builder (otherwise the command below won't work)
21-
@docker build --platform linux/amd64 --tag "$(GCP_REGION)-docker.pkg.dev/$(GCP_PROJECT_ID)/$(IMAGE)" --push --build-arg COMMIT_SHA="$(COMMIT_SHA)" .
22-
@rm -rf .shared/
20+
@docker build --platform linux/amd64 --tag "$(GCP_REGION)-docker.pkg.dev/$(GCP_PROJECT_ID)/$(IMAGE)" --push --build-arg COMMIT_SHA="$(COMMIT_SHA)" -f ./Dockerfile ..
2321

2422
.PHONY: test
2523
test:

packages/orchestrator/Dockerfile

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,31 @@ ARG DEBIAN_VERSION=bookworm
55

66
FROM golang:${GOLANG_VERSION}-${DEBIAN_VERSION} AS builder
77

8+
# Cached golang dependencies
89
WORKDIR /build/shared
9-
10-
COPY .shared/go.mod .shared/go.sum ./
10+
COPY ./shared/go.mod ./shared/go.sum ./
1111
RUN go mod download
1212

13-
COPY .shared/pkg pkg
14-
1513
WORKDIR /build/clickhouse
14+
COPY ./clickhouse/go.mod ./clickhouse/go.sum ./
15+
RUN go mod download
1616

17-
COPY .clickhouse/go.mod .clickhouse/go.sum ./
17+
WORKDIR /build/orchestrator
18+
COPY ./orchestrator/go.mod ./orchestrator/go.sum ./
1819
RUN go mod download
1920

20-
COPY .clickhouse/pkg pkg
21+
# Copy source code
22+
WORKDIR /build
2123

22-
WORKDIR /build/orchestrator
24+
COPY ./shared/pkg ./shared/pkg
25+
COPY ./clickhouse/pkg ./clickhouse/pkg
2326

24-
COPY go.mod go.sum ./
25-
RUN go mod download
27+
COPY ./orchestrator/internal ./orchestrator/internal
28+
COPY ./orchestrator/cmd ./orchestrator/cmd
29+
COPY ./orchestrator/main.go ./orchestrator/main.go
30+
COPY ./orchestrator/Makefile ./orchestrator/Makefile
2631

27-
COPY . ./
32+
WORKDIR /build/orchestrator
2833

2934
ARG COMMIT_SHA
3035
RUN --mount=type=cache,target=/root/.cache/go-build make build-local COMMIT_SHA=${COMMIT_SHA}

packages/orchestrator/Makefile

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@ generate:
2222
.PHONY: build
2323
build:
2424
$(eval COMMIT_SHA := $(shell git rev-parse --short HEAD))
25-
@rm -rf .shared/
26-
@cp -r ../shared .shared/
27-
@rm -rf .clickhouse/
28-
@cp -r ../clickhouse .clickhouse/
29-
@docker build --platform linux/amd64 --output=bin --build-arg COMMIT_SHA="$(COMMIT_SHA)" .
30-
@rm -rf .shared/
31-
@rm -rf .clickhouse/
25+
@docker build --platform linux/amd64 --output=bin --build-arg COMMIT_SHA="$(COMMIT_SHA)" -f ./Dockerfile ..
3226

3327
.PHONY: build-local
3428
build-local:

0 commit comments

Comments
 (0)