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.
docker run -p<host_port>:<container_port><image>docker run -p<host_ip>:<host_port>:<container_port><image>
Example
docker run -d-p8080:3000 myapp:1.0docker run -d-p127.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.