UnknownEndpoint:无法访问的主机:“localhost”。尝试使用 express 和 dynamodb local 连接到 localhost
Posted
技术标签:
【中文标题】UnknownEndpoint:无法访问的主机:“localhost”。尝试使用 express 和 dynamodb local 连接到 localhost【英文标题】:UnknownEndpoint: Inaccessible host: `localhost'. When trying to connect to localhost with express and dynamodb local 【发布时间】:2021-05-09 18:25:49 【问题描述】:我正在使用 docker compose 创建两个容器,一个用于本地 dynamodb,另一个用于 nodejs express 应用程序。
docker-compose.dev.yml
version: '3'
services:
dynamodb-local:
command: "-jar DynamoDBLocal.jar -sharedDb -optimizeDbBeforeStartup -dbPath ./data"
image: "amazon/dynamodb-local:latest"
container_name: dynamodb-local
ports:
- "8000:8000"
volumes:
- "./docker/dynamodb:/home/dynamodblocal/data"
working_dir: /home/dynamodblocal
networks:
- dynamodb-network
node-app:
depends_on:
- dynamodb-local
container_name: dev-nodejs-backend
ports:
- '3000:3000'
volumes:
- .:/usr/src/node-app
environment:
AWS_ACCESS_KEY_ID: 'DUMMYIDEXAMPLE'
AWS_SECRET_ACCESS_KEY: 'DUMMYEXAMPLEKEY'
networks:
- dynamodb-network
command: yarn dev -L
networks:
dynamodb-network:
driver: bridge
Dockerfile
FROM node:alpine
RUN mkdir -p /usr/src/node-app && chown -R node:node /usr/src/node-app
WORKDIR /usr/src/node-app
COPY package.json yarn.lock ./
USER node
RUN yarn install --pure-lockfile
COPY --chown=node:node . .
EXPOSE 3000
model.js
var AWS = require('aws-sdk');
AWS.config.update(
accessKeyId: 'YOUR-ACCESSKEYID' ,
secretAccessKey: 'YOUR-SECRETACCESSKEY' ,
region: "localhost",
endpoint: 'http://localhost:8000',
);
const DynamoDB = new AWS.DynamoDB();
var docClient = new AWS.DynamoDB.DocumentClient();
var table = 'URL';
const createTable = async () =>
var params =
TableName : table,
KeySchema: [
AttributeName: "hash", KeyType: "HASH", //Partition key
],
AttributeDefinitions: [
AttributeName: "hash", AttributeType: "S" ,
],
ProvisionedThroughput:
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
;
const result = await DynamoDB.createTable(params).promise();
return result;
控制台报错
UnknownEndpoint: Inaccessible host: `localhost'. This service may not be available in the `localhost' region.
at Request.ENOTFOUND_ERROR (/usr/src/node-app/node_modules/aws-sdk/lib/event_listeners.js:507:46)
at Request.callListeners (/usr/src/node-app/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
at Request.emit (/usr/src/node-app/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
at Request.emit (/usr/src/node-app/node_modules/aws-sdk/lib/request.js:688:14)
at ClientRequest.error (/usr/src/node-app/node_modules/aws-sdk/lib/event_listeners.js:339:22)
at ClientRequest.<anonymous> (/usr/src/node-app/node_modules/aws-sdk/lib/http/node.js:96:19)
at ClientRequest.emit (node:events:379:20)
at ClientRequest.EventEmitter.emit (node:domain:470:12)
at Socket.socketErrorListener (node:_http_client:462:9)
at Socket.emit (node:events:379:20)
at Socket.EventEmitter.emit (node:domain:470:12)
at emitErrorNT (node:internal/streams/destroy:188:8)
at emitErrorCloseNT (node:internal/streams/destroy:153:3)
at processTicksAndRejections (node:internal/process/task_queues:81:21)
code: 'UnknownEndpoint',
region: 'localhost',
hostname: 'localhost',
retryable: true,
originalError: Error: connect ECONNREFUSED 127.0.0.1:8000
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1139:16)
errno: -111,
code: 'NetworkingError',
syscall: 'connect',
address: '127.0.0.1',
port: 8000,
region: 'localhost',
hostname: 'localhost',
retryable: true,
time: 2021-02-05T14:23:15.559Z
,
time: 2021-02-05T14:23:15.559Z
Dynamodb local 在端口 8000 上运行,但我无法从我的 express 应用程序连接它。 我可以在撰写文件中更改什么,或者这是一些不同的错误吗?
【问题讨论】:
【参考方案1】:在将 s3 与 localstack 一起使用时,我得到了相同的错误输出。检查他们的troubleshooting page 很重要,在我的情况下,它是通过 s3ForcePathStyle 属性解决的
const s3 = new SDK.S3(
endpoint: "http://localhost:4566",
s3ForcePathStyle: true,
);
【讨论】:
【参考方案2】:我怀疑你的问题在这里:
AWS.config.update(
accessKeyId: 'YOUR-ACCESSKEYID' ,
secretAccessKey: 'YOUR-SECRETACCESSKEY' ,
region: "localhost",
endpoint: 'http://localhost:8000',
);
当您尝试从容器内部连接到localhost
时,它会尝试从该容器的角度连接到localhost
,即容器本身。
容器认为它是自己独立的机器,因此localhost
指的是容器本身。这不起作用,因为 DynamoDB 没有在您的节点容器内运行。
您希望容器连接到端口 8000 或 docker 主机或 dynamodb-local
容器。由于您使用的是桥接网络,因此后者应该很容易实现 - 将端点设置为http://dynamodb-local:8000/
。 Docker 应该为您的容器提供其网络中其他容器名称的 DNS 解析。我建议您查看docs 了解有关桥接网络的更多信息。
tl;dr:这可能会解决问题
AWS.config.update(
accessKeyId: 'YOUR-ACCESSKEYID' ,
secretAccessKey: 'YOUR-SECRETACCESSKEY' ,
region: "localhost",
endpoint: 'http://dynamodb-local:8000',
);
【讨论】:
我还必须添加创建数据库文件所需的权限。有关更多信息,请参阅此问题:***.com/a/53132375/9858016以上是关于UnknownEndpoint:无法访问的主机:“localhost”。尝试使用 express 和 dynamodb local 连接到 localhost的主要内容,如果未能解决你的问题,请参考以下文章