简体   繁体   中英

how to correctly configure ngrok docker container for node or react app?

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(ie, 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM