layout: post
title: Ngrok做内网穿透。
categories: Kubernetes,docker,Ngrok
description: Ngrok。

keywords: Docker, Ngrok

Ngrok for Docker Server and client

Codenvy badge

A Docker image for ngrok v2, introspected tunnels to localhost.
It’s based on the excellent work of wizardapps/ngrok and fnichol/ngrok.



To see command-line options, run docker run --rm wernight/ngrok ngrok --help.


Supposing you’ve an Apache or Nginx Docker container named web_service_container listening on port 80:

$ docker run --rm -it --link web_service_container wernight/ngrok ngrok http web_service_container:80  

Environment variables

Please consider using directly the command-line arguments of Ngrok.

If you use the default CMD (i.e. don’t specify the ngrok command-line but only wernight/ngrok),
then you can use instead envrionment variables magic below.

You simply have to link the Ngrok container to the application under the app or http or https aliases, and all of the configuration will be done for you by default.

Additionally, you can specify one of several environment variable (via -e) to configure your Ngrok tunnel:

Full example

  1. We’ll set up a simple example HTTP server in a docker container named www:

    $ docker run -v /usr/share/nginx/html --name www_data busybox true  
    $ docker run --rm --volumes-from www_data busybox /bin/sh -c 'echo "<h1>Yo</h1>" > /usr/share/nginx/html/index.html'  
    $ docker run -d -p 80 --volumes-from www_data --name www nginx  
    $ curl $(docker port www 80)  
  2. Now we’ll link that HTTP server into an ngrok container to expose it on the internet:

    $ docker run -d -p 4040 --link www:http --name www_ngrok wernight/ngrok  
  3. You can now access the API to find the assigned domain:

    $ curl $(docker port www_ngrok 4040)/api/tunnels  

    or access the web UI to see requests and responses:

    $ xdg-open http://$(docker port www_ngrok 4040)  


For common cases you may want to create an alias in your ~/.profile (or ~/.bashrc, ~/.zshrc, or equivalent):

function docker-ngrok() {  
  docker run --rm -it --link "$1":http wernight/ngrok ngrok http http:80  
# For ZSH with Oh-My-Zsh! and 'docker' plugin enabled, you can also enable auto-completion:  
#compdef __docker_containers docker-ngrok  

Then to the simple example just do docker-ngrok web_service_container.


Report issues/questions/feature requests on GitHub Issues.

Pull requests are very welcome!