在容器内连接到Amazon的DynamoDB [英] Connecting to Amazon's DynamoDB inside a container
问题描述
我正在尝试使用 node 和 dynamoDB 设置本地环境,但是我无法在我的应用容器内连接到DynamoDB
I'm tryng to setup a local environment with node and dynamoDB, but I can't connect inside my app container to DynamoDB container.
它与nodemon一起使用,但不适用于2个容器。
It works with nodemon but not with 2 containers.
我暴露了所需的端口,并添加了所需的环境变量,但我仍然遇到此错误:
I exposed needed ports and I add needed environment variables but I keep having this error :
Error: connect ECONNREFUSED 127.0.0.1:8000
at TCPConnectWrap.afterConnec
$ b中连接ECONNREFUSED 127.0.0.1:8000
$ b
这是我的 Docker
文件:
Here is my Docker
file :
FROM node:12-alpine
WORKDIR /usr/src/app
ENV AWS_ACCESS_KEY_ID=24534
ENV AWS_SECRET_ACCESS_KEY=33535
COPY package*.json ./
RUN npm install
COPY . .
CMD [ "npm", "start" ]
EXPOSE 3000
这是我的 docker-compose.yml
文件:
version: "3"
services:
app:
build: .
ports:
- 3000:3000
- 8001:8001
volumes:
- ~/.aws:/root/.aws
- .env:/usr/src/app/.env
dynamodb:
image: amazon/dynamodb-local
ports:
- 8000:8000
我的节点应用程序已启动(我的测试端点正常工作),但是它不起作用,谢谢您的帮助。
My node app is up (my test endpoint works) but it doesn't work, thanks for helping.
有关信息,这是我如何在节点应用程序中连接到 aws-sdk
:
For info here is how I'm connecting to aws-sdk
in my node app :
const AWS = require('aws-sdk');
AWS.config.update({
region: process.env.AWSRegion,
endpoint: process.env.AWSEndpoint
});
AWSRegion=eu-west-3
AWSEndpoint="http://localhost:8000"
推荐答案
所以我终于实现了在我的应用容器内访问dynamoDB容器。
So I finally achieved to access my dynamoDB container inside my app container.
localhost
指的是容器,因此我必须在 .env
文件:
localhost
inside the app container refers to the container so I had to change in in my .env
file :
- 来自
AWSEndpoint = http:// localhost:8000
- 到
AWSEndpoint = http:// dynamodb:8000
- from
AWSEndpoint=http://localhost:8000
- to
AWSEndpoint=http://dynamodb:8000
默认情况下使用 docker-compose.yml
文件,无需定义容器之间的链接
,因为默认情况下它们在同一网络 bridge 上是 ,因此这些更改使其可以使用。
By default with a docker-compose.yml
file there is no need to define a links
between containers because they are by default on the same network : bridge, so these change make it works.
这篇关于在容器内连接到Amazon的DynamoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!