skip to Main Content

I have created a simple application with react and spring boot. Now I’m trying to deployed it with docker container. Both react and spring boot are compiled correctly ( containers are working on the same machine ).

React App is visible to the internet ( port 80 of the react app container ), spring boot is published on port 8080 and is not reachable from the outside.
This is the current port mapping:

enter image description here

The docker compose file is configured as follow:

version: '3'
services:
  app_fe:
    image: app_fe:latest
    container_name: app_fe
    expose:
      - 3000
    ports:
      - 80:3000 
    depends_on:
      - app_be

  app_be:
    image: app_be:latest
    container_name: app_be
    ports:
      - 8080:8080

I’ve tried all possible ip configuration inside app_fe but is not able to reach the backend of the app when the request comes from the client browser.

The request is completed successfully if it is made from the machine where the containers are published.

Which IP should I configure in the front end or how should I modify the docker compose configuration to make the communication possible?

2

Answers


  1. You need to link your app_fe to your app_be. Linking allows your containers to discover each other.

    Regarding this point, your compose file should look like following:

    version: '3'
    services:
      app_fe:
        image: app_fe:latest
        container_name: app_fe
        expose:
          - 3000
        ports:
          - 80:3000 
        depends_on:
          - app_be
        links:
          - app_be
    
      app_be:
        image: app_be:latest
        container_name: app_be
        ports:
          - 8080:8080
    
    Login or Signup to reply.
  2. Since the links marked as legacy feature of Docker. It may eventually be removed.

    https://docs.docker.com/network/links/

    The –link flag is a legacy feature of Docker. It may eventually be removed. Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using –link. One feature that user-defined networks do not support that you can do with –link is sharing environment variables between containers. However, you can use other mechanisms such as volumes to share environment variables between containers in a more controlled way.

    You can use the network feature to enable the communication between containers.

    https://docs.docker.com/compose/networking/

    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.

    Here is the updated compose yaml

    version: '3'
    services:
      app_fe:
        image: app_fe:latest
        container_name: app_fe
        expose:
          - 3000
        ports:
          - 80:3000 
        depends_on:
          - app_be
        networks:
          - my_apps_network
    
      app_be:
        image: app_be:latest
        container_name: app_be
        ports:
          - 8080:8080
        networks:
          - my_apps_network
        
    networks:
      my_apps_network:
        driver: bridge
    

    The app_fe and app_be are connected to the same network. This allows them to communicate with each other using the service names.

    References :

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search