简体   繁体   中英

Getting error "cURL error 7: Failed to connect to localhost port" when calling API from another container

I am dockerizing an app that has several different services and I have used the 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. Integration Service(NodeJS)[http://localhost:4433]
  5. Integration Service Database(MongoDB)
  6. Authentication Layer(NodeJS)[http://localhost:4454]
  7. Authentication Layer Database(Mysql)

So far I have successfully containerized all these services but when my "Backend(PHP)" container tries to call API from "Integration Service(NodeJS)" it throws the error which is cURL error 7: Failed to connect to localhost port 4433: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) .

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=
    volumes:
      - ./dump:/docker-entrypoint-initdb.d
    container_name: wms-backend-mysql-app
  phpmyadmin:
    image: phpmyadmin/phpmyadmin:4.7
    depends_on:
      - mysql
    networks:
      - backend
    ports:
      - "40002:80"
    environment:
      - PMA_HOST=wms-backend-mysql-app
      - PMA_PORT= 3306
    volumes:
      - /sessions
    container_name: wms-backend-phpmyadmin-app
  app:
    container_name: wms_auth
    restart: always
    build: ../../wms_auth/.
    networks:
      - backend
    volumes:
      - ../../auth/./:/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
      - MYSQL_USER= root
      - MYSQL_PASSWORD=
    volumes:
      - ../../auth/dump/:/docker-entrypoint-initdb.d
    networks:
      - backend
    healthcheck:
      test: ["CMD", "mysql", "-h", "db","-u","root", "mysql", "-e", "select 1"]
      interval: 1s
      retries: 20
  wms_integration:
    container_name: integration
    restart: always
    build: ../../wmsIntegeration/.
    volumes:
      - ../../wmsIntegeration/./:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - "4433:4433"
    networks:
      - backend
    links:
      - db
    depends_on:
      db:
        condition: service_healthy
  wms_integration_db:
    image: mongo
    restart: always
    networks:
      - backend
    ports:
      - "27019:27017"
    volumes:
      - ../../wmsIntegeration/mongodb:/data/db
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongo db:27017/config --quiet
      interval: 10s
      timeout: 10s
      retries: 5
networks:
  local:
    driver: bridge
    
networks:
  frontend:
  backend:

Bridge all the networks as follows:

networks:
  local:
    driver: bridge
  frontend: 
    driver: bridge
  backend:
    driver: bridge

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