19

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?

Felix
  • 5,452
  • 12
  • 68
  • 163

7 Answers7

15

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.

L0j1k
  • 12,255
  • 7
  • 53
  • 65
  • I only need console for accessing with Jenkins in this container. – Felix May 16 '15 at 10:01
  • I am newbie in docker. now using Jenkin in one of the docker , but what I observed, Jenkins don't have a separate IP. I am accessing site using host IP. – Arun Aug 02 '20 at 06:36
13

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
miguelghz
  • 375
  • 3
  • 6
  • 1
    Is it possible to get more than one ssh session per container? Right now I can connect via ssh but only from one place. When I'm trying connect from another place at the same time I just get the following error - ```ssh_exchange_identification: read: Connection reset by peer``` – ALex_hha Aug 04 '16 at 16:02
  • It doesn't work out well with Debian. I am using Debian and it seems that I'm getting a permission denied even though I'm using the Dockerfile you gave @miguelghz – Zeid Tisnes Mar 09 '20 at 01:30
0

1.issue the command docker inspect (containerId or name)

You will get a result like this

   "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "my_bridge": {
                    "IPAMConfig": {
                        "IPv4Address": "172.17.0.20"
                    },
                    "Links": null,
                    "Aliases": [
                        "3784372432",
                        "xxx",
                        "xxx2"
                    ],
                    "NetworkID": "ff7ea463ae3e6e6a099e0e044610cdcdc45b21f7e8c77a814aebfd3b2becd306",
                    "EndpointID": "6be4ea138f546b030bb08cf2c8af0f637e8e4ba81959c33fb5125ea0d93af967",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.20",
                    "IPPrefixLen": 24,
...

  1. read out and copy the IP address from there, connect to it via command ssh existingUser@IpAddress , eg someExistingUser@172.17.0.20. If the user doesn't exist, create him in the guest image, preferably with the sudo privileges. Probably don't use a root user directly, since as far as I know, that user is preset for connecting to the image via ssh keys, or has a preset password and changing it would probably end up in not being able to ssh connect to the image terminal via a regular way of doing it docker exec -it containerName /bin/bash or docker-compose exec containerName /bin/bash
FantomX1
  • 1,577
  • 2
  • 15
  • 23
0

For some case, enabling SSH in docker container is useful, specially when we want to test some scripts.

The link bellow give a good example how to create and image with ssh enabled and how to get it's IP and connect to it.

Here

Yassine Khachlek
  • 1,134
  • 12
  • 17
0

If a true SSH connection into the container is needed (i.e. to allow isolated access over the internet), this image from the linuxserver.io guys could be a great solution: https://hub.docker.com/r/linuxserver/openssh-server

-1

Much more robust solution is pulling down nsenter to your sever, then sshing in and running docker-enter from there. That way you don't need to run multiple processes in the container (ssh server + whatever the container is for), or worry about all the extra overhead of ssh users and such (not to mention security concerns).

Sean King
  • 203
  • 1
  • 5
  • Thats more a security fail as direct ssh . because there are many other dockers in the Maschine. A simple SSH should Work .... Only the authentication is a Problem – Felix May 16 '15 at 12:59
  • The guy who created the `nsenter` docker image actually [recommends](https://github.com/jpetazzo/nsenter/issues/19#issuecomment-61248200) using `docker exec` over `nsenter`. Since the introduction of `docker exec`, this is considered best practice. See also [this question](http://stackoverflow.com/questions/27873312/docker-exec-versus-nsenter-any-gotchas). – L0j1k May 16 '15 at 21:19
-1

The idea behind containers is that a container runs a single process so that it can be monitored by the daemon. If this process stops || fails for some reason, it can be restarted depending on your preference in your config. An ssh server is a running process. Therefore, if you need ssh access to your setup, make an ssh server service, which can share Volumes with other containers that are running alongside it in the setup.

To open a shell on a container in a host directly:

Imagine you are on your PC at home and you have a remote machine that runs docker and has running containers, and you want to open a shell on the container directly without "stopping by" on the remote host: (The -t flag exposes tty)

ssh -t user@remote.host 'docker exec -it running_container_name /bin/bash'

If you are already on the host, like the accepted answer: (The -i interactive -t tty)

docker exec -it running_container_name /bin/bash
visualex
  • 736
  • 12
  • 17