What should I add as api-address when within a multi-container docker-compose setup?

别来无恙 提交于 2021-01-28 21:59:26

问题


I have a simple 2 container docker-compose setup. A frontend (Angular) and a backend (nestjs/express).

Here's my docker-compose (development)

version: "3.3"
services:
  db:
    image: 'postgres:10-alpine'
    container_name: 'postgresnew'
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=4321
      - POSTGRES_DB=bleh
    volumes:
      - pgdata:/var/lib/postgresql/data
  frontend:
    build: ./frontendapp
    ports:
      - 4001:4200
    volumes: 
      - ./frontendapp:/app
      - /app/node_modules/
  backend: 
    build: ./backendapp
    ports: 
      - 4000:8080
    volumes:
      - ./backendapp:/app
      - /app/node_modules/
    env_file:
      - .env
volumes: 
  pgdata:

Now, having exposed the correct ports, my frontend is able to access the backend pretty easily using a localhost:4000 address, as seen in my frontend environmnent

const x = 'http://localhost:4000';

export const environment = {
  production: false,
  API_USERSERVICE: x + "/api/user",
  API_PROJECTSERVICE: x + "/api/project",
  API_TASKSERVICE: x + "/api/task",
  API_PGROUPSERVICE: x + "/api/pgroup",
  API_KPISERVICE: x + "/api/kpi",
  API_FUNDRAISINGSERVICE: x + "/api/fundraising"

}; 

But I would like for it to connect using docker's internal bridge network; instead of localhost:4000, and I'm not sure what to use as its address... I tried const x = 'http://twrapp_backend_1' which is it's name, but I got a CORS error, plus, it seems like it was trying to access a website called by that name... I tried const x = 'twrapp_backend_1' as well... I googled this for a week, not sure if I'm making a mistake on the docker end of things, or on the angular end of things. Help!


回答1:


No, you cannot request your backend service via docker's internal bridge network. because your request to backend service is invoked by the browser who is not in the docker's internal bridge network.

Only containers in Docker containers rectangle is supposed to calling each other withing container's name.



来源:https://stackoverflow.com/questions/59350343/what-should-i-add-as-api-address-when-within-a-multi-container-docker-compose-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!