Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose make requests between containers

I'm a bit new to docker-compose, so I'm not so sure what am I looking for even.

I created two images, and I'm running them using docker-compose, on a local environment these two services communicate via HTTP requests (both are running on localhost, one service on port 3000, one service on port 8000)

When I moved those two services to docker (two separated containers and images) I can't seem to make them communicate.

this is my docker-compose file:

version: '3'

services:
  service1:
    image: services/services1
    ports:
      - 3000:3000
    links:
      - "service2"
    depends_on:
      - service2

  service2:
    image: services/service2
    ports:
      - 8000:8000

When I'm making http requests directly to each of the services I get a good response, but when I'm making a request to service1 and in services1 I have another request to service 2, I can't get a response at all

Error: connect ECONNREFUSED 127.0.0.1:8000

Both services are running on 0.0.0.0

like image 491
Elon Salfati Avatar asked Aug 24 '18 19:08

Elon Salfati


People also ask

How do you communicate between containers in Docker compose?

Here's the gist: For containers to communicate with other, they need to be part of the same “network”. Docker creates a virtual network called bridge by default, and connects your containers to it. In the network, containers are assigned an IP address, which they can use to address each other.

Can two Docker containers talk to each other?

A Docker network lets your containers communicate with 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.

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.


2 Answers

so I'm not so sure what am I looking for even.

Most probably you are looking for this part of the docker documentation. It explains how docker compose is treating network. Part of particular interest for you is this:

By default Compose sets up a single network for your app.
Each container for a service joins the default network and is both reachable
by other containers on that network, and discoverable by them 
at a hostname identical to the container name.

Meaning that you should use service1 and service2 instead of localhost to target across services.

like image 117
Const Avatar answered Sep 28 '22 05:09

Const


All the services declared in the docker-compose.yml file are running in their own container. They have different ips. You can address them with their service name that will resolve to the service ip. In your case:

docker-compose exec service1 ping service2
or
docker-compose exec service2 ping service1
like image 24
Jonathan Santerre Avatar answered Sep 28 '22 05:09

Jonathan Santerre