-1

I'm trying to use ngrok for exposing my localhost to share my node backend with a friend. The problem I face is that whenever I start node backend the ngrok container couldn't work cause it trys to map the same port as being used by node.

node:events:371
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::5000
    at Server.setupListenHandle [as _listen2] (node:net:1319:16)
    at listenInCluster (node:net:1367:12)
    at Server.listen (node:net:1454:7)
    at Function.listen (C:\Users\928941\node_backends\express-for-react\node_modules\express\lib\application.js:618:24)
    at Object.<anonymous> (C:\Users\928941\node_backends\express-for-react\app.js:27:5)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1346:8)
  code: 'EADDRINUSE',
  errno: -4091,
  syscall: 'listen',
  address: '::',
  port: 5000
}
[nodemon] app crashed - waiting for file changes before starting...

the above error simply means that port 5000 is currently in use(i.e., by ngrok docker container).

If I run the node app first and try to run the docker container after that I get this error from docker.

[+] Running 1/0
 - Container docker-compose_for_ngrok_ngrok_1  Created                                                             0.0s
Attaching to ngrok_1
Error response from daemon: Ports are not available: listen tcp 0.0.0.0:5000: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.

This is my docker-compose.yml file

version: '3'

services:
  ngrok:
    image: ngrok/ngrok:alpine
    ports:
      - 4040:4040
      - 5000:5000
    environment:
      - NGROK_AUTHTOKEN=my_authentication_token
    command: http 5000

the compose file is same as this docker run command so anyone can be used.

docker run -d -p 4040:4040 -p 5000:5000 -e NGROK_AUTHTOKEN=my_token ngrok/ngrok:alpine http 5000

Port 4040 is default ngrok port for dashboard and it works fine for me.

I'm also facing this issue with react app as well.

  • Why the overhead with a docker container? – Marc Feb 08 '22 at 13:55
  • @TheFool I don't want to deploy the files to a server. I wanted to try out the ngrok tunnels. – Deepak Gupta Feb 08 '22 at 14:08
  • @Marc I understand you but the machine I'm learning on is not mine and I don't have the administrative access to install anything that's the reason why docker container. – Deepak Gupta Feb 08 '22 at 14:10
  • Why do you publish from the docker container 2 ports? I get that `4040` is for the Web UI, but for what does docker need to reserve/use port `5000`? Is port `5000` not supposed to be your node.js app? If docker uses this port, then you cant use it for your node.js app, wich results in the "address already in use :::5000" error. – Marc Feb 08 '22 at 14:15
  • @Marc If I don't do the port forwarding by mentioning 5000:5000 it's not going to connect my docker's network to my machine's localhost ports. When I mention 4040:4040 I'm doing that port forwading and similarly 5000:5000 is required. I wouldn't have to do this if I had ngrok cli on my local machine. – Deepak Gupta Feb 08 '22 at 14:18
  • @DeepakGupta I think thats a missunderstanding. But this -p argument is to publish ports from the container to the host system. This measn inside the container need to be a service running which creates a socket on that port (e.g. a webserver) By default docker use the "bridge" network, specify to use the "host" network instead and remove the publishing of port 5000. – Marc Feb 08 '22 at 14:23

1 Answers1

0

I'm not sure if you are running on linux, but you might not need to do any port forwarding if you use the --net=host option. See the examples in https://hub.docker.com/r/ngrok/ngrok/

The equivalent for a docker compose file is network_mode:https://docs.docker.com/compose/compose-file/compose-file-v3/#network_mode

So your docker compose would be:

version: '3'

services:
  ngrok:
    image: ngrok/ngrok:alpine
    environment:
      - NGROK_AUTHTOKEN=my_authentication_token
    command: http 5000
    network_mode: "host"

Assuming your node is running on localhost:5000, if you start a docker container and use the same network, then you don't need to open any ports.

If you are using mac or windows, you will need to use the workaround here: https://docs.docker.com/desktop/mac/networking/#use-cases-and-workarounds

Which is to use command: http host.docker.internal:5000 instead.

Russ Savage
  • 548
  • 2
  • 14
  • nothing seems to work. I've tried it with the host network as well but now I can't even access the dashboard for ngrok running on port 4040. FYI I'm running windows. – Deepak Gupta Feb 14 '22 at 12:09