Connecting to host port from Docker container
- 11
- Oct
In my case I wanted to connect to a MySQL database that my laptop was SSH’d to.
# I had port forwarding in my ssh connection to fwd port 3306, the mysql port.
cat ~/.ssh/config
Host memsql
HostName
User ubuntu
LocalForward 3306 127.0.0.1:3306
# on my laptop I start the ssh connection (use -N if you don’t actually want the ssh terminal for the connection)
ssh memsql
# On the laptop run Docker with net=host:
docker run -it –net=host ….
# Inside Docker get the IP of the host:
route
# Look for Gateway in output, in my case it is: 192.168.65.1
# this is the IP of your host from Docker’s perspective
# Command inside to test if Docker has access to the port on localhost:
nc
nc 192.168.65.1 3306
# output something like
4.7.8?,Mtgh???!?s%T6-oGJ%YB2mysql_native_password