Gavello Gavello - 3 months ago 54
HTTP Question

docker http request between containers

I'm at the first stage in learning how to use Docker so I'm trying basic things
I've created two nodejs express services that need to exchange data via http requests

using docker-compose yml file

networks:
isolation-network:
driver: bridge

services:
service1-nodejs:
build:
context: ./service1/
dockerfile: .docker/node.dockerfile
ports:
- "10000:9000"
- "10001:5858"
env_file: ./service1/.docker/env/app.${APP_ENV}.env
networks:
- isolation-network

service2-nodejs:
build:
context: ./service2/
dockerfile: .docker/node.dockerfile
ports:
- "10010:9000"
- "10011:5858"
env_file: ./service2/.docker/env/app.${APP_ENV}.env
networks:
- isolation-network


service1 use request library making a POST request to service 2

request({ url: "http//service2:10010/api/",
method: "POST",
headers: { "Content-Type": "application/json" },
json: true,
body: {...},
time: true
}, function (err, res, body) {
if (!err && res.statusCode == 200) {
// - success
}
// - failed
});


the result of this call is

{ Error: connect ECONNREFUSED 172.18.0.3:10010}


using postman I can test service2 at localhost:10010/api/ and I can confirm they actually can be reached and work as expected

I'm missing something but can't figure it out?

Answer

See the document. The port 10010 is a host port but not container port. You should use 9000 when you access service2 container directly.

So just change "http//service2:10010/api/" to "http//service2:9000/api/" and it will work.

Comments