一、准备程序

image-20210530215018555

ngrok.cfg

server_addr: "xiaoqiangzai.xyz:4443"
trust_host_root_certs: false
tunnels: 
  http:
    subdomain: www
    proto:
      http: 192.168.8.245:8080
  https:
    subdomain: www
    proto:
      https: 192.168.8.245:8080

start.sh

./ngrok -config ngrok.cfg -log ngrok.log start http https

二、下载Go基础镜像

docker pull golang:alpine

三、编写Dockerfile

# 指定基础镜像
FROM golang:alpine
# 作者的信息
MAINTAINER wuduoqiang
# 进入到容器后进入到哪个目录
WORKDIR /home
# 指定数据卷
VOLUME ["/home"]
# 将文件拷贝到镜像中
COPY ngrok /home
COPY ngrok.cfg /home
COPY start.sh /home
# 容器启动时候执行的命令
CMD /bin/sh /home/start.sh

四、构建镜像

docker build -t ngrok:1.0.0 .

image-20210530230144821

五、编写Docker-compose

docker-compose up -d && docker-compose logs -f
version: "3.0"
services:
  ngrok:
    image: ngrok:1.0.0
    container_name: ngrok
    restart: always
    privileged: true

六、从Docker拷贝文件

mkdir data
docker cp ngrok:/home/ngrok ./data/
docker cp ngrok:/home/ngrok.cfg ./data
docker cp ngrok:/home/start.sh ./data

七、映射数据卷

需要穿透的地址需要修改ngrok.cfg文件已经start.sh脚本

version: "3.0"
services:
  ngrok:
    image: ngrok:1.0.0
    container_name: ngrok
    restart: always
    privileged: true
    volumes:
      - "./data:/home"

ngrok.cfg

server_addr: "xiaoqiangzai.xyz:4443"
trust_host_root_certs: false
tunnels: 
  http:
    subdomain: www
    proto:
      http: 192.168.8.245:8080
  https:
    subdomain: www
    proto:
      https: 192.168.8.245:8080
  centos246:
    remote_port: 50000
    proto:
      tcp: 192.168.8.246:22

start.sh

./ngrok -config ngrok.cfg -log ngrok.log start http https centos246

八、上传镜像

docker tag ngrok:1.0.0 192.168.8.247/xiaoqiangzai/ngrok:1.0.0
docker push 192.168.8.247/xiaoqiangzai/ngrok:1.0.0

image-20210530233153128

version: "3.0"
services:
  ngrok:
    image: 192.168.8.247/xiaoqiangzai/ngrok:1.0.0
    container_name: ngrok
    restart: always
    privileged: true
    volumes:
      - "./data:/home"