I am trying to get a container built that has ports forwarded, such that code running in the container can access a remote db for instance. So I put the line
CMD ssh -L 27017:localhost:27017
in my dockerfile and then run it, but in no case do I see ports forwarded (I tried RUN instead of cmd, and tried both interactively running the container or using -d , but in the former case I don't have forwarding and in the latter case the container exits soon after starting, even after tacking && /bin/bash to the end of the ssh command). The only way I have succeeded doing this is doing an interactive run
$docker run -it --name cont_name im_name /bin/bash
and then from the interactive shell doing the ssh necessary for port forwarding (which now ties up the shell). Then from another window on my local host, I 'get inside' the first container using
$ docker exec -it cont_name bash
where I now indeed see ports forwarded . Is there a better/automatic way to do this? 'screen' seems to be a hassle to get running in a container.
I think what you want to do is "bind" the port from the container to the host. First thing you need to know is if the port has been exposed via
EXPOSE
in the docker container you plan on using. The next thing would be to add this to thedocker run...
:Let's assume it's a mysql instance that you are using, the port that is exposed is 3306, so you would bind that to the host on the same port or whatever port you prefer on the host.