Forward port

2 snippets across 2 stacks - Bash & Linux, Docker

Also written as port forward

SHBash & Linux

Secure Shell Remote Login

SH · Networking
Syntax
ssh [options] user@host
Example
ssh deploy@10.0.1.50
ssh -i ~/.ssh/prod_key.pem ec2-user@server.example.com
ssh -L 5432:db-host:5432 bastion@jump.example.com

Note -i specifies a private key file. -L sets up local port forwarding (the example tunnels a remote Postgres port to localhost:5432). -p sets a non-standard SSH port. Add -v for verbose debugging of connection issues.

DKDocker

Port Mapping (-p)

DK · Run Options
Syntax
docker run -p <host_port>:<container_port> <image>
docker run -p <host_ip>:<host_port>:<container_port> <image>
Example
docker run -d -p 8080:3000 myapp:1.0
docker run -d -p 127.0.0.1:5432:5432 postgres:16

Note The format is always host:container. Binding to 127.0.0.1 restricts access to localhost only - critical for databases you do not want exposed on the network. Without an IP, Docker binds to 0.0.0.0 (all interfaces).

Frequently asked questions

How does Bash & Linux handle forward port?
This task is covered in 2 stacks on this page: Bash & Linux, Docker. The "Secure Shell Remote Login" snippet in Bash & Linux uses `ssh [options] user@host`.
Which command does the Bash & Linux example use?
The "Secure Shell Remote Login" snippet uses `ssh [options] user@host`, from the Networking section of the Bash & Linux cheat sheet.
Which stacks cover "forward port" on this page?
Bash & Linux, Docker. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Secure Shell Remote Login": -i specifies a private key file. -L sets up local port forwarding (the example tunnels a remote Postgres port to localhost:5432). -p sets a non-standard SSH port. Add -v for verbose debugging of connection issues.