ngrok: Accessible Self Hosting Simplified

I was trying to come up with a descriptive title for what ngrok does and that was the best I could do. Let me explain what I really mean.

Whether you are developing a micro service or Web app on your laptop, it is pretty common to want to give someone access to it over the Internet. That can be a bit of a pain as you need to look up your IP; make sure your router does the appropriate port forwarding; and confirm that you don’t have any firewalls that could get in the way. All that just to demo to someone what you are working on. Anyone that has done any amount of remote work has had to deal with this scenario.

Ngrok simplifies this whole process. It is a command line utility that sets up the necessary secure tunnels to expose the app you are running locally and registers it with an ngrok URL to make it easy to share. Let’s give it a go.

Step 1: Setup a simple Web app / service

Any app will do here. You can use one that you are currently developing or just have sitting around. In case you don’t have one, let’s spin up an instance of Elasticsearch using docker. First download the image we want to run.

docker pull docker.elastic.co/elasticsearch/elasticsearch:7.5.2

Now start it up in single node mode, a.k.a. dev mode.

docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.5.2

Let’s check that it is running correctly by sending it a request. I am using HTTPie to do that.

http localhost:9200

You should get a successful response with a 200 status code and some information about your Elasticsearch instance.

Step 2: Install ngrok

This install is pretty painless. Download the binary from the ngrok Website.

Create an account with ngrok. Don’t worry, it’s free. You need an account to get an auth token that you will use to setup the binary that you just downloaded.

./ngrok authtoken <YOUR_AUTH_TOKEN>

If you are looking for your auth token, it should be on your ngrok dashboard.

Step 3: Expose your Elasticsearch instance

Now for the fun stuff. Let’s use ngrok to make your instance of Elasticsearch accessible over the Internet. The command below creates an HTTP tunnel on port 9200 which is the same one that our docker container is using for Elasticsearch.

./ngrok http 9200

The tunnel should start up pretty quickly and you’ll see on the console the ngrok URLs that have been assigned to it. You should get one HTTP and one HTTPS.

Account                       Chris Jordan (Plan: Free)                                                                                                                                                                                  
Version                       2.3.35                                                                                                                                                                                                     
Region                        United States (us)                                                                                                                                                                                         
Web Interface                 http://127.0.0.1:4040                                                                                                                                                                                      
Forwarding                    http://fed1a36e.ngrok.io -> http://localhost:9200                                                                                                                                                          
Forwarding                    https://fed1a36e.ngrok.io -> http://localhost:9200

Let’s send a request to one of those URLs.

http https://fed1a36e.ngrok.io

That’s it. You’ve exposed your locally running Elasticsearch on the Internet securely.

As well as the tunnel that just got setup, ngrok has a locally running dashboard that displays all sorts of useful information about requests and connections being made to your app. You can access it here: http://localhost:4040/

Closing Thoughts

Ngrok is an amazing utility especially given the current climate that many of us find ourselves in with respects to COVID-19. A lot of us are now working from home and I know it can be a bit of a struggle to be as efficient and effective as we normally are when at the office. Calling a co-worker or friend over and giving them a demo of what you are doing is just so simple and natural to do in person. Ngrok doesn’t completely replace that but it is pretty close. Probably as close as we are going to get while we are all remote. Still not the same but it does make it easy to demo what you are doing.

Be safe everyone. We’ll get through it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: