I have containers running in docker-compose networks on a remote VPS. I would like to be able to access a database running in one of these containers from my localhost.
Eg, if the containers app
and db
are in a container network on the VPS, I want to access db:5432
from my machine's localhost:5432
. Kubernetes' CLI allows this with kubectl port-forward <service-name> 5432:5432
.
Are there any existing solutions to achieve this effect within existing unix commands and Docker's API? My searching around the Internet hasn't yielded any CLI to do this.
Cheers.
Edit: I have accepted Ali Tou's answer, however, it would be great to see a solution which does not require restarting a container to re-configure its ports to expose them onto the host.
Edit 2: I found a clever method for forwarding traffic in the Docker network using socat in a tangentially-related post https://stackoverflow.com/a/42071577/12406113. This is, however, publicly exposing the port instead of creating a tunnel, such as in the SSH solution.