Reputation: 5073
HTTPConnectionPool(host='localhost', port=5000): Max retries exceeded with url: /xyz/123/ (Caused by NewConnectionError(': Failed to establish a new connection: [Errno -2] Name or service not known',))
I've looked through a lot of stack overflow questions containing this error but none that match my exact issue.
I get the above error while sending a post request from one docker container to another docker container. My first container is running on port 8000 and the second on port 5000. Also my first container is running via docker-compose and the second by docker run and doesn't use a docker-compose file. What is causing this error and how would you go about fixing it? Thanks in advance.
EDIT: I've changed the post url to be the name of my docker container IE. http://festive_hugle/xyz/123/
Upvotes: 1
Views: 8515
Reputation: 5073
I figured it out. I had to change the following:
docker run -d --net network_name img_name
docker inspect container_id
http://172.20.0.5:5000/xyz/123/
Doing this allowed the first container to send a post request to the second.
Upvotes: 1
Reputation: 331
by default, docker-compose create a dedicated network for the services that you are running. When you run the docker instance using docker run
and you do not specify in which network should be run, it will run on the default
network.
This could be the problem and you can solve it by running the docker instance that do not use compose on the docker-compose network.
To obtain the name of the network you can run
docker inspect <compose_container_id> | grep -i "NetworkMode"
Once you get the name of the network, you can run the container, connect to the network and link with the services of the compose instances.
docker run -it --network <network_name> --link <compose_container_name> <docker_image>
On the other hand, I don't know why the error that you pasted is showing that you are trying to connect to localhost.
HTTPConnectionPool(host='localhost', port=5000)
In case that you want to connect to a host mapped port, you have to specify the host ip that is connected to the container network.
I hope you find this useful.
Upvotes: 2