Q:

Running NodeJs binary with scratch Docker image

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?

A:

I think @Isanych is right, as scratch is good to run c++, go binaries but I did not find a way to run pkg executable file on scratch image, so here is the solution that is based on alpine, but alpine still needs some dependency that is mentioned here, and it's working with below image
You can try this
FROM node:10 as build
WORKDIR /app
COPY index.js .
RUN npm i pkg -g
RUN pkg -t node10-alpine-x64 index.js
FROM alpine
RUN apk add --no-cache libstdc++ libgcc
WORKDIR /app
COPY --from=build /app/ .
CMD ["./index"]

Bonus: your image is still below then 50 MB.

A:

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.

