Skip to content

Instantly share code, notes, and snippets.

@lambdaydoty
Created July 8, 2020 02:27
Show Gist options
  • Save lambdaydoty/56a1838bce527dbd48c4bbde4be01d46 to your computer and use it in GitHub Desktop.
Save lambdaydoty/56a1838bce527dbd48c4bbde4be01d46 to your computer and use it in GitHub Desktop.
How to access host port from a docker container
## `How to access host port from a docker container`
## `Access host's ssh tunnel from docker container`
## `Prevent from using --net=host`
## ...
# +-------------------+
# | |
# | |
# | local container |
# | +---+
# +-------------------+ |172.10.0.1
# |
# +-------------------+ <-+ +----------------------+
# | | | |
# | | | |
# | +^-----------------+ |
# | local host A +-v---------------v+ remote server B |
# | | | |
# +-------------------+12345 98765+----------------------+
#
# 1. Make sure have enabled local host A openssh server's `GatewayPorts` options
# (to `yes` or `clientspecified`)
sudo vim /etc/ssh/sshd_config
sudo systemctl restart ssh.service
# 2. Get the IP address of the `docker0` interface on your local host A
ip addr show docker0 # say 172.17.0.1
# 3. Establish a tunnel either from local host A or from remote server B
ssh -N -L 172.17.0.1:12345:localhost:98765 name@remoteHostB # from local host A
# or
ssh -N -R 172.17.0.1:12345:localhost:98765 name@localHostA # from remote server B
# 4. Now you can access the remote server B's port 98765
# from local container via 172.17.0.1:12345