CGO_Enabled=1 required building GORM Docker container

  alpine, docker, go, go-gorm, sqlite

I am trying to compile an Alpine Go container which uses GORM and it’s SQLite driver for an in-memory database. This depends on CGO being enabled. My binary builds and executes fine using go build ., but when running my docker image I get the error message:

standard_init_linux.go:219: exec user process caused: no such file or directory

I am building on Windows 10 64 bit. I have gcc installed. Both C:TDM-GCC-64bin and C:cygwin64bin are in my $env:path.

My docker file is as follows:

FROM golang:1.16.4-alpine AS builder

RUN apk update 
    && apk add --no-cache git 
    && apk add --no-cache ca-certificates 
    && apk add --update gcc musl-dev 
    && update-ca-certificates

# Create a user so that the image doens't run as root
RUN adduser 
    --disabled-password 
    --gecos "" 
    --home "/nonexistent" 
    --shell "/sbin/nologin" 
    --no-create-home 
    --uid "100001" 
    "appuser"

# Set the working directory inside the container.
WORKDIR $GOPATH/src/app

# Copy all files from the current directory to the working directory
COPY . .

# Fetch dependencies.
RUN go get -u -d -v

# Go build the binary, specifying the final OS and architecture we're looking for
RUN GOOS=linux CGO_ENABLED=1 GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/app -tags timetzdata

FROM scratch

# Import the user and group files from the builder.
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

# Copy our static executable.
COPY --from=builder /go/bin/app/go/bin/app

# Use the user that we've just created, one that isn't root
USER appuser:appuser

ENTRYPOINT ["/go/bin/app"]

Can you please help me understand why my docker image won’t run?

In case it’s of any value, the line of Go code to open the DB is like so. As this works using Go build locally or using go run . I don’t think this is relevant however.

db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})

Source: Docker Questions

LEAVE A COMMENT