Connecting to Amazon's DynamoDB inside a container

旧城冷巷雨未停 提交于 2019-12-11 06:48:20

问题


I'm tryng to setup a local environment with node and dynamoDB, but I can't connect inside my app container to DynamoDB container.

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

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

And here my docker-compose.yml file :

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.

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"

回答1:


So I finally achieved to access my dynamoDB container inside my app container.

localhost inside the app container refers to the container so I had to change in in my .env file :

  • from AWSEndpoint=http://localhost:8000
  • to AWSEndpoint=http://dynamodb:8000

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.



来源:https://stackoverflow.com/questions/58917144/connecting-to-amazons-dynamodb-inside-a-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!