SSH directly into a docker container

I have some docker juicers and now I want to access one using ssh. Thats working I got a ssh connection to a docker container.

But now I have a problem, I don't know which user can I access this container with?

I tried this with both users I have on the host machine (web and root). But they don't work. What to do?

+3


source to share


3 answers


You can go directly to the working container with:

$ docker exec -it myContainer /bin/bash

      

You can get wrapped on a container that doesn't work:



$ docker run -it myContainer /bin/bash

      

This is the preferred method for getting a shell in a container. Running an SSH server is not considered good practice and while there are some use cases out there, it should be avoided whenever possible.

+4


source


If you want to connect directly to the Docker container without connecting to the docker host, your Dockerfile should include the following:

# SSH login fix. Otherwise user is kicked off after login
RUN echo 'root:pass' | chpasswd
RUN mkdir /var/run/sshd
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

      

Then use docker run with the -p and -d flags. Example:



docker run -p 8022:22 -d your-docker-image

      

You can contact:

ssh root@your-host -p8022

      

+4


source


A more robust solution is dumping nsenter to your server and then backtracking and starting docker-enter

. This way, you don't have to run multiple processes in a container (ssh + server whatever it is for) or worry about all the extra overhead of ssh users and the like (not to mention security concerns).

-1


source







All Articles