0

I'm coding with NestJS and using Docker, I can't configure my own docker-compose file to serve on the local network card with IP 192.168.1.7.

i saw this link before.

Docker subnet: 192.168.65.0

networks:
  host:
    name: mamanpaz
    external: true
  lan_access:
    driver: bridge

services:
  application:
    build:
      context: .
      dockerfile: apps/application/Dockerfile
      target: development
    command: npm run start:dev application
    env_file:
      - apps/application/.env
    depends_on:
      - rabbitmq
      - register
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    networks:
      - default
    extra_hosts:
      - "host.docker.internal:host-gateway"
    ports:
      - '3000:3000'

docker network ls command output

NETWORK ID     NAME                  DRIVER    SCOPE
2b6d6312d176   bridge                bridge    local
a776b0af5ba3   host                  host      local
b152fd5400db   mamanpaz_default      bridge    local
4b401d652fb2   my-network            bridge    local
6476cf7a691a   none                  null      local
DolDurma
  • 15,753
  • 51
  • 198
  • 377
  • You can use the host network in Docker: https://docs.docker.com/network/drivers/host/ – jabaa Jun 07 '23 at 11:58
  • @jabaa i didn't get your meant – DolDurma Jun 07 '23 at 13:33
  • You can configure your servers to listen on your local IP address using the host network. You write: _"I can't configure my own d`ocker-compose` file to serve on the local network card with IP `192.168.1.7`."_, but technically it's possible. – jabaa Jun 07 '23 at 13:43
  • 2
    May I ask why you want to use the "start:dev" command instead of compiling the application and using "npm start" to run the production build? Usually this is the better approach when creating docker containers. – Fabian Strathaus Jun 07 '23 at 14:10
  • @jabaa my problem is i don't know how can i configure that, could you update my `docker-compose.yaml`? – DolDurma Jun 07 '23 at 14:22
  • @FabianStrathaus my application is under development and it in's done yet – DolDurma Jun 07 '23 at 14:23
  • Does this answer your question? [How to use host network for docker compose?](https://stackoverflow.com/questions/56582446/how-to-use-host-network-for-docker-compose) – jabaa Jun 07 '23 at 14:52
  • 1
    You do **not** need or want host networking for this. The existing `ports:` setting should be enough; assuming you're not using a VM-based solution like Docker Desktop, that should make the container visible on port 3000 on all host interfaces, and from their your local network. – David Maze Jun 07 '23 at 14:54
  • 1
    The Angular dev server is bound to the localhost IP. If you start it inside a container, you also have to modify this configuration, otherwise you can't reach the server. I don't know NestJs, but it's probably the same dev server. Either use a production-ready server (also for dev) or check the configuration. – jabaa Jun 07 '23 at 14:57
  • @DavidMaze i'm using Docker Desktop and how can i change port? i tested `posrt: '80:3000` but when i try to open local network ip which that's `http://192.168.1.7` i get refused error – DolDurma Jun 07 '23 at 16:26
  • @jabaa i saw the your referred link, i have the same problem, and i checked `extra_hosts: - "host.docker.internal:host-gateway"` and `network_mode: host` but i can't. could you updare my `docker-compose.yaml` please? – DolDurma Jun 07 '23 at 16:40
  • 1
    No, I wouldn't write a duplicate answer. If this is the solution, your question should be closed as duplicate. You can't use `networks` with `network_mode: host`. – jabaa Jun 07 '23 at 16:54
  • @jabaa But host networking mode is not necessary. If the dev server starts bound to `127.0.0.1:3000` within the container, then the server would also not be accessible from remote LAN machines. – OneCricketeer Jun 07 '23 at 17:49

2 Answers2

1

Make sure you're accessing the API on port 3000, not with the IP alone. Otherwise, you need to forward port 80:3000 (assuming NestJS is actually configured to run on port 3000)

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • port of `3000` is free and when i change left part of port to 80 i get refused connection of `192.168.1.7`, could you please update my `docer-compose. yaml`? – DolDurma Jun 07 '23 at 15:03
  • I can't update your file if I don't know if your server is actually started on port 3000, or something else. You need to use `sudo docker compose up` to bind to port less than 1024. But as mentioned, you should access http://192.168.1.7:3000 anyway, not modify the forwarded port (and you've not clarified what the server port is actually using) – OneCricketeer Jun 07 '23 at 17:47
  • My application is launched on port 3000 because it is easily accessible with the addresses `http://127.0.0.1:3000/` and `http://localhost:3000/`. However, I cannot access it with the local IP address `http://192.168.1.7:3000`. i updated `docker-compose.yaml` on my question – DolDurma Jun 07 '23 at 20:33
  • Okay, well, the Docker Compose doesn't control external IP access, so sounds like your OS firewall is blocking external connections from other machines – OneCricketeer Jun 07 '23 at 20:51
0

Enabling Debian distribute via Settings->Resources -> WSL integration in Docker Desktop solved my problem

DolDurma
  • 15,753
  • 51
  • 198
  • 377