Is it possible to configure Docker to use a remote host for everything?
Enable ssh into a live docker container
containers - How to SSH into Docker? - Stack Overflow
VS Code: connect a docker container in a remote server - Stack Overflow
Videos
Here is my scenario. I have a Windows 10 professional deployment running as a guest under KVM. The performance of the Windows guest is sufficient. However, I need to use docker under Windows (work thing, no options here) and even though I can get it to work via configuring the KVM, the performance is no longer acceptable.
If I could somehow use the docker commands so that they would perform all the actions on a remote host, it would be great, because then I could use the KVM host to run docker, and use docker from within the Windows guest. I know it is possible to configure access to docker by exposing a TCP port etc but what I don't know is if stuff like port forwarding could work if I configured a remote docker host.
There's also the issue about mounting disk volumes. I can probably get away by using docker volumes to replace that, but that's not the same as just mounting a directory, which is what devcontainers do for example.
I realise I am really pushing for a convoluted configuration here, so please take the question as more of an intellectual exercise than something I insist on doing.
Firstly you need to install a SSH server in the images you wish to ssh-into. You can use a base image for all your container with the ssh server installed.
Then you only have to run each container mapping the ssh port (default 22) to one to the host's ports (Remote Server in your image), using -p <hostPort>:<containerPort>. i.e:
docker run -p 52022:22 container1
docker run -p 53022:22 container2
Then, if ports 52022 and 53022 of host's are accessible from outside, you can directly ssh to the containers using the ip of the host (Remote Server) specifying the port in ssh with -p <port>. I.e.:
ssh -p 52022 myuser@RemoteServer --> SSH to container1
ssh -p 53022 myuser@RemoteServer --> SSH to container2
Notice: this answer promotes a tool I've written.
The selected answer here suggests to install an SSH server into every image. Conceptually this is not the right approach (https://docs.docker.com/articles/dockerfile_best-practices/).
I've created a containerized SSH server that you can 'stick' to any running container. This way you can create compositions with every container. The only requirement is that the container has bash.
The following example would start an SSH server exposed on port 2222 of the local machine.
$ docker run -d -p 2222:22 \
-v /var/run/docker.sock:/var/run/docker.sock \
-e CONTAINER=my-container -e AUTH_MECHANISM=noAuth \
jeroenpeeters/docker-ssh
$ ssh -p 2222 localhost
For more pointers and documentation see: https://github.com/jeroenpeeters/docker-ssh
Not only does this defeat the idea of one process per container, it is also a cumbersome approach when using images from the Docker Hub since they often don't (and shouldn't) contain an SSH server.
I resolved this problem by switching to the remote server's Docker context on my local machine:
docker context create some-context-label --docker "host=ssh://user@remote_server_ip"
docker context use some-context-label
docker ps
# A list of remote containers on my local machine! It works!
After that:
- Connect via Remote-SSH to the container server
- Right click relevant container -> the "Attach Visual Studio Code"
That works for me.
(Note: One would think that I should be able to just use my local VSCode (skip step 1) to connect to said remote container after switching my local context, but VSCode complains Failed to connect. Is docker running? in the Docker control pane.)
This might sound very strange, but for me, I had to open a folder on the remote SSH server prior to using the Remote Containers extension in VS Code. If I didn't do that, then it would constantly try to find the docker service running locally, even though the terminal tab was connected to the remote SSH server.
This seems very weird, because if you're conncted via SSH in VS Code, then the extension should assume you're trying to attach to the container on the remote server. Shouldn't have to open a remote folder first.
By "opening a folder" on the remote server, the Remote Containers extension was then able to attach VS code to the container running on the remote SSH server. I didn't have to do any of the steps in any of those articles. Just simply use Remote SSH to connect VS Code remotely via SSH, open a folder, and then use Remote Containers.