0

I have two apps (microservices) in separate docker composes

app1.yml
version: "3.4"

services:
  app1:
    image: flask-app1
    environment:
      - APP2_URL=http://localhost:8000
    ports:
      - 5000:8000
    volumes:
      - "../:/app/"
    depends_on:
      - db_backend1
    restart: on-failure

  db_backend1:
    ...
app2.yml
version: "3.4"

services:
  app2:
    image: flask-app2
    ports:
      - 8000:8000
    volumes:
      - "..:/app"
    restart: on-failure

of course they have other dependecies (database server, etc) i need to run both of them locally, each of them can run well locally, but in this case app1 need to fetch data from app2 by sending a http get request, so i set the app2 url (http://localhost:8000) as an environment variable (just for dev purposes). but the always get requests exception error saying the connection end closed.

So, it will be great if anyone knows how to sort it out.

  • The hostname is resolved within the container, i.e. `localhost` is the container, not the host. We can either use `host.docker.internal` (see [this question](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach)) or start them in the same network such that they are reachable by the internal dns names (much like we connect to the database through the hostname `db_backend` instead of `localhost`). – Turing85 Oct 13 '21 at 20:29
  • I'd recommend setting the Compose `default` network of one file to point at the other, and then you can use normal Docker networking to talk from one to the other (`app1` and `app2` will be normal host names), but I've also linked this to the canonical question about accessing the host system that @Turing85 mentions. – David Maze Oct 13 '21 at 20:38

1 Answers1

0

The container is a “device” so it has it’s own “localhost” so when you set the url as is, it’s called internally which is not what you want. The solution is to create a network between the composes so you can refer to the specific container as “containerName:port”. You can refer to : Communication between multiple docker-compose projects