0

I have a nestjs monorepo where I a have a gateway app that I built to handle all requests. When I use the project outside of docker I am able to query correctly however once I use docker-compose to run my project I get the following error

Error occurred while proxying request localhost:3000/ to http://localhost:3001/

I have tried changing localhost to 127.0.0.1 and additionally if I go to localhost:3001 I can see the auth endpoints correctly so the ports are mapped correctly. My hunch is that this is something to do with Docker

Gateway

async function bootstrap() {
    const app = await NestFactory.create<NestExpressApplication>(AppModule, {
        rawBody: true,
    })
    app.setGlobalPrefix('api')

    app.set('trust proxy', true)
    app.enableCors()
    app.disable('x-powered-by')

    const configService = app.get(ConfigService)
    const port = configService.get<number>('app.port')

    app.use(
        '/api/auth',
        createProxyMiddleware({
            target: `http://localhost:3001`,
            changeOrigin: true,
            secure: false,
            pathRewrite: {
                '^/api/auth': '/',
            },
        }),
    )

    console.log(`Starting on ${port}`)
    await app.listen(port)
}

docker-compose.yml

    version: '3.8'
    services:
        mongodb:
            image: mongo:latest
            environment:
                MONGO_INITDB_ROOT_USERNAME: root
                MONGO_INITDB_ROOT_PASSWORD: rootpassword
            ports:
                - 27017:27017
    
        gateway:
            build:
                context: .
                dockerfile: ./apps/gateway/Dockerfile
                target: development
            command: npm run start:dev gateway
            ports:
                - 3000:3000
            env_file:
                - ./apps/gateway/.env
            depends_on:
                - mongodb
            volumes:
                - .:/usr/src/app
                - /usr/src/app/node_modules
    
        auth:
            build:
                context: .
                dockerfile: ./apps/auth/Dockerfile
                target: development
            command: npm run start:dev auth
            ports:
                - 3001:3001
            env_file:
                - ./apps/auth/.env
            depends_on:
                - mongodb
            volumes:
                - .:/usr/src/app
                - /usr/src/app/node_modules

Jason McFarlane
  • 2,048
  • 3
  • 18
  • 30

0 Answers0