英文:
Call from docker ui container to api container
问题
这是我的docker-compose文件
version: "3.3"
services:
api:
image: api
container_name: api
ports:
- "3000:3000"
external_links:
- front
networks:
- global
front:
image: front
container_name: front
ports:
- "8080:80"
external_links:
- api
networks:
- global
networks:
global:
我正在尝试从UI容器中调用API。
这是调用的URL:http://api:3000/api/search?query=ihpone&page=1&minPrice=0&maxPrice=9999
我收到了ERR_NAME_NOT_RESOLVED错误。
有人可以帮助我吗?如何解决我的问题?如何从UI容器向API容器发出请求?
英文:
Here is my docker-compose
services:
api:
image: api
container_name: api
ports:
- "3000:3000"
external_links:
- front
networks:
- global
front:
image: front
container_name: front
ports:
- "8080:80"
external_links:
- api
networks:
- global
networks:
global:
I'm trying to call api from ui container.
Here is the call url: http://api:3000/api/search?query=ihpone&page=1&minPrice=0&maxPrice=9999
I'm getting error ERR_NAME_NOT_RESOLVED
Someone can help me ? How to solve my issue. How make request from ui container to api container?
答案1
得分: 4
当您在浏览器中打开您的UI应用程序时,实际上是将其下载到主机(安装了Docker的计算机)上,然后发出API调用。
因此,在您的示例中,您需要将UI应用程序的配置中的URL设置为:localhost:3000,而不是api:3000。
- localhost:3000 - 用于从主机中的Docker容器中访问应用程序。
- api:3000 - 用于从另一个Docker容器中的Docker容器中访问应用程序。
英文:
When you're opening your UI application on browser you're actually downloading it to host (your machine where docker is installed) and then you're making a call to API.
So in your example you need to set url in your UI application's configuration to be: localhost:3000 instead of api:3000
- localhost:3000 - is to hit an app in a docker container from a host.
- api:3000 - is to hit an app in a docker container from another docker container.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论