EN
Docker - connect via ssh with run container
10 points
In this short article, we would like to show how to get ssh connection to the existing process - to the run Docker container.
Quick solution:
xxxxxxxxxx
1
docker exec -i -t MY_CONTAINER_NAME /bin/bash
or:
xxxxxxxxxx
1
docker exec -i -t MY_CONTAINER_ID /bin/bash
Note: ssh connection we understand as command line for Docker process.
Example Docker processes listing (run containers):
xxxxxxxxxx
1
$ docker ps
2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3
dcca9aecbcff postgres "docker-entrypoint.s…" 5 hours ago Up 2 hours 0.0.0.0:5432->5432/tcp postgres-shop
Example connection to postgress-shop
process:
xxxxxxxxxx
1
$ docker exec -i -t postgres-shop /bin/bash
2
[root@dcca9aecbcff /]# ls -al
3
total 88
4
drwxr-xr-x 1 root root 4096 Feb 5 09:55 .
5
drwxr-xr-x 1 root root 4096 Feb 5 09:55 ..
6
-rwxr-xr-x 1 root root 0 Feb 5 09:54 .dockerenv
7
lrwxrwxrwx 1 root root 7 Nov 13 01:53 bin -> usr/bin
8
drwxr-xr-x 5 root root 340 Feb 5 10:08 dev
9
drwxr-xr-x 1 root root 4096 Feb 5 09:54 etc
10
drwxr-xr-x 2 root root 4096 Apr 11 2018 home
11
lrwxrwxrwx 1 root root 7 Nov 13 01:53 lib -> usr/lib
12
lrwxrwxrwx 1 root root 9 Nov 13 01:53 lib64 -> usr/lib64
13
drwxr-xr-x 2 root root 4096 Apr 11 2018 media
14
drwxr-xr-x 2 root root 4096 Apr 11 2018 mnt
15
drwxr-xr-x 1 root root 4096 Feb 4 00:34 opt
16
dr-xr-xr-x 479 root root 0 Feb 5 10:08 proc
17
dr-xr-x--- 1 root root 4096 Feb 5 09:56 root
18
drwxrwxrwx 1 root root 4096 Feb 5 10:08 run
19
lrwxrwxrwx 1 root root 8 Nov 13 01:53 sbin -> usr/sbin
20
drwxr-xr-x 2 root root 4096 Apr 11 2018 srv
21
dr-xr-xr-x 13 root root 0 Feb 5 10:08 sys
22
drwxrwxrwt 1 root root 4096 Feb 5 10:09 tmp
23
drwxr-xr-x 1 root root 4096 Feb 4 00:33 usr
24
drwxr-xr-x 1 root root 4096 Nov 13 01:54 var
25
[root@dcca9aecbcff /]#
Where:
-i
means keeping opened STDIN even terminal is detached,-t
means the container will use pseudo-tty,postgres-shop
is the name of the container to which we want to get the ssh connection (the name was set by--name
attribute during container creating),/bin/bash
used tty.