Pablo
Pablo

Reputation: 1332

Making an HTTP request within the same Docker network

I have a few services running in different docker containers, as per my docker-compose:

version: '3'
services:
  rest:
    build:
      context: './service/'
    image: persian_rest:latest
    container_name: persian_rest
    ports:
      - 8080:8080
    networks:
      - persian_net
    volumes:
      - persian_volume:/data
  scheduler:
    build:
      context: './scheduler/'
    image: persian_scheduler:latest
    container_name: persian_scheduler
    networks:
      - persian_net
  ui:
    build:
      context: './ui/'
    image: persian_ui:latest
    container_name: persian_ui
    ports:
      - 5000:5000
    networks:
      - persian_net
  database:
    image: 'mongo:latest'
    container_name: 'persian_database'
    networks:
      - persian_net
    environment:
      - MONGO_INITDB_ROOT_USERNAME=persian_admin
      - MONGO_INITDB_ROOT_PASSWORD=123456
    ports:
      - 27017:27017
    volumes:
      - persian_volume:/data
volumes:
  persian_volume:
networks:
  persian_net:
    driver: bridge

I have my UI persian_ui service making HTTP request to the REST service persian_rest. I thought that since they were in the same network, I would just make a request to http://persian_rest:8080/api

However, when I do make that request, it fails to find that resource: enter image description here

Does anyone know why my containers joined by the same network are not able to perform requests?

Upvotes: 0

Views: 1679

Answers (1)

Sydney Y
Sydney Y

Reputation: 3152

Currently you are looking at a webpage at localhost:5000. You requested the webpage from the server localhost:5000 and it complied and sent you a webpage which is now sitting on your computer.

If you now want to access an API on the same server as the webpage, you can make another request to localhost but this time port 8080. localhost:8080/api.

The webpage in the browser is on the client-side, and the names you've given your containers are for reference inside the server. From outside the server, currently the reference is localhost.

Upvotes: 3

Related Questions