在Docker Compose环境中无法将请求从前端发送到后端

问题描述 投票:0回答:0

我目前面临使用 fetch 或 axios 将请求从我的 React 前端容器发送到我的 NestJS 后端容器的问题。我正在使用 Docker Compose 来运行容器,我可以使用后端的容器名称从前端容器使用 curl 成功发送请求。但是,当我尝试从我的 React 应用程序中发送请求时,请求失败并且我收到错误响应。我检查过 API 端点是否正确,并且前端容器连接到与后端容器相同的网络。是什么导致了这个问题,我该如何解决?这是我的 Dockercompose 文件;

version: '3'

services:
database:
image: postgres
container_name: database
ports:
\- "5434:5432"
volumes:
\- database:/var/lib/postgresql/data
\- images:/app/public/images
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 123
POSTGRES_DB: nest
networks:
\- transcendence

backend:
build:
context: ./backend
container_name: backend
ports:
\- "3000:3000"
\- "5555:5555"
volumes:
\- images:/app/public/images
depends_on:
\- database
networks:
\- transcendence

frontend:
build:
context: ./frontend
container_name: frontend
ports:
\- "80:80"
volumes:
\- images:/app/public/profilePhotos
depends_on:
\- backend
networks:
\- transcendence

volumes:
images:
driver_opts:
o: bind
type: none
device: /root/src/volume/images
database:
driver_opts:
o: bind
type: none
device: /root/src/volume/database

networks:
transcendence:
driver: bridge

我尝试检查 API 端点和 URL 以确保它们正确,并验证前端容器与后端容器连接到同一网络。当我使用后端的容器名称从前端容器使用

curl
时,我希望请求能够像它们一样工作。但是,当我使用
fetch
axios
从我的 React 应用程序发送请求时,请求失败并且我收到错误响应。

docker docker-compose httprequest
© www.soinside.com 2019 - 2024. All rights reserved.