英文:
not being able to create table locally when running DynamoDB local on Docker
问题
这是您的Django应用程序代码的一部分,其中出现了一些错误。您提到在设置endpoint_url
为空时,表成功创建在与IAM帐户关联的DynamoDB上。问题似乎与endpoint_url
有关。错误消息中提到连接被拒绝,这表明可能有一些问题阻止了您的应用程序连接到本地DynamoDB端点。
您可以尝试以下几种方法来解决问题:
-
确保您的DynamoDB本地实例正在运行,并且端口
localhost:8000
可用。您可以使用命令aws dynamodb list-tables --endpoint-url http://localhost:8000
来验证它是否正常运行。 -
确保您的Django应用程序容器可以访问本地DynamoDB端点。这可能涉及到Docker容器网络设置或端口映射的问题。
-
检查您的IAM凭证是否正确配置,并且环境变量
ACCESS_KEY
和SECRET_KEY
被正确设置。这些凭证应该具有足够的权限来创建DynamoDB表。 -
尝试使用其他工具(例如Python的
boto3
库)连接到本地DynamoDB并执行一些操作,以确保本地DynamoDB实际上可以正常工作。
请尝试这些方法,并查看是否能够解决连接问题。如果问题仍然存在,请提供更多详细信息,以便我能够提供更具体的建议。
英文:
I am working on a Django App
Here is my code:
def get(self, request):
dynamo_db = boto3.resource(
"dynamodb",
region_name="localhost",
aws_access_key_id=os.getenv("ACCESS_KEY"),
aws_secret_access_key=os.getenv("SECRET_KEY"),
endpoint_url="http://localhost:8000",
)
table = dynamo_db.create_table(
TableName="CustomUsers_Duplicate",
AttributeDefinitions=[
{"AttributeName": "username", "AttributeType": "S"},
],
KeySchema=[
{"AttributeName": "username", "KeyType": "HASH"},
],
ProvisionedThroughput={"ReadCapacityUnits": 1, "WriteCapacityUnits": 1},
)
return Response("Inside GET endpoint")
Every time when I hit the endpoint, I kept getting an error
botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL: "http://localhost:8000/"
But I don't see any issue when running
aws dynamodb list-tables --endpoint-url http://localhost:8000
My docker config
version: "3.8"
services:
moof-backend-app:
build: .
container_name: moof-backend-app
image: moof-backend-app:eb-cli-installed
environment:
PYTHONUNBUFFERED: 1
ports:
- 8080:8080
volumes:
- .:/moof-backend
command: python3 manage.py runserver 0.0.0.0:8080
depends_on:
- dynamodb-local
dynamodb-local:
command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data"
image: "amazon/dynamodb-local:latest"
container_name: dynamodb-local
ports:
- "8000:8000"
volumes:
- "./docker/dynamodb:/home/dynamodblocal/data"
working_dir: /home/dynamodblocal
Interestingly enough, if I erase endpoint_url
, the table was successfully created on the DynamoDB of the associated IAM account.
What is the problem here?
Edited:
The error occurs at the very top looks like this,
Traceback (most recent call last):
moof-backend-app | File "/usr/local/lib/python3.10/site-packages/urllib3/connection.py", line 174, in _new_conn
moof-backend-app | conn = connection.create_connection(
moof-backend-app | File "/usr/local/lib/python3.10/site-packages/urllib3/util/connection.py", line 95, in create_connection
moof-backend-app | raise err
moof-backend-app | File "/usr/local/lib/python3.10/site-packages/urllib3/util/connection.py", line 85, in create_connection
moof-backend-app | sock.connect(sa)
moof-backend-app | ConnectionRefusedError: [Errno 111] Connection refused
答案1
得分: 1
您的DynamoDB容器名称是dynamodb-local
,因此服务容器中的连接URL应为http://dynamodb-local:8000
。这在https://docs.docker.com/compose/networking中有解释:
> 默认情况下,Compose为您的应用程序设置一个网络。每个服务的容器都加入默认网络,可以被该网络上的其他容器访问,并且它们可以通过与容器名称相同的主机名发现它们。
针对您的评论:
> 另外,如果我将其更改为http://dynamodb-local:8000,我应该运行什么CLI来查看表的列表?
如果您在Docker之外的主机上运行CLI,请使用aws dynamodb list-tables --endpoint-url http://localhost:8000
。
英文:
Your DynamoDB container name is dynamodb-local
, so the connection URL from the service container should be http://dynamodb-local:8000
. This is explained in https://docs.docker.com/compose/networking:
> By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
In response to your comment:
> also, if i change that to http://dynamodb-local:8000 , what CLI should i run to see the list of tables?
If you are running the CLI on your host machine, outside of Docker, use aws dynamodb list-tables --endpoint-url http://localhost:8000
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论