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
66.92k stars 7.55k forks source link

Using configServer on docker cannot access the remote database #10107

Closed ghost closed 2 years ago

ghost commented 2 years ago

Is there an existing issue for this?

Current behavior

app.module.ts

TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        const tempHost = configService.get('MYSQL_HOST')
        const tempPort = configService.get('MYSQL_PORT')
        const tempUser = configService.get('MYSQL_USER')
        const tempPassword = configService.get('MYSQL_PASSWORD')
        const tempName = configService.get('MYSQL_NAME')
        return {
          type: 'mysql',
          host: tempHost,
          port: tempPort,
          username: tempUser,
          password: tempPassword,
          database: tempName,
          entities: allEntity,
          synchronize: true
        } as TypeOrmModuleOptions
      },
    }),
    // redis
    RedisModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        const tempHost = configService.get('REDIS_HOST')
        const tempPort = configService.get('REDIS_PORT')
        const tempPassword = configService.get('REDIS_PASSWORD')
        const tempName = configService.get('REDIS_NAME')
        return {
          config: {
            host: tempHost,
            port: tempPort,
            password: tempPassword,
            db: tempName,
          }
        } as RedisModuleOptions;
      }
    }),
截屏2022-08-13 下午2 29 47

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-ptvjje?file=src/app.module.ts

Steps to reproduce

No response

Expected behavior

nestjs is work in docker

Package

Other package

No response

NestJS version

9.0.0

Packages versions

{
  "name": "123",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "css-build": "node-sass --omit-source-map-url ./public/scss/bulma.scss ./public/css/bulma.css",
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "NODE_ENV=development nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "NODE_ENV=production node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@liaoliaots/nestjs-redis": "^9.0.0",
    "@nestjs/common": "^9.0.2",
    "@nestjs/config": "^2.2.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/microservices": "^9.0.0",
    "@nestjs/platform-express": "9.0.0",
    "@nestjs/platform-socket.io": "^9.0.5",
    "@nestjs/swagger": "^6.0.2",
    "@nestjs/typeorm": "^9.0.0",
    "@nestjs/websockets": "^9.0.5",
    "bulma": "^0.9.4",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "crypto-js": "^4.0.0",
    "hbs": "^4.2.0",
    "ioredis": "^5.2.1",
    "moment": "^2.29.3",
    "mysql2": "^2.3.3",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0",
    "swagger-ui-express": "^4.4.0",
    "swiper": "^6.5.1",
    "typeorm": "^0.3.7"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/crypto-js": "^4.1.1",
    "@types/express": "^4.17.13",
    "@types/hbs": "^4.0.1",
    "@types/jest": "28.1.4",
    "@types/multer": "^1.4.7",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "^28.1.2",
    "node-sass": "^7.0.1",
    "prettier": "^2.3.2",
    "supertest": "^6.1.3",
    "ts-jest": "^28.0.5",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "^4.0.0",
    "typescript": "^4.3.5"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

No response

In which operating systems have you tested?

Other

No response

jmcdo29 commented 2 years ago

Nest works in Docker. I use Nest with Docker almost daily. Your reproduction in stackblitz doesn't have anything docekr related, so it's not helpful.

Please use our Discord channel (Support). We are using GitHub to track Bug Reports, Feature Requests, and Regressions.

micalevisk commented 2 years ago

for reference, here's the Dockerfile I've been using so far: https://gist.github.com/micalevisk/9ee47135b22f284dd73de6ea6d5aa864