Go build in docker container is way slower than on the root system

Viewed 2088

I'm trying to build a go package with the following command:

CGO_ENABLED=0 GOOS=linux go build -o bin/router -installsuffix cgo -ldflags '-w'

This takes around 0.5 seconds on my pc. The same command in a docker container takes 45 seconds.

RUN CGO_ENABLED=0 GOOS=linux go build -o /app/router -installsuffix cgo -ldflags '-w' /build/src/global/router

Locally I have go version 1.12.9 linux/amd64. The docker container uses the golang:1.13 image as base.

My guess would be that the docker build process has less CPU available but does that make this big of a difference? What could be the cause for this issue?

This is reproducable with a minimal example:

main.go:

package main

import "log"

func main() {
    log.Println("test")
}

Dockerfile:

FROM golang:1.13

ADD main.go .

RUN CGO_ENABLED=0 GOOS=linux go build -o main -installsuffix cgo -ldflags '-w'

CMD [ "main" ]

The command inside the docker container takes about 5 seconds compared to < 0.1 seconds on my pc.

1 Answers

In my opinion, the problem isn't build on container itself.

$ go clean
$ time GO_ENABLED=0 GOOS=linux go build -o main -installsuffix cgo -ldflags '-w'

real    0m0,233s
user    0m0,278s
sys 0m0,094s

$ time GO_ENABLED=0 GOOS=linux go build -o main -installsuffix cgo -ldflags '-w'

real    0m0,076s
user    0m0,098s
sys 0m0,053s

$ go clean
$ time GO_ENABLED=0 GOOS=linux go build -o main -installsuffix cgo -ldflags '-w'

real    0m0,238s
user    0m0,315s
sys 0m0,070s

You can see the times that build takes after 'go clean' in comparation consecutives build.

SYNOPSIS go clean [-i] [-r] [-n] [-x] [ packages ]

DESCRIPTION Clean

removes object files from package source directories. The go command builds most objects in a temporary directory, so go clean is mainly concerned with object files left by other tools or by manual invocations of go build.

https://manpages.debian.org/testing/golang-go/go-clean.1.en.html

In each try to build on your computer are you clearing your build folder?

Related