SSH directly into a docker container
You can drop directly into a running container with:
$ docker exec -it myContainer /bin/bash
You can get a shell on a container that is not running with:
$ docker run -it myContainer /bin/bash
This is the preferred method of getting a shell on a container. Running an SSH server is considered not a good practice and, although there are some use cases out there, should be avoided when possible.
docker: SSH access directly into container
Let's remember however that having ssh support in a container is typically an anti-pattern (unless it's your container only 'concern' but then what would be the point of being able to ssh in. Refer to http://techblog.constantcontact.com/devops/a-tale-of-three-docker-anti-patterns/ for information about that anti-pattern
How to SSH into Docker?
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
Using SSH keys inside docker container
Turns out when using Ubuntu, the ssh_config isn't correct. You need to add
RUN echo " IdentityFile ~/.ssh/id_rsa" >> /etc/ssh/ssh_config
to your Dockerfile in order to get it to recognize your ssh key.
Related Topics
Installing Multiple Versions of R
Getting Meteor 0.9.2 Build to Work Osx -> Linux
Docker: Ssh Access Directly into Container
Comma Separated Values to Single Inverted Quote and Comma Separated Values
Linux Assembler Error "Impossible Constraint in 'Asm'"
How to Find the Count of Multiple Words in a Text File
Zipping Without Creating Parent Folder
Is There Way to Delete Duplicate Header in a File in Unix
Automating Linux Ebs Snapshots Backup and Clean-Up
Shell Script for Process Monitoring
Read File Without Disk Caching in Linux
Difference Between Linux Kernel and Unix Kernel(Such as Freebsd) from Programmer's Point of View
What Really Is the "Linger Time" That Can Be Set with So_Linger on Sockets
Linux Sort Doesn't Work with Negative Float Numbers
How to Run Command During Docker Build Which Requires a Tty