1

In the docker compose file i added api server as a service and mongodb is installed in my local pc. But when the api run in docker container it could not connect with 127.0.0.1:27017.

Here is the docker-compose file.

networks:
  test_network:
    name: test_network
    driver: bridge
services:
  api:
    container_name: api
    build: ./api
    ports:
      - "3031:3031"
    networks:
      - test_network

Why this problem is happening and how i can resolve this problem ?

Kousher Alam
  • 1,005
  • 1
  • 15
  • 30
  • 1
    To connect outside a docker container instead of `localhost or 127.0.0.1` try this `host.docker.internal` – Andrew Mititi Aug 26 '21 at 13:40
  • For more info on troubleshooting checkout this https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach – Andrew Mititi Aug 26 '21 at 13:41

1 Answers1

1

It happens because the IP address 127.0.0.1:27107 refers to the container itself.

You have to address the above port, but the host ip as I explained here.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74