Port Mapping on host network mode

version: '3.8'
services:
  app:
    build: 
      context: .
      dockerfile: Dockerfile
    ports:
      - "51000:51000"
    volumes:
      - .:/app
    depends_on:
      - mongo
    network_mode: "host"

What Will happen to ports If my network_mode is the host but still I map the ports? does it map to localhost:51000 or something else happens?

You mean you gave it as --net=host? Then just use the same IP as the host.

I think port 51000 will be exposed rather than port forwarded?

Let me rephrase your question: If a container is not isolated from the network namespace of the host, does port mapping still work?

No it does not. The container will behave network-wise like any native process on the host. The application inside the container will bind its ports directly on the ips of the host network interfaces.

1 Like