無法在 aws lambda 中為 node-alpine 創建 docker

尤克西

我在嘗試為基於節點 js 打字稿圖像 (NestJs) 的 aws lambda 創建 docker 圖像時遇到以下錯誤,當我使用帶有處理程序函數的示例 app.js 文件時也會發生此錯誤

internal/modules/cjs/loader.js:905
  throw err;
  ^

Error: Cannot find module '/function/main.handler'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []

我的運行命令:

docker run --rm -p 9001:8080 lambda-test:latest
ARG FUNCTION_DIR="/function"

#*****************************************************************************
#  Builder Stage
#****************************************************************************/
FROM node:14-alpine AS builder

# Include global arg in this stage of the build
ARG FUNCTION_DIR

# The working directory is where "npm install" created the node_modules folder.
WORKDIR ${FUNCTION_DIR}

# Install aws-lambda-cpp build dependencies. aws-lambda-cpp is used by aws-lambda-ric which is a
# node-gyp compiled dependency. Find it in package.json.
# See the Node.js example at https://github.com/aws/aws-lambda-nodejs-runtime-interface-client

RUN apk add --no-cache \
    libstdc++ \
    build-base \
    libtool \
    autoconf \
    automake \
    libexecinfo-dev \
    make \
    cmake \
    libcurl \
    python3


RUN mkdir -p ${FUNCTION_DIR}
COPY . ${FUNCTION_DIR}

# Install the AWS Lambda Runtime Interface Client (RIC) that is only required within this Docker container (not in package.json on development machine).
# It helps AWS to run the Lambda function code that autoiXpert provides.
RUN npm install
RUN npm install aws-lambda-ric
RUN npx tsc
RUN npm prune --production
#*****************************************************************************
#  Production Stage
#****************************************************************************/
FROM node:14-alpine

# Include global arg in this stage of the build
ARG FUNCTION_DIR

# The working directory is where "npm install" created the node_modules folder.
WORKDIR ${FUNCTION_DIR}

# If this directory does not exist, lambda shows an annoying warning.
RUN mkdir -p /opt/extensions

COPY --from=builder ${FUNCTION_DIR}/node_modules ${FUNCTION_DIR}/node_modules
COPY --from=builder ${FUNCTION_DIR}/package*.json ${FUNCTION_DIR}
COPY --from=builder ${FUNCTION_DIR}/dist/src* ${FUNCTION_DIR}

CMD [ "main.handler" ]

所有文件都正確定位

當我使用 aws 基礎圖像時,它運行良好,但圖像大小約為 800mb,而 alpine 為 300mb

班魚都

您錯過ENTRYPOINT ["/usr/local/bin/npx", "aws-lambda-ric"]了 Dockerfile 的最後階段。

圖片node:14-alpine的默認 ENTRY_POINT 是

#!/bin/sh
set -e

if [ "${1#-}" != "${1}" ] || [ -z "$(command -v "${1}")" ]; then
  set -- node "$@"
fi

exec "$@"

SoCMD [ "main.handler" ]等價於node main.handler,這導致Error: Cannot find module '/function/main.handler'

要解決此問題,您應該將入口點更改為aws-lambda-ricwith

ENTRYPOINT ["/usr/local/bin/npx", "aws-lambda-ric"]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Alpine Docker中安装Docker

無法為字典創建“設置”訪問器

訪問 aws_eks_node_group 創建的 aws_autoscaling_group 組

无法使用 Linux Alpine docker 为 AWS lambda 构建 zip 文件

無法創建 AWS EBS 多附加卷

無法在 AutoHotkey 中創建多線陣列

在 IBM MQ 中創建隊列 (docker compose)

無法從 Web 服務在數據庫中創建用戶和更新用戶

無法在單個 Pod 中創建多個容器

無法創建 ViewModel 的實例

無法在 MongoDB 中創建數組 - MERN 堆棧

無法在 Apache Spark 中創建 HIVE 表

創建seaborn熱圖時“無法將字符串轉換為浮點數”ValueError

無法在 Minikube 中創建 Pod

無法訪問在 Node.js 中創建的 localhost:PORT 和 express

如果使用 Azure 基於角色的訪問控制,則無法在 Azure Key Vault 中創建機密

第二個node.js無法在docker compose中連接到mysql

無法為場景步驟創建黃瓜表達式

為什麼我的搜索功能在我的 Python 3 數據庫和表創建項目中無法正常工作?我似乎無法在我的數據庫中搜索它

使用 Terraform 使用 AWS Lambda 創建應用程序 Auto Scaling 的問題

如何在 JS/node 中創建無限加法輸出循環?

Aws lambda 無法讀取加密隊列

無法在excel中創建公式以滿足某些條件

Terraform 無法在已存在的組中創建資源

無法在 iframe 中創建 div

為什麼我的 iOS 應用無法使用 Node.js Agora 令牌服務器創建的令牌通過 AgoraRtcEngineKit 進行身份驗證?

在 API 中,我無法訪問我在 vue js 中創建的 FormData 對象

為什麼我的 node js 腳本無法連接到我在 docker 中運行的 mssql 數據庫,但 Microsoft SQL Server Management 可以

docker-machine 無法在 macOS 上創建機器,VBoxManage 返回 E_ACCESSDENIED 錯誤