在本地堆栈启动时创建DynamoDB表。

huangapple go评论58阅读模式
英文:

Creating DynamodDB table on start of the localstack

问题

使用Localstack与DynamoDB,并需要在启动时默认创建一些表格。然而,我遇到了一个奇怪的错误:

/docker-entrypoint-initaws.d/init.sh: is a directory

docker-compose配置:

localstack:
    container_name: localstack
    image: localstack/localstack:latest
    network_mode: bridge
    ports:
      - "4566:4566"
      - "8010:8010"
    environment:
      - EDGE_PORT=8010
      - SERVICES=s3,dynamodb
      - DEBUG=1
      - DATA_DIR=/tmp/localstack/data
    volumes:
      - "$PWD/.localstack/init.sh:/docker-entrypoint-initaws.d/init.sh"

init.sh 文件如下:

awslocal dynamodb create-table \
   --table-name table-global \
   --attribute-definitions AttributeName=id,AttributeType=S \
   --key-schema AttributeName=id,KeyType=HASH \
   --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

我的环境:

  • 操作系统:MacOS Ventura
  • Docker 版本:v4.16.2

我尝试了多种选项,但仍然无法解决问题。我不确定我可能做错了什么。

英文:

Using Localstack with Dynamodb and need to create some tables by default on the start. However, I am getting the strange error that:

/docker-entrypoint-initaws.d/init.sh: is a directory

docker-compose config:

localstack:
    container_name: localstack
    image: localstack/localstack:latest
    network_mode: bridge
    ports:
      - "4566:4566"
      - "8010:8010"
    environment:
      - EDGE_PORT=8010
      - SERVICES=s3,dynamodb
      - DEBUG=1
      - DATA_DIR=/tmp/localstack/data
    volumes:
      - "$PWD/.localstack/init.sh:/docker-entrypoint-initaws.d/init.sh"

and the init.sh looks like:

awslocal dynamodb create-table \
   --table-name table-global \
   --attribute-definitions AttributeName=id,AttributeType=S \
   --key-schema AttributeName=id,KeyType=HASH \
   --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

My stack:

  • OS: MacOS Ventura
  • Docker: v4.16.2

I have tried multiple options but it still does not work. I am not sure what I might be doing wrong.

答案1

得分: 2

我通过更改您的 docker-compose.yml 文件来修复了您的问题:

version: "3.8"

services:
  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
    image: localstack/localstack
    ports:
      - "127.0.0.1:4566:4566"
    environment:
      - DEBUG=1
      - DOCKER_HOST=unix:///var/run/docker.sock
    volumes:
      - "$PWD/init-aws.sh:/etc/localstack/init/ready.d/init-aws.sh"  # ready hook
      - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

init-aws.sh 文件如下:

#!/bin/bash
awslocal dynamodb create-table \
   --table-name table-global \
   --attribute-definitions AttributeName=id,AttributeType=S \
   --key-schema AttributeName=id,KeyType=HASH \
   --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

请确保首先对文件运行 chmod +x init-aws.sh

您可以通过以下命令验证此操作:

$ awslocal dynamodb list-tables
{
    "TableNames": [
        "table-global"
    ]
}
英文:

I fixed your issue by changing your docker-compose.yml to:

version: "3.8"

services:
  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
    image: localstack/localstack
    ports:
      - "127.0.0.1:4566:4566"
    environment:
      - DEBUG=1
      - DOCKER_HOST=unix:///var/run/docker.sock
    volumes:
      - "$PWD/init-aws.sh:/etc/localstack/init/ready.d/init-aws.sh"  # ready hook
      - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

The init-aws.sh looks like:

#!/bin/bash
awslocal dynamodb create-table \
   --table-name table-global \
   --attribute-definitions AttributeName=id,AttributeType=S \
   --key-schema AttributeName=id,KeyType=HASH \
   --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

Make sure to run chmod +x init-aws.sh on the file first.

I can validate this via the following command:

$ awslocal dynamodb list-tables
{
    "TableNames": [
        "table-global"
    ]
}

huangapple
  • 本文由 发表于 2023年3月7日 08:09:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定