/
Go - Slow Scan Command in Containers

Go - Slow Scan Command in Containers

Problem

When running the Golang agent scan command on your Go-based project in a containerized environment without a local cache, the Go toolchain must repeatedly download all module dependencies from the internet, which can significantly slow down builds. This also leads to increased CPU usage and memory consumption during repeated dependency downloads.

Cause

As part of its instrumentation step in the scan command, the go agent does a full compilation and therefore has to resolve all the project dependencies before it. The lack of a build cache means that each new container running the scan command starts from scratch, fetching all necessary modules.

This process repeats for every build without caching, causing heavy network usage and long compile times.

Solution

Pre-Fetch Dependencies

Run go mod download (or go get -d ./... in older versions) to download and cache all dependencies ahead of time. This surfaces any network or dependency issues sooner and streamlines subsequent steps.

Use a Shared Cache

  1. Mount a volume inside your container to store the module downloads and build artifacts. By preserving this directory across builds, you no longer need to re-download dependencies every time.

  2. The mounted directory will contain pre-fetched Go modules (e.g., from go mod download) and any build artifacts (e.g., from go build).

  3. Configure Go’s environment variables GOMODCACHE (module cache) and GOCACHE (build cache) to set where to store these caches in the container context. For example:

    FROM golang:1.20 AS builder ENV GOMODCACHE=/cache/go-mod ENV GOCACHE=/cache/go-build WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . #Download and execute Golang agent commands

    In the above, any dependencies downloaded by go mod download and build artifacts generated by go build are stored in /cache, which you can persist by mounting it as a volume.

By combining pre-fetching with a shared cache, you drastically reduce both build times and network overhead, keeping container builds faster, more consistent, and less dependent on external network availability.

Related content

SeaLights Go agent
SeaLights Go agent
More like this
Using Go Agent - Initializing agent and Generating a session ID
Using Go Agent - Initializing agent and Generating a session ID
More like this
Using Go Agent - Running your instrumented application for testing
Using Go Agent - Running your instrumented application for testing
More like this
Using Go Agent - Scanning a build
Using Go Agent - Scanning a build
More like this
Using Go Agent - Running your Tests
Using Go Agent - Running your Tests
More like this