dockercontainerslxc

How to SSH into Docker?


I'd like to create the following infrastructure flow, where I have three Docker containers on a remote server and want admin and standard users able to use the same login for those resources. I expect the admin to ssh into a different IP than the standard user(s).

an image depicting the above described infrastructure originally hosted on lucidchart.com

How can that be achieved using Docker? I need to ssh into the container.


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