Create a local port forwarding with ssh -L using docker containers
Imagine there is a test environment database on the servers of your company. Data is noticably large and you don't want to create a backup from the database and create the test env database on your local machine. Also you can't access the database from your local machine for obvious reasons. But there is an edge server in your company that has access to database AND you can ssh into this edge server; with this situation you can use ssh tunneling mechanism to use the edge server as a proxy and use ssh port forwarding cababilities to make your localhost:5432 to be the database server 5432 port. The setup in this repo is a mimic of what is described.
Task: download the script.py from the webserver container into clinet container.
- First bring up the containers
docker compose -f company-compose.yaml up -d
- Now log in into the
clientby
docker exec -it client /bin/bash- Let's test if we could access
webserver:3000or get anything fromlocalhost:3000
- Run the following command
ssh -L localhost:3000:webserver:3000 proxy -fNThis command creates a ssh tunnel in the background. With this command any request to localhost:3000 will pass the tunnel, and then the proxy server will create a request webserver:3000 and then it will return the response to localhost:3000. (the password is 1234)
- Create a HTTP GET request to download the
script.pyusing the tunnel fromwebserver:3000.
Easy and very beautiful.