Skip to content

Instantly share code, notes, and snippets.

@nhthai2005
Last active July 17, 2021 16:16
Show Gist options
  • Save nhthai2005/1f109a6b9ea2e2998feadc2a44a249ec to your computer and use it in GitHub Desktop.
Save nhthai2005/1f109a6b9ea2e2998feadc2a44a249ec to your computer and use it in GitHub Desktop.
Exposing a port on a live Docker container

docker exec -it <containterid> ssh -R5432:localhost:5432 <user>@<hostip>

By default, OpenSSH only allows connecting to remote forwarded ports from the server host. However, the GatewayPorts option in the server configuration file sshd_config can be used to control this. The following alternatives are possible:

GatewayPorts no

This prevents connecting to forwarded ports from outside the server computer.

GatewayPorts yes

This allows anyone to connect to the forwarded ports. If the server is on the public Internet, anyone on the Internet can connect to the port.

GatewayPorts clientspecified

This means that the client can specify an IP address from which connections to the port are allowed. The syntax for this is:

ssh -R 52.194.1.73:8080:localhost:80 host147.aws.example.com

In this example, only connections from the IP address 52.194.1.73 to port 8080 are allowed.

FROM debian:7
RUN apt-get update && \
apt-get -y install socat && \
apt-get clean
USER nobody
CMD socat -dddd TCP-LISTEN:1521,reuseaddr,fork TCP:db:1521
# docker run \
--rm \
-p $PORT:1234 \
verb/socat \
TCP-LISTEN:1234,fork \
TCP-CONNECT:$TARGET_CONTAINER_IP:$TARGET_CONTAINER_PORT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment