带有Docker的Spring Boot + DynamoDB Local:连接到localhost:8000失败 [英] Spring Boot + DynamoDB Local with Docker: Connect to localhost:8000 failed

查看:304
本文介绍了带有Docker的Spring Boot + DynamoDB Local:连接到localhost:8000失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个查询DynamoDB的Spring Boot REST API,我想配置一个或两个作为微服务运行的Docker容器:一个在本地运行DynamoDB,另一个运行REST API。我为Spring Boot REST API配置了一个容器,但是当我对该API进行调用时,即使DynamoDB在localhost:8000上运行,它也无法查询本地DynamoDB。我得到的错误是:

I built a Spring Boot REST API that queries DynamoDB, and I would like to configure one or two Docker containers that run as microservices: one that runs DynamoDB locally and another that runs the REST API. I have a container configured for the Spring Boot REST API, but when I make calls to the API, it cannot query the local DynamoDB even though DynamoDB is running on localhost:8000. The error I get is:

无法执行HTTP请求:连接到localhost:8000 [localhost / 127.0.0.1]失败:连接被拒绝(拒绝连接)

尽管收到该消息,我仍可以使用浏览器通过本地主机访问DynamoDB。好像Docker容器即使API容器正在localhost:8080上运行,也不允许API访问localhost:8000。

Despite getting that message, I can access DynamoDB via localhost using my browser. It's as if the Docker container will not allow the API to access localhost:8000 even though the API container is running at localhost:8080.

我应该如何配置我的容器( s),以便每个都作为独立的微服务运行?有人曾经见过类似的问题吗?

How should I configure my container(s) so that each runs as a separate microservice? Has anyone seen a similar issue before?

推荐答案

localhost 并不指向真实机器主机。从字面上看,当您在任何Docker容器中使用 localhost 时,它将指向容器本身。

localhost inside a container doesn't point to real machine host. Literally when you use localhost inside any docker container it will be pointing to the container itself.

Docker提供了两种处理这种情况的方法:

Docker provides two ways of handling this sitution:

链接。不推荐使用的方法,在新项目中应避免

Links. This is deprecated way and should be avoided in new projects.

网络。 Docker可以建立一个网络,您可以在其中通过DNS名称(容器ID或容器名称)访问容器。

Networks. Docker can establish a network where you can access your containers by DNS names (container ID, or container name).

方式创建网络中所述,将 localhost 替换为数据库容器的名称,即可开始使用。

Create a network in a way described here, replace localhost by the name of DB container and you'll be ready to go.

这篇关于带有Docker的Spring Boot + DynamoDB Local:连接到localhost:8000失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆