【问题标题】:How to create DynamoDB table using docker CMD instruction and the AWS CLI inside running container?如何使用 docker CMD 指令和运行容器内的 AWS CLI 创建 DynamoDB 表?
【发布时间】:2022-02-10 19:34:12
【问题描述】:

您好,有一个简单的 Dockerfile,它必须创建 DynamoDB 表:

FROM amazon/aws-cli AS seed                                                                                                                            
                                                                                                                                                       
CMD \                                                                                                                                                  
aws dynamodb --endpoint-url http://localhost:8080 create-table \                                                                                       
    --table-name mytable \                                                                                                           
    --attribute-definitions AttributeName=user_id,AttributeType=N AttributeName=order_id,AttributeType=N \
    --key-schema AttributeName=user_id,KeyType=HASH AttributeName=order_id,KeyType=RANGE \                                                             
    --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \                                                                                
    --region eu-west-2                                                                                                                                 

当我尝试运行它时,我得到以下输出:

Attaching to my-container
my-container       | 
my-container       | usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
my-container       | To see help text, you can run:
my-container       | 
my-container       |   aws help
my-container       |   aws <command> help
my-container       |   aws <command> <subcommand> help
my-container       | 
my-container       | aws: error: argument command: Invalid choice, valid choices are:
my-container       | 
my-container       | accessanalyzer                           | account                                 
my-container       | acm                                      | acm-pca                                 
my-container       | alexaforbusiness                         | amp                                     
my-container       | amplify                                  | amplifybackend                          
my-container       | amplifyuibuilder                         | apigateway                              
my-container       | apigatewaymanagementapi                  | apigatewayv2                            

// Lot of services in here
                       
my-container       | s3                                       | ddb                                     
my-container       | configure                                | deploy                                  
my-container       | configservice                            | opsworks-cm                             
my-container       | history                                  | cli-dev                                 
my-container       | help                                    
my-container       | 
my-container exited with code 252

我做错了什么?感谢您的帮助!

【问题讨论】:

    标签: amazon-web-services docker amazon-dynamodb


    【解决方案1】:

    对于此特定图像,CMD 必须采用 JSON 数组语法(一个 shell 字对应一个数组元素),并且不得包含 aws 命令。

    FROM amazon/aws-cli
    CMD ["dynamodb", "create-table", "..."]                                                                                                                                                  
    

    这是因为 the aws-cli image declares ENTRYPOINT ["aws"]、the ENTRYPOINT and CMD are combined into a single command 和 string-format CMD inserts a shell wrapper。当前表单中的主容器命令类似于aws sh -c 'aws dynamodb ...',该工具并不真正理解。

    (我可能会将此特定命令作为独立的 docker run 命令运行,而不是尝试将其打包到映像中,或者可能将 AWS 开发工具包用作集成测试设置代码的一部分。显式 localhost 端点也似乎它可能无法正常工作,因为它通常指的是仅运行 AWS CLI 的同一个容器。)

    【讨论】:

      【解决方案2】:

      在您的命令中,您必须在 \ 之后没有空格,这就是为什么您可能会收到错误,因为其余语句被忽略了。

      用这个再次尝试测试:

      aws dynamodb --endpoint-url http://localhost:8080 create-table \
      --table-name mytable \
      --attribute-definitions AttributeName=user_id,AttributeType=N AttributeName=order_id,AttributeType=N \
      --key-schema AttributeName=user_id,KeyType=HASH AttributeName=order_id,KeyType=RANGE \
      --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
      --region eu-west-2
      

      并且还尝试拥有一个虚假的个人资料,因为它需要一个访问密钥和一个秘密密钥,即使它是本地的

      【讨论】:

      • 您好@Hatim,感谢您的关注,我已经删除了前面的空格,但输出仍然相同:/
      • 好的@Bulchsu,我没有关注CMD部分,确实,正确的答案是在dockerfile中的命令部分进行拆分
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      相关资源
      最近更新 更多