Skip to content

About

Create a local port forwarding with `ssh -L` using docker containers

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

9 Commits

Folders and files

Repository files navigation

ssh-tunnel-with-docker

Create a local port forwarding with ssh -L using docker containers

Senario

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.

The layout of the compose file

layout-dark

How to test the Tunnel

Task: download the script.py from the webserver container into clinet container.

  1. First bring up the containers
docker compose -f company-compose.yaml up -d
Screenshot_20260731_222007
  1. Now log in into the client by
docker exec -it client /bin/bash
  1. Let's test if we could access webserver:3000 or get anything from localhost:3000
Screenshot_20260731_222748
  1. Run the following command
ssh -L localhost:3000:webserver:3000 proxy -fN

This 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)

Screenshot_20260731_222457
  1. Create a HTTP GET request to download the script.py using the tunnel from webserver:3000.
Screenshot_20260731_222923

Easy and very beautiful.

About

Create a local port forwarding with `ssh -L` using docker containers

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages