Home > Mobile >  Docker container not able to reach another container url from config file
Docker container not able to reach another container url from config file

Time:04-21

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. when i'm trying to test in postman it's giving status: 500 internal server error. but when it is running locally using npm start then postman is giving message success. i have created a seperate docker network named test and created these 2 containers in that 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

and i even checked if they both can communicate with each other using ping command

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

next i used docker inspect -f '{{.NetworkSettings.Networks.test.IPAddress}}' sopra-container and i got ip as 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

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.

this is my config.env file inside inventory-image

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

CodePudding user response:

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.

Also don't use hardcoded IPs. Docker supports service discovery in user-defined network bridges such as your test network. You can just use the container name instead of the IP address to connect to the container.

Here an excerpt from the Docker networking tutorial:

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. 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. So if you want to connect from the outside to one of your services e.g. using Postman use the first port. If you want to connect to the service from within your test network, use the container port.

Applying all three, change SOPRA_SERVICE_BASE_URL in config.env and it should work:

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.

  • Related