nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.69k stars 7.63k forks source link

Request cannot connect to host inside docker container #9989

Closed viktor111 closed 2 years ago

viktor111 commented 2 years ago

Is there an existing issue for this?

Current behavior

I have nestjs application running in docker container. It uses HttpService which uses '@nestjs/axios' to call some service trough http. I get this error:

Error: connect ECONNREFUSED 127.0.0.1:8081

    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16)

Code:

const getRequest = lastValueFrom(
      this.httpService.get("http://127.0.0.1:8081/").pipe(
        map(resp => resp.data),
      ),
    ); 

Minimum reproduction code

https://stackblitz.com/edit/typescript-6rdzmz?file=index.ts

Steps to reproduce

No response

Expected behavior

To return the request from service.

Package

Other package

No response

NestJS version

No response

Packages versions

{
  "name": "ethereum-service",
  "version": "0.0.1",
  "description": "",
  "author": "sendcrypto",
  "private": true,
  "license": "",
  "scripts": {
    "postinstall": "is-ci || husky install",
    "pretypeorm": "ts-node -r tsconfig-paths/register ./src/database/scripts/copy-typeorm-config.ts",
    "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js",
    "seeds": "ts-node -r tsconfig-paths/register ./node_modules/typeorm-seeding/dist/cli.js",
    "migration:generate": "npm run typeorm -- migration:generate -n",
    "migration:create": "npm run typeorm -- migration:create -n",
    "migration:run": "npm run typeorm -- migration:run",
    "migration:revert": "npm run typeorm -- migration:revert",
    "schema:drop": "npm run typeorm -- schema:drop",
    "seed:config": "npm run seeds -- config",
    "seed:run": "npm run seeds -- seed",
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs}/**/*.ts\" --fix"
  },
  "dependencies": {
    "@alch/alchemy-web3": "^1.4.6",
    "@nestjs-modules/mailer": "1.6.1",
    "@nestjs/axios": "^0.1.0",
    "@nestjs/common": "8.4.7",
    "@nestjs/config": "2.1.0",
    "@nestjs/core": "8.4.7",
    "@nestjs/jwt": "8.0.1",
    "@nestjs/passport": "8.2.2",
    "@nestjs/platform-express": "8.4.7",
    "@nestjs/schedule": "^2.1.0",
    "@nestjs/swagger": "5.2.1",
    "@nestjs/typeorm": "8.0.3",
    "@typechain/ethers-v5": "^10.1.0",
    "apple-signin-auth": "1.5.1",
    "axios": "^0.27.2",
    "bcryptjs": "2.4.3",
    "class-transformer": "0.5.1",
    "class-validator": "0.13.2",
    "ethers": "^5.6.9",
    "fb": "2.0.0",
    "google-auth-library": "8.0.1",
    "handlebars": "4.7.7",
    "multer": "1.4.4",
    "multer-s3": "2.10.0",
    "nestjs-i18n": "8.3.2",
    "nodemailer": "6.7.6",
    "passport": "0.6.0",
    "passport-anonymous": "1.0.1",
    "passport-jwt": "4.0.0",
    "pg": "8.7.3",
    "reflect-metadata": "0.1.13",
    "rimraf": "3.0.2",
    "rxjs": "7.5.5",
    "save": "^2.5.0",
    "source-map-support": "0.5.21",
    "swagger-ui-express": "4.3.0",
    "twitter": "1.7.1",
    "typeorm": "0.2.43",
    "typeorm-seeding": "1.6.1"
  },
  "devDependencies": {
    "@nestjs/cli": "8.2.8",
    "@nestjs/schematics": "8.0.11",
    "@nestjs/testing": "^8.4.7",
    "@types/bcryptjs": "2.4.2",
    "@types/cron": "^2.0.0",
    "@types/express": "4.17.13",
    "@types/facebook-js-sdk": "3.3.5",
    "@types/jest": "27.5.1",
    "@types/multer": "1.4.7",
    "@types/node": "16.11.41",
    "@types/passport-anonymous": "1.0.3",
    "@types/passport-jwt": "3.0.6",
    "@types/supertest": "2.0.12",
    "@types/twitter": "1.7.1",
    "@typescript-eslint/eslint-plugin": "5.25.0",
    "@typescript-eslint/parser": "5.25.0",
    "aws-sdk": "2.1137.0",
    "env-cmd": "10.1.0",
    "eslint": "8.19.0",
    "eslint-config-prettier": "8.5.0",
    "eslint-plugin-import": "2.26.0",
    "eslint-plugin-prettier": "4.0.0",
    "husky": "7.0.4",
    "is-ci": "3.0.1",
    "jest": "28.1.0",
    "prettier": "2.6.2",
    "supertest": "6.2.4",
    "ts-jest": "28.0.1",
    "ts-loader": "9.3.1",
    "ts-node": "10.7.0",
    "tsconfig-paths": "4.0.0",
    "tslib": "2.4.0",
    "typescript": "4.6.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  },
  "engines": {
    "node": ">=14.0.0"
  }
}

Node.js version

No response

In which operating systems have you tested?

Other

No response

micalevisk commented 2 years ago

please use our Discord. This is not a bug but something you've missed in your environment.

Also, your Minimum reproduction code doesn't reproduces anything.

why reproductions are required