简体   繁体   中英

Getting error "ECONNREFUSED 127.0.0.1:80" when one docker container call API from another container

I am dockerizing an app that has several different services and I have used Docker Compose file to achieve this Single docker-compose.yml file for all the services and one Dockerfile for each service.

  1. Frontend(angular)[http://localhost:4200]
  2. Backend(PHP)[http://localhost:80]
  3. Backend Database(Mysql)
  4. Authentication Layer(NodeJS)[http://localhost:4454]
  5. Authentication Layer Database(Mysql)

So far I have successfully containerized all these services but there is one problem when the application tries to log in the "Auth Layer(NodeJS)" sends a request on the "Backend(PHP)" API for some data processing on this step I get "ECONNREFUSED 127.0.0.1:80" error but if I try the same API on Postman it works but not from "Auth Layer(NodeJS)" I have also tried to call some third party API and it also works fine in AuthLayer so what should be the issue as I am new to Docker I am unable to find a solution in days.

this is my docker.compose.yml file

services:
  angular-service:
    container_name: wms_frontend
    build: ../frontend/.
    ports:
      - "4200:80"
  php:
    build: 
      context: .
    image: wms-backend
    networks:
      - frontend
      - backend
    environment:
      - MYSQL_HOST=wms-backend-mysql-app
      - MYSQL_USER=wmsroot
      - MYSQL_PASSWORD=pass
      - MYSQL_DB=dbname
    volumes:
      - ./:/var/www/html/wms/backend
    ports:
      - "80:80"
    container_name: wms-backend-php-app
  mysql:
    image: mysql:5.7
    networks:
      - backend
    environment:
      - MYSQL_ROOT_PASSWORD=rootpassword
      - MYSQL_USER=wmsroot
      - MYSQL_PASSWORD=pass
      - MYSQL_DATABASE=dbname2
    container_name: wms-backend-mysql-app
  phpmyadmin:
    image: phpmyadmin/phpmyadmin:4.7
    depends_on:
      - mysql
    networks:
      - backend
    ports:
      - "40002:40002"
    environment:
      - PMA_HOST=wms-backend-mysql-app
      - PMA_PORT= 3306
    volumes:
      - /sessions
    container_name: wms-backend-phpmyadmin-app
  app:
    container_name: auth_layer
    restart: always
    build: ../../auth_layer/.
    networks:
      - backend
    volumes:
      - ../../auth_layer/./:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - "4454:4454"
    links:
      - db
    depends_on:
      db:
        condition: service_healthy
  db:
    image: mariadb
    restart: always
    ports:
      - "3308:3306"
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD= YES
      - MYSQL_DATABASE=auth_layer
      - MYSQL_USER= root
      - MYSQL_PASSWORD=
    volumes:
      - ../../auth_layer/dump/:/docker-entrypoint-initdb.d
    networks:
      - backend
    healthcheck:
      test: ["CMD", "mysql", "-h", "db","-u","root", "mysql", "-e", "select 1"]
      interval: 1s
      retries: 20
networks:
  local:
    driver: bridge
    
networks:
  frontend:
  backend:

When your app is running inside a docker container, localhost points no longer to your development laptop (or server) it points to the container itself.

As each application is running in separeted container when the front access to the back, you cannot use localhost . As localhost points to the front container, and the back is not deployed there.

You should use the container name instead localhost when specificying the connection urls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM