I commonly see solutions that expose a docker container's port to the host.
In my case I want to forward a local port from one container, to another.
Let's say I run a service on container A that has a hard-coded configuration to access db on localhost 3306. But I want to run the db server on container B.
What is the best way to port-forward from A-localhost:3306 to B-IP:3306?
Install
socat
in your container and at startup runThis will listen locally on your 3306 and pass any traffic bidirectionally to
B-IP:3306
. socat is available in package named socat. So you will run any of the below commands to install itEdit-1
You can even do this by not touching your original container
Dockerfile
Build the file as below
Now run a container from same
This will run this container on A's network. So when it listens on A's network the
localhost:3306
will become available in A even though A container was not touched.