0

I have 3 images (todofront, todoback, and todotests), and I use docker-compose to orchestrate these images by creating the containers.

Docker-compose:

version: '3'

services:
  todotests:
    image: todotests
    container_name: todotests
    environment: 
      - FRONT_HOST=todofront
    restart: always
    depends_on:
      - todoback
      - todofront

  todofront:
    image: todofrontend
    container_name: todofront
    ports:
      - 3000:3000
    environment:
      - REACT_APP_API_HOST=todoback
    restart: always
    depends_on:
      - todoback

  todoback:
    image: todobackend
    container_name: todoback
    restart: always
    ports:
      - 3001:3001

In todofront, I use the environment variable REACT_APP_API_HOST with the value 'todoback'. However, this way there is no communication between the front and the back. But when I change the value 'todoback' to 'localhost', then the application works. Is it not possible to use the container name, or is something missing?

To launching containers I use: docker-compose up -d

System: Ubuntu 20.04.6 LTS

image 1:

image 1

image 2:

image 2

image 3:

image 3

In image1, the containers are able to communicate with each other. In image2, I can access the 'todoback' container using 'localhost', but not using 'todoback' which is the container's name.

On image3 I can also access the container using its IP address.

3
  • Hello, you're missing crucial details, like what's the system? How are you launching the containers?
    – Destroy666
    Commented Aug 16, 2023 at 18:53
  • Hello, I believe I have updated the necessary information. Please let me know if anything is missing. Commented Aug 16, 2023 at 19:32
  • I don't quite remember how it was by default on Linux, but you can also attempt to use custom networks: stackoverflow.com/a/59281683/7264739
    – Destroy666
    Commented Aug 16, 2023 at 20:52

1 Answer 1

0

This is expected - the container names are not resolvable from the host. But within the same custom network, you can use the service name to resolve and connect to different containers.

docker-compose creates a custom network and puts all the containers defined in the compose file onto the custom network by default.

So, you can refer to todoback / todofront within the containers, but not from the host.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .