如何使用dockerfile启动AWS SAM

时间:2019-04-16 05:21:07

标签: node.js amazon-web-services docker amazon-dynamodb

我正在尝试构建两个受Containerizing Serverless APIs启发的docker容器。我想在Dockerfile(针对API)和localstack(针对docker-compose.yml`指定)中为本地DynamoDB指定的两个不同容器中本地运行API和DynamoDB。这些文件在下面共享。

为我的API构建容器后退出,如以下日志所示。

Successfully built e0798a6833ce
Successfully tagged hafiz/hafiz_aws_api_gateway:latest
Starting dynamodb_localstack ... done
Recreating hafiz_aws_api_gateway ... done
Attaching to dynamodb_localstack, hafiz_aws_api_gateway
dynamodb_localstack | Starting mock DynamoDB (http port 8000)...
dynamodb_localstack | Ready.
dynamodb_localstack | (. .venv/bin/activate; exec bin/localstack start)
dynamodb_localstack | Starting local dev environment. CTRL-C to quit.
dynamodb_localstack | Starting mock DynamoDB (http port 8000)...
dynamodb_localstack | Ready.
dynamodb_localstack | (. .venv/bin/activate; exec bin/localstack start)
dynamodb_localstack | Starting local dev environment. CTRL-C to quit.
dynamodb_localstack | Starting mock DynamoDB (http port 8000)...
dynamodb_localstack | Ready.
dynamodb_localstack | 2019-04-16 05:01:01,975 INFO Set uid to user 0 succeeded
dynamodb_localstack | 2019-04-16 05:01:01,977 INFO supervisord started with pid 12
hafiz_aws_api_gateway | sam_local_start_api.sh: 1: sam_local_start_api.sh: sam: not found
hafiz_aws_api_gateway exited with code 127

这是我的docker-compose.yml

version: '3'
# builds the api container and sets up the localstack container to run
services:
  api:
    build: .
    image: hafiz/hafiz_aws_api_gateway
    depends_on:
      - localstack
    ports:
      - '3000:3000'
    container_name: hafiz_aws_api_gateway
    # these are the environment variables that are used in the api
    environment:
      AWS_ENDPOINT: 'http://dynamodb_localstack:8000' # localstack container host for dynamodb
      AWS_REGION: 'localhost'
      AWS_ACCESS_KEY_ID: 'fake-access-key-id'
      AWS_SECRET_ACCESS_KEY: 'fake-secret-key'
      AWS_TABLE_NAME: 'hafizItemTable'

  localstack:
    image: localstack/localstack:latest
    ports:
      - '8000:8000' # using port 8000 to be consistent with dynamodb local jar
      - '8080:8080' # the localstack admin portal
    container_name: dynamodb_localstack
    environment:
      SERVICES: dynamodb:8000
      DATA_DIR: '/tmp/localstack/data'

这是我的dockerfile

# use this dockerfile to build an image for this api

FROM node:8.10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 3000

CMD ["sh", "sam_local_start-api.sh"

我的package.json

...
"scripts": {
    "seed": "node ./seed/runner.js",
    "start": "sam local start-api",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
...

我的问题是如何在我的docker文件中指定CMD命令,该命令将触发AWS SAM CLI命令:

sam local start-api

或者是否有更好的启动脚本的方法?

1 个答案:

答案 0 :(得分:1)

CMD ["sh", "sam_local_start.sh"]

更改为:

CMD ["npm","run","start"]

它将在sam local start-api定义的容器终端上触发运行命令:package.json

确保首先在容器中安装了sam工具。