从Docker UI容器调用API容器

huangapple go评论80阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年1月4日 01:49:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/59583086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定