Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit29bf8bf

Browse files
authored
Optimize Docker build with bind mounts (#208)
* Optimize Docker build with bind mountsThis 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-mountsSigned-off-by: Eng Zer Jun <engzerjun@gmail.com>* Remove `go mod download` step`go build` will automatically download module dependencies. In manycases, that is a much smaller set of modules than what is downloaded by`go mod download`.Size of GOMODCACHE with `go mod download:$ go clean -i -r -cache -modcache$ go mod download$ du -sh ~/go/pkg/mod186M/home/jun/go/pkg/modSize of GOMODCACHE with `go build`:$ go clean -i -r -cache -modcache$ CGO_ENABLED=0 go build -ldflags="-s -w" cmd/github-mcp-server/main.gogo: downloading github.com/spf13/viper v1.20.1go: downloading github.com/mark3labs/mcp-go v0.18.0go: downloading github.com/google/go-github/v69 v69.2.0go: downloading github.com/sirupsen/logrus v1.9.3go: downloading github.com/spf13/cobra v1.9.1go: downloading golang.org/x/sys v0.31.0go: downloading github.com/spf13/afero v1.14.0go: downloading github.com/fsnotify/fsnotify v1.8.0go: downloading github.com/spf13/cast v1.7.1go: downloading github.com/go-viper/mapstructure/v2 v2.2.1go: downloading github.com/subosito/gotenv v1.6.0go: downloading gopkg.in/yaml.v3 v3.0.1go: downloading github.com/spf13/pflag v1.0.6go: downloading github.com/pelletier/go-toml/v2 v2.2.3go: downloading github.com/sagikazarmark/locafero v0.9.0go: downloading golang.org/x/text v0.23.0go: downloading github.com/google/uuid v1.6.0go: downloading github.com/yosida95/uritemplate/v3 v3.0.2go: downloading github.com/sourcegraph/conc v0.3.0go: downloading github.com/google/go-querystring v1.1.0$ du -sh ~/go/pkg/mod80M/home/jun/go/pkg/modReference:https://stackoverflow.com/a/68172023/7902371Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
1 parent0ca07aa commit29bf8bf

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

‎.dockerignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.github
2+
.vscode
3+
script
4+
third-party
5+
.dockerignore
6+
.gitignore
7+
**/*.yml
8+
**/*.yaml
9+
**/*.md
10+
**/*_test.go
11+
LICENSE

‎Dockerfile

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1+
FROM golang:1.24.2-alpine AS build
12
ARG VERSION="dev"
23

3-
FROM golang:1.24.2 AS build
4-
# allow this step access to build arg
5-
ARG VERSION
64
# Set the working directory
75
WORKDIR /build
86

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

11-
# Install dependencies
12-
COPY go.mod go.sum ./
13-
RUN --mount=type=cache,target=/root/.cache/go-build go mod download
14-
15-
COPY . ./
1611
# Build the server
17-
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)" \
18-
-o github-mcp-server cmd/github-mcp-server/main.go
12+
# go build automatically download required module dependencies to /go/pkg/mod
13+
RUN --mount=type=cache,target=/go/pkg/mod \
14+
--mount=type=cache,target=/root/.cache/go-build \
15+
--mount=type=bind,target=. \
16+
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)" \
17+
-o /bin/github-mcp-server cmd/github-mcp-server/main.go
1918

2019
# Make a stage to run the app
2120
FROM gcr.io/distroless/base-debian12
2221
# Set the working directory
2322
WORKDIR /server
2423
# Copy the binary from the build stage
25-
COPY --from=build /build/github-mcp-server .
24+
COPY --from=build /bin/github-mcp-server .
2625
# Command to run the server
2726
CMD ["./github-mcp-server","stdio"]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp