2

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 (docker build . followed by docker run $imagename) 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-64\bin and C:\cygwin64\bin are in my $env:path. I have changed the line endings to Linux style (LF) for all files in the package.

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{})
Voran
  • 39
  • 1
  • 4

2 Answers2

0

Libc not exist in scratch. I changed it to alpine and it solved this error.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
outdead
  • 448
  • 6
  • 15
0

Solution was to install both gcc and alpine-sdk packages using apk in the Dockerfile. Only gcc was being installed in the question above.

Voran
  • 39
  • 1
  • 4
  • Thank you for your answer, however it is a little lacking in detail and is likely to just be deleted. You could say which build step did you install this in for example? You should explain why this is the answer. – Bracken Nov 15 '21 at 17:55
  • Should have been `RUN apk add build-base` – claus Mar 01 '23 at 13:37