samuelj123
samuelj123

Reputation: 67

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

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!

Upvotes: 1

Views: 1152

Answers (1)

Narro
Narro

Reputation: 440

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.
enter image description here
Only containers in Docker containers rectangle is supposed to calling each other withing container's name.

Upvotes: 8

Related Questions