micromdm / scep

Go SCEP server
MIT License
310 stars 121 forks source link

Building on Ubuntu and running on Docker Alpine results in "no such file or directory" #188

Closed NxSoftware closed 2 years ago

NxSoftware commented 2 years ago

When building the executables using make docker in Ubuntu 18.04 or 20.04 and then attempting to run the Docker container as defined by the Dockerfile (using alpine:3) the container immediately exits with the following error:

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

After discovering the following Stack Overflow post it would appear that there is some dynamic linking going on and the paths are different on Alpine.

https://stackoverflow.com/questions/36279253/go-compiled-binary-wont-run-in-an-alpine-docker-container-on-ubuntu-host

I was able to solve this by adding the following line to the Dockerfile:

RUN apk add --no-cache libc6-compat

Though to be honest I'm not a Go developer so not sure if this is the best approach to fix the issue.

korylprince commented 2 years ago

This is a pretty common issue. Alpine uses musl, while Ubuntu uses glibc. Sometimes there are compatibility issues. The proper fix here is to make sure you're building on the same platform as your container will run. i.e. multi-stage builds.

However, you can also just disable CGo so that the binary isn't linked at all (also in the SO page you linked):

$ CGO_ENABLED=0 make docker
$ docker build . -t <some tag>

This means scepserver will use Go's built in DNS, user, and group resolution and probably some other stuff. I don't think any of that would affect scepserver though.

NxSoftware commented 2 years ago

Thanks for the quick reply. I've gone with the CGO_ENABLED flag for now but will explore setting up a multi-stage build

korylprince commented 2 years ago

@NxSoftware I have a minimal example here, if you want a starting place.