title: Can't run AWS SAM CLI under Docker
body: <p>I'm trying to create a Docker image for <a href="https://github.com/awslabs/aws-sam-cli" rel="nofollow noreferrer">AWS SAM CLI</a>, 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 <a href="https://github.com/dc7/docker-sam-local" rel="nofollow noreferrer">here</a> or see the files below.</p>
  
  <p>I already tried the following:</p>
  
  <ul>
  <li>Setting permissions of files and parent folder to 777 (or 755).</li>
  <li>Disabling SELinux in the Docker daemon (or enabling it).</li>
  <li>Running Docker under privileged mode (or not).</li>
  <li>I get the same error using an old (SAM 0.22) <a href="https://hub.docker.com/r/cnadiminti/aws-sam-local/" rel="nofollow noreferrer">Docker image</a>.</li>
  <li>Running the same function locally as described below (works).</li>
  <li>Zipping the folder and running on AWS (works).</li>
  </ul>
  
  <p>These solutions probably don't apply:</p>
  
  <ul>
  <li>The format of the zip file incorrectly including the parent folder (no zip file is used).</li>
  <li>Issues related to NPM dependencies or node_modules (index.js has no dependencies).</li>
  <li>Compilation errors in index.js (syntax is correct, works outside of Docker and on AWS).</li>
  <li>The volume is being mounted on the host as described <a href="https://github.com/awslabs/aws-sam-cli#remote-docker" rel="nofollow noreferrer">here</a>.</li>
  </ul>
  
  <p>Dockerfile</p>
  
  <pre><code>FROM alpine:3.6
  WORKDIR /usr/src/app
  RUN apk add --no-cache py-pip
  RUN pip install --no-cache-dir aws-sam-cli
  </code></pre>
  
  <p>event.json</p>
  
  <pre><code>{}
  </code></pre>
  
  <p>index.js</p>
  
  <pre><code>exports.handler = function(event, context, callback) {
      return callback(null, {
          'body': 'Hello, World!'
      });
  };
  </code></pre>
  
  <p>template.yml</p>
  
  <pre><code>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
  </code></pre>
  
  <p>To run SAM locally:</p>
  
  <pre><code>sam local invoke -t template.yml -e event.json HelloWorld
  </code></pre>
  
  <p>Running SAM locally succeeds:</p>
  
  <pre><code>{"body":"Hello, World!"}
  </code></pre>
  
  <p>To run SAM under Docker:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Running SAM under Docker fails:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>OS: Ubuntu 16.04.1 x86_64</p>
  
  <p>Docker version: 18.03.1-ce</p>
  
  <p>SAM CLI version: 0.3.0</p>