Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection between docker containers

  1. I have a docker-compose.yml where I create 2 containers: A and B.
  2. B is a web server running on port 8000 and in my docker-compose file I expose the port 8000
  3. A has a link to B in my docker-compose-file.

But when I make a GET request on B, I got an error :

GET http://localhost:8000 dial tcp 127.0.0.1:8000 refused

How can I fix that?

docker-compose ps just tells me the port which is 8000/tcp

like image 847
sylver-john Imhoff Avatar asked Mar 26 '26 20:03

sylver-john Imhoff


1 Answers

Can you show us your docker-compose.yml file? The name used for the link between two containers actually creates an alias to connect to, just like localhost is an alias for 127.0.0.1: https://docs.docker.com/compose/yml/#links

So you shouldn't be connecting via localhost in your GET request. Instead, use the name that is used in the link to service B, with the port after it. Here is an example config file with the appropriate GET request url:

docker-compose.yml:

service_a:
    image: service_a_image
    ports:
        - 7000 # maps port to host
    links:
        - service_b
service_b:
    image: serivce_b_image
    expose:
        - 8000 # exposes port to other containers

Get URL from service A to B:

http://service_b:8000

Hope this helps!

like image 161
Sneaksta Avatar answered Mar 28 '26 14:03

Sneaksta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!