Skip to content

Optimize Docker build with bind mounts #208

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

Merged
merged 5 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Optimize Docker build with bind mounts
This commit further optimize the Docker builds on top of PR #92 with:

1. Add .dockerignore file to exclude non-source code files [1].

2. Use Alpine image variant for build stage to reduce download size.
   golang:1.23.7-alpine is 200 MB smaller than golang:1.23.7 [2][3].

3. Replace COPY instruction with RUN --mount=type=bind. Bind mounts do
   not add unnecessary layers to the cache [4][5].

[1]: https://docs.docker.com/build-cloud/optimization/#dockerignore-files
[2]: https://hub.docker.com/layers/library/golang/1.23.7-alpine/images/sha256-333d4ba78773b3a3ae9cf2cff8962df56effc5c9481faa355f211abf2baf175c
[3]: https://hub.docker.com/layers/library/golang/1.23.7/images/sha256-2087a99c3235972660b3d35c1564d9d1a3f639dcace9c790acbabc7e938d1570
[4]: https://docs.docker.com/build/building/best-practices/#add-or-copy
[5]: https://docs.docker.com/build/cache/optimize/#use-bind-mounts

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee committed Apr 10, 2025
commit db3d8f703b1b0c76c1b3fe0eee82cdeb1f293ee4
11 changes: 11 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.github
.vscode
script
third-party
.dockerignore
.gitignore
**/*.yml
**/*.yaml
**/*.md
**/*_test.go
LICENSE
24 changes: 14 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
FROM golang:1.23.7-alpine AS build
ARG VERSION="dev"

FROM golang:1.23.7 AS build
# allow this step access to build arg
ARG VERSION
# Set the working directory
WORKDIR /build

RUN go env -w GOMODCACHE=/root/.cache/go-build
# Install git
RUN --mount=type=cache,target=/var/cache/apk \
apk add git

# Install dependencies
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/root/.cache/go-build go mod download
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=bind,source=go.mod,target=go.mod \
--mount=type=bind,source=go.sum,target=go.sum \
go mod download

COPY . ./
# Build the server
RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -ldflags="-s -w -X main.version=${VERSION} -X main.commit=$(git rev-parse HEAD) -X main.date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
-o github-mcp-server cmd/github-mcp-server/main.go
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=bind,target=. \
CGO_ENABLED=0 go build -ldflags="-s -w -X main.version=${VERSION} -X main.commit=$(git rev-parse HEAD) -X main.date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
-o /bin/github-mcp-server cmd/github-mcp-server/main.go

# Make a stage to run the app
FROM gcr.io/distroless/base-debian12
# Set the working directory
WORKDIR /server
# Copy the binary from the build stage
COPY --from=build /build/github-mcp-server .
COPY --from=build /bin/github-mcp-server .
# Command to run the server
CMD ["./github-mcp-server", "stdio"]