【问题标题】:Running NodeJs binary with scratch Docker image使用临时 Docker 映像运行 NodeJs 二进制文件
【发布时间】:2020-06-16 01:53:05
【问题描述】:

我们可以使用pkg package 将 NodeJs 应用程序转换为二进制文件。我想构建二进制文件并使用 Docker scratch 映像运行相同的文件。

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"]

当我运行 docker build -t index . && docker run --rm -it index 时,我收到此错误消息 - standard_init_linux.go:211: exec user process caused "no such file or directory"

我错过了什么?

【问题讨论】:

    标签: node.js docker binary dockerfile alpine


    【解决方案1】:

    scratch 是一个完全没有文件的空映像,您的二进制文件可能有依赖关系并需要特定的 linux 环境。尝试使用最小的 linux 基础镜像而不是从头开始 - alpinedebianubuntu

    【讨论】:

      【解决方案2】:

      我认为@Isanych 是对的,因为scratch 可以很好地运行c++,转到二进制文件但我没有找到在scratch 图像上运行pkg 可执行文件的方法,所以这里是基于的解决方案在 alpine 上,但 alpine 仍然需要here 提到的一些依赖项,并且它正在与下图一起使用

      你可以试试这个

      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"]
      

      奖励:您的图片仍低于 50 MB。

      【讨论】:

        【解决方案3】:

        您可以使用像https://github.com/astefanutti/scratch-node 这样的无发行版Node.js 图像作为基础图像,例如:

        FROM node as builder
        WORKDIR /app
        COPY package.json package-lock.json index.js ./
        RUN npm install --prod
        
        FROM astefanutti/scratch-node
        COPY --from=builder /app /
        ENTRYPOINT ["node", "index.js"]
        

        Node 16 基础映像的大小为 17.1 MB 压缩。

        【讨论】:

          猜你喜欢
          • 2022-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-30
          • 1970-01-01
          • 2012-08-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多