1

I'm trying to run a full-stack application using the docker-compose file. However, back-end(SpringBoot) and front-end(ReactJs) containers can not communicate with each other using the service name.

I know that containers can communicate in user-defined bridge networks. my back-end connects to the database using db:5432 address. However, when I try to make a get request from frontend to the back-end: example: http://back-end:5055/getLogs?page=0

It is not working -> net::ERR_NAME_NOT_RESOLVED

So, Frontend couldn't make any requests to the backend.

If I expose ports from back-end and use http://localhost:5055/getLogs?page=0 everything works fine. However, I want to expose only front-end port to the user and the other stuff (back-end/DB) I want it to be isolated.

How to set up communication between these two???

docker-compose file

services:
  db:
    ... env vars
    networks:
      - my-net
  back-end:
    image: back/preproduction:0.0.1
    networks:
      - my-net
  front-end:
    image: front/preproduction:0.0.1
    ports:
      - "80:3055"
    networks:
      - my-net

... volumes

networks:
  my-net:
    driver: bridge 
Aniket patel
  • 551
  • 1
  • 5
  • 17
  • 1
    without exposing the Backed you won't be able to request, so you have to expose it. – Avi Jul 11 '19 at 09:45
  • 1
    @Avi That's not true. Exposing just makes docker service available to docker host. In the network created by docker-compose, container can communicate directly with the service name. For the questioner, what is your `docker-compose` file version? – leopal Jul 11 '19 at 09:49
  • @leopal I'm using version '3' – DragonDancing Jul 11 '19 at 09:53
  • @Avi, I didn't expose db, however back-end has no problems conntecting to it. There seems to be another issue – DragonDancing Jul 11 '19 at 09:55
  • @РамазанСаатов If you do not have any particular reason to use bridge driver, consider removing all network related stuff from docker-compose file and try again. Also make sure that everywhere you are accessing your services with the docker specified names. – leopal Jul 11 '19 at 10:00
  • @leopal oh.. might be I am wrong, I will look into it – Avi Jul 11 '19 at 10:01
  • 3
    The React app ultimately runs in a browser, not inside a container, and so it can't use these Docker-internal hostnames. – David Maze Jul 11 '19 at 10:14
  • User defined networks have an embedded DNS server : :https://docs.docker.com/v17.09/engine/userguide/networking/configure-dns/ . I know it deprecated but you could try with using `--link`. Containers for the linked service are reachable at a hostname identical to the alias, or the service name if no alias was specified.) – dejanualex Jul 11 '19 at 11:13
  • @DavidMaze Can you explain how to solve this problem? The answered question doesn't give a normal explanation on how to deal with the problem! – DragonDancing Jul 11 '19 at 12:30

0 Answers0