Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH directly into a docker container

Tags:

docker

ssh

debian

i've got some docker conatiners and now I want to access into one with ssh. Thats working I got a connection via ssh to the docker container.

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

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

like image 578
Felix Avatar asked May 16 '15 09:05

Felix


People also ask

How do I connect to a Docker container?

Use docker attach to attach your terminal's standard input, output, and error (or any combination of the three) to a running container using the container's ID or name. This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.

How do I connect to a host container?

TLDR. Use --network="host" in your docker run command, then 127.0. 0.1 in your docker container will point to your docker host. Note: This mode only works on Docker for Linux, per the documentation.


2 Answers

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.

like image 82
L0j1k Avatar answered Sep 16 '22 15:09

L0j1k


If you want to connect directly into a 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 -p and -d flags. Example:

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

You can connect with:

ssh root@your-host -p8022
like image 32
miguelghz Avatar answered Sep 18 '22 15:09

miguelghz