How to Restrict Access to a Dockerized Node.js Backend While Allowing React Frontend to Communicate Internally?

I’m working on a web project with a React frontend and a Node.js backend, both running in Docker containers. I’m using Docker Compose to orchestrate the services. The Node.js backend serves XML data and HTML content to the React frontend.

Project Structure:

  • React Frontend: Serves the static site and fetches data from the Node.js backend.
  • Node.js Backend: Provides the data and content to the React frontend.
  • Traefik: Acts as a reverse proxy.
    Docker Compose Configuration:

Here’s a snippet of my docker-compose.yml:

version: '3.8'

services:
  react-app:
    container_name: "example-static-site-frontend"
    build:
      context: ./react-app
    networks:
      - t2_proxy
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.react.rule=Host(`static.example.in`)"
      - "traefik.http.routers.react.entrypoints=web"
      - "traefik.port=3472"
    depends_on:
      - node-app

  node-app:
    container_name: "example-static-site-backend"
    build:
      context: ./node-app
    networks:
      - t2_proxy

  traefik:
    image: "traefik:v2.9"
    container_name: "example-static-site-traefik"
    command:
      #- "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
    ports:
      - "80:80"
      - "8080:8080"
    labels:
       - traefik.enable=true
       - traefik.http.routers.api.rule=Host(`example-static-site-traefik.example.in)
       - traefik.http.routers.api.entrypoints=web
       - traefik.http.routers.api.service=api@internal
      #  - traefik.port=8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    networks:
      - t2_proxy

networks:
  t2_proxy:

Problem:

I want to restrict access to the Node.js backend (node-app running on port 8672) so that it is not accessible from outside the Docker network. However, I still need the React frontend to fetch data from the Node.js backend using Axios.

I’ve tried removing the ports section from the node-app service and updating the Axios requests in the React app to use the Docker service name (node-app) instead of localhost, but I’m getting a 404 Page Not Found error when the React app (http://static.example.in/?site=a&page=b) tries to fetch the data.

Axios Configuration in React:

Here’s how I’ve updated the Axios calls in the React app:

// In App.js and xmlService.js
const response = await axios.get('http://node-app:8672/data.xml');

Questions:

How can I correctly configure Docker Compose and Axios in my React app to restrict external access to the Node.js backend while allowing internal communication between the services?
What could be causing the 404 Page Not Found error when trying to fetch data from the backend, and how can I troubleshoot this issue?
Any guidance or suggestions would be greatly appreciated!

I have tried replacing localhost with service name of node but it did not work.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật