How to ssh or setup a sftp connection directly to a docker container running on a remote machine

Hi, I’m relatively new to docker and could use some help here. I want to setup PyCharm locally so that I can upload, download and debug the project easily in a docker container that I created on a remote machine. I’m sure a lot of people have a similar setup, so feel free to suggest an existing thread if this seems like a repeated question to you. With my limited knowledge, I haven’t found a good solution so far.

I am working in PyCharm, which allows me to set SFTP connections to the remote GPU machines, here is an example I found online, where I need an SFTP hostname, default port:22, a username and a password to be able to remotely debug on the GPU machine.

Now I have a docker container running on that remote GPU machine, and I want to be able to set up a similar SFTP connection to directly upload, and download code into the docker image on the remote GPU machine. Ideally, they should share the same SFTP host, but having a different port and username. I think I saw it being done somewhere, but I’m not sure how to do it.

I have tried Dockerize an SSH service, but I am not sure how to make that work in my situation, since the docker container is on a remote machine that requires an extra layer of credentials.

Any help would be much appreciated!

The host itself probably has port 22 open for the ability to remotely execute commands via SSH. You can scp/sftp your source files into a directory on the host that is bound by a mounted volume to the container on that machine.

example:
docker run -d -v /some/host/directory:/container/directory some/container:latest

Another idea is to bind a port (other than the taken port 22 of the host) to the host machine into port 22 of the container and configure your SSH client (pycharm) to connect to that high port number. For example, bind port 2222:22 to the host machine, configure pycharm to port 2222.

example:
docker run -d -p 2222:22 some/container:latest

Thank you for your advice.

For the option 1, I would like to be able to debug using pycharm in the docker environment directly, mount a volume to the container does not allow me to access the container for debugging in the local pycharm environment. Please let me know if I misunderstood this.

For the option 2, I actually tried this option, are you suggesting that with this method, I would be able to set connection from local PyCharm to the remote docker directly using the same host name , same credential, with 2222 instead of 22 as the port? I tried this before, it did not work for me.

It seems like my server has some setups that somehow blocked docker container outbound signals, which might have caused this issue. I’ll talk to the server operators first, then try the second option again and update on this thread. Thanks for the suggestions.

OK. It turns out I needed to add --network=host option while building the docker image to allow the docker image to connect to the network. However, after being able to have network access, specifying docker port 2222:22 did not work for remote SSH session.