You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker容器内Go构建速度远慢于宿主机的原因排查

Why is Go build so much slower in Docker than locally?

I've run into this exact issue before—local Go builds flying by while Docker builds drag their feet. Let's break down the most likely reasons for this huge speed difference, and how to test them:

1. CPU Resource Constraints

Docker containers don't automatically get full access to your host's CPU cores by default. If your local machine is using multiple cores for the Go build (which Go does by default, using GOMAXPROCS equal to the number of CPU cores), but the Docker container is limited to just 1 core, that alone can cause massive slowdowns.

  • To test this, try running your Docker build with increased CPU allocation:
    docker build --cpus 4 .
    
    If the build time drops significantly, you've found the culprit. You can set permanent CPU limits in your Docker Compose file or Docker daemon config if needed.

2. Missing Build Caching

Local Go builds leverage two layers of caching:

  • The module cache ($GOPATH/pkg/mod) that stores downloaded dependencies.
  • The build cache ($HOME/.cache/go-build) that stores compiled object files to avoid re-compiling unchanged code.

By default, each RUN command in a Dockerfile runs in a clean environment, so these caches aren't preserved between builds. Even for your minimal example with just the standard library, the container might be re-compiling parts of the standard library every time, while your local build reuses cached artifacts.

  • Fix this by adding cache mounts to your Docker build (requires Docker 18.09+):
    FROM golang:1.13
    ADD main.go .
    RUN --mount=type=cache,target=/go/pkg/mod \
        --mount=type=cache,target=/root/.cache/go-build \
        CGO_ENABLED=0 GOOS=linux go build -o main -installsuffix cgo -ldflags '-w'
    CMD [ "main" ]
    
    The first build will still take time, but subsequent builds should be almost as fast as your local one.

3. Container Filesystem Overhead

Docker uses overlay filesystems (like overlay2) which have higher IO latency than your local host's filesystem. Go builds involve a lot of small file reads and writes, and this overhead can add up—especially if your host is using slower storage (like a mechanical HDD) or the container's storage is network-mounted.

  • You can test this by running a simple IO benchmark inside the container:
    docker run --rm golang:1.13 dd if=/dev/zero of=/tmp/test bs=1M count=1024 conv=fdatasync
    
    Compare the speed to running the same command locally. If the container's IO is significantly slower, that's contributing to the build delay.

4. Go Version Differences

Your local environment uses Go 1.12.9, while the Docker image uses 1.13. While the difference between these versions isn't huge, there could be subtle changes in the compiler's performance or default behavior that affect build times.

  • Try switching your Docker image to golang:1.12.9 and see if the build time gets closer to your local result. This will rule out version-specific compiler differences.

Wrapping Up

For your minimal example, the most likely culprits are CPU limits or missing build caching. For your original 45-second build vs 0.5-second local build, it's probably a combination of CPU constraints and no cache reuse—especially if your actual project has more dependencies that need to be downloaded and compiled every time.

内容的提问来源于stack exchange,提问作者jrenk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 19:37:50