We can convert NodeJs applications into binaries with the pkg
package. I want to build the binary and run the same with the Docker scratch
image.
index.js
const http = require('http')
http.createServer().listen(3000)
Dockerfile
FROM node:10 as build
COPY index.js .
RUN npm i pkg -g && pkg -t node10-alpine-x64 index.js
FROM scratch
COPY --from=build index /index
ENTRYPOINT ["/index"]
When I run docker build -t index . && docker run --rm -it index
, I get this error message - standard_init_linux.go:211: exec user process caused "no such file or directory"
.
What am I missing?
scratch
is an empty image with no files at all, your binary may have dependencies and expect certain linux environment. Try to use minimal linux base images instead of scratch - alpine
, debian
, ubuntu
.