Imagine you're messing around in a container, and you install some stuff, add some config, and now it's time to load up your client and check it out! Oh wait, you forgot to forward ports when you created the container! Fear not, all is not lost, for in the world of pipes, and streams, there is always a way to do something disgusting.
Example Dockerfile included will install Nginx, and socat in a container, and make Nginx run in foreground mode. To build, and run:
docker build .
docker run -it --name socat-nginx <image_id>
This will give you a container running Nginx on port 80, but with no forwarded ports.
I'll just get right into it. Forgive me:
socat TCP-LISTEN:8080,reuseaddr,fork 'EXEC:docker exec -i socat-nginx "socat STDIO TCP-CONNECT:localhost:80"'
So what's this doing?
TCP-LISTEN:8080
: socat running locally will accept connections of port 8080reuseaddr,fork
: When accepting a connection, socat will fork to handle it. This means that it can handle parallel connections, and won't die after the first connectionEXEC:...
: Instructs socat to execute a command, and forward the accepted connection (in this case, ourTCP-LISTEN
) to the standard input/output. This will happen every time socat accepts a connectiondocker exec -i socat-nginx
: Execute a new command in our running container.-i
lets Docker know that we want to immediately attach (formard stardard in/out/err)socat STDIO
: In the Docker container, run socat and accept standard input/output as the stream sourceTCP-CONNECT:localhost:80
: Open a connection to port 80 in the container, and stream stdin to it's write buffer. Socat will read, and stream from the port to stdout (which is passed back through Docker, to local socat, and out the TCP port we've given)
This is worthless on it's own. What is socat-nginx, where did you include the Dockerfile?