Want to ssh into a running docker container running inside CentOs Image

1.8k Views Asked by At

I want to ssh into a running CentOs container running inside a CentOs image. My aim is to SSH into container and install applications into it. Please provide me appropriate steps for this.

Ran below command

 yum install openssh-server

Used below approach but did not get the result.

How to get into a docker container?

2

There are 2 best solutions below

2
On

You don't need to use SSH or install anything on a running container to get inside, you can use docker directly :

docker exec -it myContainer /bin/bash

Where myContainer is the name or the ID of the running container you need to connect to.

You will be then connected as root in CentOS, and you will be able to do what you need.

To leave it and go back to your host, you need to type CTRL + P - Q.

1
On

You can exec in the container docker exec -it myContainer /bin/bash mentioned above. But if you do not want to allow user to exec into container and then you can do this:

  1. Create a ssh key and store in file (user ssh keygen to generate key)
  2. Modify your docker image and store the key in authorized_key file in docker image. (take care of permission)
  3. Use the that key -i .ssh_key.pem to ssh in docker container.