项目作者: itsziget

项目描述 :
Forward the ports of Docker containers and leave your local ports untouched.
高级语言: Shell
项目地址: git://github.com/itsziget/ssh-tunnel.git
创建时间: 2017-09-16T14:03:58Z
项目社区:https://github.com/itsziget/ssh-tunnel

开源协议:MIT License

下载


Description

This Docker image helps you forward ports of containers to remote servers.
If you publish the forwarded ports of a container, you can use it to forward your local ports to the remote server through the container.
Sometimes the previous way is undesired, since you want to keep the local ports free.
In this case you should keep /etc/hosts up to date even if the ip addresses of the containers can be changed. This can be solved using itsziget/hosts-gen and itsziget/hosts-updater.
With the help of these images you can have a VPN-like solution. So you can even mount a samba drive from a remote private network.

First of all you have to copy your SSH public key to each server you want to ssh.

The simplest example when a local port is forwarded to a remote server’s local port. For instance, you have a remote web server with a virtual host accessible only from localhost or a MySQL server which is not published.

  1. version: "2"
  2. services:
  3. remote-mysql:
  4. image: itsziget/ssh-tunnel
  5. volumes:
  6. - "${HOME}/.ssh/id_rsa.pub:/root/.ssh/id_rsa.pub"
  7. - "${HOME}/.ssh/id_rsa:/root/.ssh/id_rsa"
  8. environment:
  9. VIRTUAL_HOST: remote-mysql
  10. TUNNEL_HOST: user@remotehost:2200
  11. TUNNEL_REMOTES: "127.0.0.1:3306"

In the above example the container’s port 3306 is forwarded remotehost’s local port 3306 via SSH tunnel through the port 2200.
If you use the mentioned itsziget/hosts-gen and the updater, you will be able to access to the remote mysql using “remote-mysql” as host name.

The other case when you have a web server inside a remote private network and you wish to access it from the local machine.

  1. version: "2"
  2. services:
  3. remote-web:
  4. image: itsziget/ssh-tunnel
  5. volumes:
  6. - "${HOME}/.ssh/id_rsa.pub:/root/.ssh/id_rsa.pub"
  7. - "${HOME}/.ssh/id_rsa:/root/.ssh/id_rsa"
  8. environment:
  9. VIRTUAL_HOST: first.remote.host,second.remote.host
  10. TUNNEL_HOST: user@remotehost:2200
  11. TUNNEL_REMOTES: |
  12. first.remote.host:443
  13. second.remote.host:443
  14. second.remote.host:80

The “|” (pipe) character allows you to set a multiline text to a variable. If you have multiple host on the remote server, you can list them line by line. Each line is a host and port separated by colon.

There is a trickier way to use the SSH tunnel. If you forward the SSH port of a container to a remote SSH port, you can tunnel an other container’s port through that. This way you can access a server’s local port inside a remote private network which is inaccessible directly via SSH.

  1. version: "2"
  2. services:
  3. privatemysql-ssh:
  4. image: itsziget/ssh-tunnel
  5. volumes:
  6. - "${HOME}/.ssh/id_rsa.pub:/root/.ssh/id_rsa.pub"
  7. - "${HOME}/.ssh/id_rsa:/root/.ssh/id_rsa"
  8. environment:
  9. TUNNEL_HOST: user@publichost:2200
  10. TUNNEL_REMOTES: "mysql.private.lan:22"
  11. expose:
  12. - 22
  13. privatemysql:
  14. image: itsziget/ssh-tunnel
  15. volumes:
  16. - "${HOME}/.ssh/id_rsa.pub:/root/.ssh/id_rsa.pub"
  17. - "${HOME}/.ssh/id_rsa:/root/.ssh/id_rsa"
  18. links:
  19. - privatemysql-ssh
  20. environment:
  21. VIRTUAL_HOST: mysql.private.lan
  22. TUNNEL_HOST: user@privatemysql-ssh:22
  23. TUNNEL_REMOTES: "127.0.0.1:3306"

Now mysql.private.lan is accessible on port 3306 from your local machine.

As I mentioned at the top of the README, this image is can be used to mount a private samba drive.

  1. version: "2"
  2. services:
  3. privatesamba:
  4. image: itsziget/ssh-tunnel
  5. volumes:
  6. - "${HOME}/.ssh/id_rsa.pub:/root/.ssh/id_rsa.pub"
  7. - "${HOME}/.ssh/id_rsa:/root/.ssh/id_rsa"
  8. environment:
  9. VIRTUAL_HOST: privatesamba
  10. TUNNEL_REMOTES: |
  11. samba.private.lan:137
  12. samba.private.lan:138
  13. samba.private.lan:139
  14. samba.private.lan:445

Here every ports used by a samba server are forwarded to the private samba server. Now type the following in a file browser’s address bar:

smb://privatesamba/sharedfolder

I assume you have many different private server. You can shorten the definitions by inheriting one container’s definition:

  1. version: "2"
  2. services:
  3. publicserver:
  4. image: itsziget/ssh-tunnel
  5. volumes:
  6. - "${HOME}/.ssh/id_rsa.pub:/root/.ssh/id_rsa.pub"
  7. - "${HOME}/.ssh/id_rsa:/root/.ssh/id_rsa"
  8. environment:
  9. TUNNEL_HOST: user@publichost:2200
  10. TUNNEL_REMOTES: "127.0.0.1:2200"
  11. privatemysql-ssh:
  12. extends:
  13. service: publicserver
  14. environment:
  15. TUNNEL_HOST: user@publichost:2200
  16. TUNNEL_REMOTES: "mysql.private.lan:22"
  17. expose:
  18. - 22
  19. privatemysql:
  20. extends:
  21. service: publicserver
  22. links:
  23. - privatemysql-ssh
  24. environment:
  25. VIRTUAL_HOST: mysql.private.lan
  26. TUNNEL_HOST: user@privatemysql-ssh:22
  27. TUNNEL_REMOTES: "127.0.0.1:3306"
  28. privatesamba:
  29. extends:
  30. service: publicserver
  31. environment:
  32. VIRTUAL_HOST: privatesamba
  33. TUNNEL_REMOTES: |
  34. samba.private.lan:137
  35. samba.private.lan:138
  36. samba.private.lan:139
  37. samba.private.lan:445
  38. privateweb:
  39. extends:
  40. service: publicserver
  41. environment:
  42. VIRTUAL_HOST: web.private.lan
  43. TUNNEL_REMOTES: |
  44. web1.private.lan:443
  45. web2.private.lan:443
  46. web2.private.lan:80

In the above case the first container’s definition is the base of the others. You need to redefine only the differences.

As you can see it is not so far from a VPN. You have to define more manually, but you get more control over the network. Your local private network and some ports of some remote machines from the remote network will be available at the same time. And this is more than a simple SSH tunnel, since your local ports will be untouched if you want.