Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang + Linux + Docker error : standard_init_linux.go:211 : no such file or directory

Tags:

linux

docker

go

This is not a duplicate because although the error is same, my use case is different from others.

I am compiling a go application to deploy with docker on:

  • latest arch linux
  • latest docker version, golang:alpine image
  • tried with go version 1.13.3 and 1.14.4 linux amd64
  • i have no bash scripts or wrong file endings. Whole project is written on this Linux machine
  • i can deploy an empty go app that has only a fmt print without any error

however,

when i build it on my OSX machine, and send it to linux, I can deploy that executable to docker without any error

  • OSX mojave
  • latest docker
  • go 1.13.3
  • GOOS=linux

Error :

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

like image 791
keser Avatar asked Dec 23 '22 17:12

keser


1 Answers

When you compile your go app with cgo enabled the compiler links dynamically to libstdc .

However, golang:alpine image is so small, because it is not using libstdc but a simplified version of it called musl libc.

The error message says

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

If you connect to your image with

$ docker run -it [image] /bin/sh

you can find your executable in let's say /dist/myexec, but when you try to run that executable, it says err not found, because of not that it can't find your executable, obviously, but it can't find file libc .

The solution is to either

  • disable CGO : use CGO_ENABLED=0 while building

  • or add

    RUN apk add --no-cache libc6-compat
    

    to your Dockerfile

  • or do not use golang:alpine

To have a all static binary executable, build it with:

$ CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' .
like image 52
keser Avatar answered Apr 09 '23 01:04

keser