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?
Port mapping is used to access the services running inside a Docker container. We open a host port to give us access to a corresponding open port inside the Docker container. Then all the requests that are made to the host port can be redirected into the Docker container.
Surprisingly or not, neither Docker nor Podman support exposing multiple containers on the same host's port right out of the box. Example: docker-compose failing scenario with "Service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash."
To make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.
You'll have to run the container, exposing this port, and then to set up a port forwarding in Virtualbox. If I use Wordpress as an example: docker run -p 80:80 --name website -d wordpress Virtual Box -> your docker VM (usually called default) -> Network -> Adapter 1 -> port forwarding -> create a mapping from host 8080 to guest 80
Port mapping makes the processes inside the container available from the outside. While running a new Docker container, we can assign the port mapping in the docker run command using the -p option: The above command launches an httpd container and maps the host’s port 81 to port 80 inside that container.
The most commonly used method to move Docker container to another host, is by migrating the image linked to that container. For the container that has to be moved, first its Docker image is saved into a compressed file using ‘docker commit’ command. The image that is generated is compressed and moved into the new host machine.
We open a host port to give us access to a corresponding open port inside the Docker container. Then all the requests that are made to the host port can be redirected into the Docker container. Port mapping makes the processes inside the container available from the outside.
Install socat
in your container and at startup run
socat TCP-LISTEN:3306,fork TCP:B-IP:3306 &
This 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 it
$ yum install -y socat $ apt install -y socat $ apk add socat
Edit-1
You can even do this by not touching your original container
Dockerfile
FROM alpine RUN apk update && apk add socat
Build the file as below
docker build -t socat .
Now run a container from same
docker run --name mysql-bridge-a-to-b --net=container:<containerAid> socat socat TCP-LISTEN:3306,fork TCP:BIP:3306
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.
You can simply run the container with network mode equal to host.
docker run --network=host ...
In that case, from the container point of view, localhost or 127.0.0.1 will refer to the host machine. Thus if your db is running in another container B that listens on 3306, an address of localhost:3306 in container A will hit the database in container B.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With