简体   繁体   English

Docker 容器无法从配置文件到达另一个容器 url

[英]Docker container not able to reach another container url from config file

I'm very new to docker concepts, i have few node microservices, i have created docker images for them using Dockerfile, i have run these images on docker containers using docker run command, now i'm testing these node apis using postman. one of the microservice named inventory has another microservice named sopra's url in its config file to run it properly.我对 docker 概念很陌生,我几乎没有节点微服务,我已经使用 Dockerfile 为它们创建了 docker 个图像,我已经使用 docker 运行命令在 docker 容器上运行了这些图像,现在我正在使用 81841348 测试这些节点 apis名为 inventory 的微服务的配置文件中有另一个名为 sopra's url 的微服务可以正常运行。 when i'm trying to test in postman it's giving status: 500 internal server error.当我尝试在 postman 中进行测试时,它显示状态:500 内部服务器错误。 but when it is running locally using npm start then postman is giving message success.但是当它使用 npm start 在本地运行时,postman 会给出消息成功。 i have created a seperate docker.network named test and created these 2 containers in that test.network.我创建了一个名为 test 的单独 docker.network,并在该 test.network 中创建了这两个容器。

to create a new.network i have used docker.network create test then to run those containers in that.network i have used docker run -dp 1050:5006 --name inventory-container -.net=test inventory-image & docker run -dp 925:5008 --name sopra-container -.net=test sopra-image要创建一个新的网络,我使用了docker.network create test然后在那个网络中运行这些容器,我使用docker run -dp 1050:5006 --name inventory-container -.net=test inventory-image & docker run -dp 925:5008 --name sopra-container -.net=test sopra-image

and i even checked if they both can communicate with each other using ping command我什至检查了他们是否可以使用 ping 命令相互通信

to get the ip address of the container i have used docker inspect -f '{{.NetworkSettings.Networks..network].IPAddress}}' inventory-container and i got ip as 172.21.0.2获取容器的 ip 地址我使用docker inspect -f '{{.NetworkSettings.Networks..network].IPAddress}}' inventory-container并且我得到了 ip 作为 172.21.0.2

next i used docker inspect -f '{{.NetworkSettings.Networks.test.IPAddress}}' sopra-container and i got ip as 172.21.0.3接下来我使用docker inspect -f '{{.NetworkSettings.Networks.test.IPAddress}}' sopra-container我得到 ip 作为 172.21.0.3

now to check the ping i have used docker exec inventory-container ping 172.21.0.3 -c2 & docker exec sopra-container ping 172.21.0.2 -c2 and both are returning 2 packs transmitted, 0% packet loss现在检查我使用的 ping docker exec inventory-container ping 172.21.0.3 -c2 & docker exec sopra-container ping 172.21.0.2 -c2并且都返回 2 个包传输,0% 数据包丢失

sopra container is working fine as it is giving success message and oauth token is generated and inventory container is also working fine as i have tested it's api when it doesn't require sopra token it also givng success message but only when need to connect to sopra it's giving error as internal server error. sopra 容器工作正常,因为它提供成功消息并生成 oauth 令牌并且库存容器也工作正常,因为我已经测试它是 api 当它不需要 sopra 令牌时它也提供成功消息但仅当需要连接到 sopra它给出错误作为内部服务器错误。

this is my config.env file inside inventory-image这是我在 inventory-image 中的 config.env 文件

SOPRA_SERVICE_BASE_URL=http://localhost:925/api/v1
ENVIRONMENT_NAME='Dev'
DATE_FORMAT_TIME_ZONE='America/Indiana/Indianapolis'

A container has its own localhost so connecting to localhost would try to connect to a service running within the container and as you don't have the sopra service running within the container on localhost you can't connect to it.容器有自己的localhost ,因此连接到localhost会尝试连接到在容器内运行的服务,并且由于您没有在localhost上的容器内运行的sopra服务,因此您无法连接到它。

Also don't use hardcoded IPs.也不要使用硬编码的 IP。 Docker supports service discovery in user-defined.network bridges such as your test .network. Docker 支持在用户定义的网络桥中进行服务发现,例如您的test网络。 You can just use the container name instead of the IP address to connect to the container.您可以只使用容器名称而不是 IP 地址来连接到容器。

Here an excerpt from the Docker.networking tutorial :这是Docker.networking 教程的摘录:

On user-defined.networks like alpine.net, containers can not only communicate by IP address, but can also resolve a container name to an IP address.在 alpine.net 等用户自定义网络上,容器不仅可以通过 IP 地址进行通信,还可以将容器名称解析为 IP 地址。 This capability is called automatic service discovery.此功能称为自动服务发现。

You also need to be careful with port mappings.您还需要注意端口映射。

docker run -p <host-port>:<container-port> <some-image> 

The first port is the port on the host , the second port the one on the container.第一个端口是host上的端口,第二个端口是容器上的端口。 So if you want to connect from the outside to one of your services eg using Postman use the first port.因此,如果您想从外部连接到您的一项服务,例如使用 Postman,请使用第一个端口。 If you want to connect to the service from within your test .network, use the container port.如果要从test .network 中连接到服务,请使用容器端口。

Applying all three, change SOPRA_SERVICE_BASE_URL in config.env and it should work:应用所有三个,更改config.env中的SOPRA_SERVICE_BASE_URL ,它应该可以工作:

SOPRA_SERVICE_BASE_URL=http://sopra-container:5008/api/v1

For an easier setup of your microservices (especially if there are more than two) I would recommend docker-compose .为了更轻松地设置您的微服务(特别是如果有两个以上)我会推荐docker-compose

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM