Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker HTTP-requests between containers

I'm at the first stage in learning how to use Docker so I'm trying basic things. I've created two Node Express services that need to exchange data via HTTP-requests.

My 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 uses the request module to make 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 http://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. What is going wrong here?

like image 523
Gavello Avatar asked Aug 19 '16 19:08

Gavello


People also ask

Can 2 Docker containers talk to each other?

If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.

Why is it difficult for Docker containers to communicate with each other?

Containers can only communicate with each other if they share a network. Containers that don't share a network cannot communicate with one another. That's one of the isolation features provided by Docker. A container can belong to more than one network, and a network can have multiple containers inside.

Does Docker use port 8080?

Docker also finds ports you expose with --expose 8080 (assuming you want to expose port 8080). Docker maps all of these ports to a host port within a given epehmeral port range . You can find the configuration for these ports (usually 32768 to 61000) in /proc/sys/net/ipv4/ip_local_port_range .


1 Answers

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.

like image 98
Philip Tzou Avatar answered Oct 11 '22 12:10

Philip Tzou