无法在Docker下运行AWS SAM CLI [英] Can't run AWS SAM CLI under Docker

查看:92
本文介绍了无法在Docker下运行AWS SAM CLI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 AWS SAM CLI 创建Docker映像,但是调用任何函数都会产生错误:无法导入模块'index'".我可以在Docker外部成功运行相同的测试用例.您可以在此处克隆测试用例,或查看以下文件.

I'm trying to create a Docker image for AWS SAM CLI, but invoking any function gives an error: "Unable to import module 'index'". I can run the same test case outside of Docker sucessfully. You can clone the test case here or see the files below.

我已经尝试了以下方法:

I already tried the following:

  • 将文件和父文件夹的权限设置为777(或755).
  • 在Docker守护进程中禁用SELinux(或启用它).
  • 在特权模式下运行Docker(或不以特权模式运行).
  • 我使用旧版本(SAM 0.22)遇到相同的错误 Docker映像.
  • 如下所述在本地运行相同的功能(可行).
  • 压缩文件夹并在AWS上运行(有效).

这些解决方案可能不适用:

These solutions probably don't apply:

  • zip文件的格式不正确,包括父文件夹(不使用zip文件).
  • 与NPM依赖项或node_modules有关的问题(index.js没有依赖项).
  • index.js中的编译错误(语法正确,可在Docker外部和AWS上使用.
  • 此处所述,将卷安装在主机上
  • The format of the zip file incorrectly including the parent folder (no zip file is used).
  • Issues related to NPM dependencies or node_modules (index.js has no dependencies).
  • Compilation errors in index.js (syntax is correct, works outside of Docker and on AWS).
  • The volume is being mounted on the host as described here.

Dockerfile

Dockerfile

FROM alpine:3.6
WORKDIR /usr/src/app
RUN apk add --no-cache py-pip
RUN pip install --no-cache-dir aws-sam-cli

event.json

event.json

{}

index.js

exports.handler = function(event, context, callback) {
    return callback(null, {
        'body': 'Hello, World!'
    });
};

template.yml

template.yml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  HelloWorld:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: HelloWorld
      CodeUri: .
      Handler: index.handler
      Runtime: nodejs6.10
      Timeout: 300

要在本地运行SAM:

sam local invoke -t template.yml -e event.json HelloWorld

在本地运行SAM成功:

Running SAM locally succeeds:

{"body":"Hello, World!"}

要在Docker下运行SAM:

To run SAM under Docker:

docker build -t hello .
docker run \
    -v $(pwd):/usr/src/app \
    -v /var/run/docker.sock:/var/run/docker.sock \
    hello sam local invoke -t template.yml -e event.json HelloWorld

在Docker下运行SAM失败:

Running SAM under Docker fails:

Unable to import module 'index': Error
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)

操作系统:Ubuntu 16.04.1 x86_64

OS: Ubuntu 16.04.1 x86_64

Docker版本:18.03.1-ce

Docker version: 18.03.1-ce

SAM CLI版本:0.3.0

SAM CLI version: 0.3.0

推荐答案

您必须引用相对于主机硬盘驱动器的目录,而不是第一个Docker容器驱动器树.

You have to reference the directory relative to your host hard drive, not the first docker container drive tree.

要使用SAM cli进行此操作,请在上使用选项-docker-volume-basedir"$ PWD" (或 -v"$ PWD" )sam本地调用.

To do so with SAM cli, use option --docker-volume-basedir "$PWD" (or -v "$PWD") on sam local invoke.

发件人: sam local invoke --help

-docker-volume-basedir值 -v值可选.指定SAM文件所在的基于位置的位置.如果Docker在远程计算机上运行,​​则必须在Docker计算机上挂载SAM文件的路径,然后修改此值以匹配远程计算机.[ $ SAM_DOCKER_VOLUME_BASEDIR ]

--docker-volume-basedir value, -v value Optional. Specifies the location basedir where the SAM file exists. If the Docker is running on a remote machine, you must mount the path where the SAM file exists on the docker machine and modify this value to match the remote machine. [$SAM_DOCKER_VOLUME_BASEDIR]

因此,通过上述设置,可以在docker下运行SAM:

So with your above setup, to run SAM under docker:

docker build -t hello .
docker run \
    -v /var/run/docker.sock:/var/run/docker.sock \
    hello sam local invoke -t template.yml -e event.json HelloWorld -v $(pwd)

这篇关于无法在Docker下运行AWS SAM CLI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆