Can't run AWS SAM CLI under Docker

☆樱花仙子☆ 提交于 2021-02-06 13:51:29

问题


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:

  • Setting permissions of files and parent folder to 777 (or 755).
  • Disabling SELinux in the Docker daemon (or enabling it).
  • Running Docker under privileged mode (or not).
  • I get the same error using an old (SAM 0.22) Docker image.
  • Running the same function locally as described below (works).
  • Zipping the folder and running on AWS (works).

These solutions probably don't apply:

  • 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

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

{}

index.js

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

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

To run SAM locally:

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

Running SAM locally succeeds:

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

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

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)

OS: Ubuntu 16.04.1 x86_64

Docker version: 18.03.1-ce

SAM CLI version: 0.3.0


回答1:


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

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

From: sam local invoke --help

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

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)


来源:https://stackoverflow.com/questions/50632405/cant-run-aws-sam-cli-under-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!