I'm using an API gateway (for now) with NestJS and a microservice (also with NestJS) in a Docker container. The plan is to have the gateway communication with any containable microservice I configure, but I'm having a hard time establishing this connection.
- If I deploy both projects (gateway and microservice) without containers, they communicate successfully;
- If the gateway is inside a container, it won't find the service because it is looking for it at 'localhost' and not the container host (I'll look into this later, even though I'm not sure how to solve it just yet);
- If the gateway is deployed locally and the microservice is inside a container, I can't seem to communicate with it, it always gives me a timeout.
This last point is the problem I'm trying to address here. After a lot of experiments I can't seem to make this work at all, and it looks to be a pretty trivial thing. Here's my code:
Gateway modules configuration:
@Module({
imports: [
ClientsModule.register([
{
name: 'trips-svc',
transport: Transport.TCP,
options: {
host: '127.0.0.1',
port: 3002,
},
},
]),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Gateway docker-compose
version: "3.7"
services:
gateway:
build:
context: .
target: development
networks:
- web-gate-net
volumes:
- .:/usr/src/web-gate
- /usr/src/web-gate/node_modules
expose:
- 5001
ports:
- 5001:5001
command: npm run start:dev
restart: "on-failure"
networks:
web-gate-net:
(the Dockerfile associated with this project simply builds the application and runs node dist/main
)
Microservice docker-compose
version: "3.7"
services:
service:
build:
context: .
target: development
network: host
volumes:
- .:/usr/src/trips-svc
- /usr/src/trips-svc/node_modules
expose:
- ${SERVICE_PORT}
ports:
- ${SERVICE_PORT}:${SERVICE_PORT}
command: npm run start:dev
restart: "on-failure"
(SERVICE_PORT is set on an .env file as 3002)
I've done some experiments with these configurations (using the expose
keyword, the ports
keyword, both...), but this is the configuration as-is. I'm relatively new to networks, so I'm not sure what's wrong here, but I'm fairly certain this has to do with my networking configurations, and since it's a rather specific issue, I can't seem to find any documentation that helps.
If anyone could give me any pointers, I'd be much appreciated.
Thanks.