Accessing a Docker container with NGrok

I want to access a local docker development environment from outside the host computer. This comes in handy for viewing a site on another device or sharing it with a client. There seems to be a couple of ways to do this documented online, but of course, none exactly worked for me. I’m going to assume you already have ngrok installed for doing the actual tunnel.

First, make sure nothing is using the port you want on the host machine. In my case, this means don’t run apache on the host machine if you’re docker container is using ports 80 or 443.

Then, to setup the ngrok tunnel I use this command:

ngrok http -host-header=rewrite local.example.com:80

The key there being the host-header switch. Without it, apache couldn’t match the configured virtual host to the random hostname assigned by the service. With it, the local site is accessible without having to change anything about its configuration.