Search code examples
dockercontainerslxc

How to SSH into Docker?


I'd like to create the following infrastructure flow:

How can that be achieved using Docker?


Solution

  • 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