The ngrok Environment

Nextjournal is a great way to experiment with building services in the cloud. Ngrok makes it easy to expose localhost servers to the internet.

Install

curl -sS https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip -o ngrok.zip
unzip /ngrok.zip
4.0s

Configure

Settings for the ngrok configuration file. The following config will tunnel to localhost:5000.

region: us
console_ui: true
tunnels:
  btf:
    proto: http
    addr: 5000
    subdomain: btf
YAML
  • proto: http - the protocol. The HTTP protocol covers both HTTP and HTTPS

  • addr

    • addr: 5000 - the port of the application.

    • addr: https://localhost:5000 use the full URL to force HTTPS (equivalent to ngrok http https://localhost:5000 on the command line)

  • subdomain: btf - available with the paid version of ngrok. Otherwise the subdomain will be random public URL

  • bind_tls - without this setting both HTTP and HTTPS will be available from ngrok

    • bind_tls: true - ngrok only listens on an HTTPS tunnel endpoint

    • bind_tls: false - ngrok only listens on an HTTP tunnel endpoint

  • host_header: localhost - some APIs will want to see the host header of your app rather than 'ngrok'

Grab your authtoken from ngrok, store the token in your Nextjournal secret vault, and add it to the environment. The token will authenticate this notebook with the ngrok service.

Prepend the authtoken to the configuration file above, ngrok.yml.

echo 'authtoken:' $ngrok | cat - ngrok.yml > /tmp/out && mv /tmp/out ngrok.yml
0.9s

Run

Run ngrok in the background using nohup. Unfortunately, there are no plans to offer a daemon service in the basic Ngrok package.

Nohup stands for "No Hangup". The command ignores the HUP signal that terminates a process when a terminal connection is closed. A few notes on using nohup:

  • nohup command &: & run the command in the background

  • nohup command > /path/to/file.txt: > redirects the output from the default nohup.out

Nextjournal will display any stout beneath the running cell. Even with the process running in the background, the stdout will occupy the notebooks process unless it is redirected to a file. Therefore, &> is used below to redirect stout output to a file.

nohup /ngrok start --all --config="/ngrok.yml" &> /tmp/ngrok.log & sleep 1
tail /tmp/ngrok.log
2.0s

Place the URL into a browser and the tunnel will be visible. Anything served from port 5000 will appear here. In this case, there is nothing being served, hence the error message.

Admin

The service is up and running. To stop the ngrok tunnel, use the kill command with the process ID: kill <PID> or kill -9 "$(pgrep ngrok)".

ps aux
0.9s
# kill -9 "$(pgrep ngrok)"
0.8s
ps aux
1.1s

Appendix

Consider reading An Ngrok Tutorial and Primer by Daniel Miessler

Runtimes (1)