Languages
[Edit]
EN

Docker - connect via ssh with run container

10 points
Created by:
troya
692

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:

docker exec -i -t MY_CONTAINER_NAME /bin/bash

or:

docker exec -i -t MY_CONTAINER_ID /bin/bash

Note: ssh connection we understand as command line for Docker process.

 

Practical example

Example Docker processes listing (run containers):

$ docker ps
CONTAINER ID       IMAGE          COMMAND                      CREATED           STATUS            PORTS                         NAMES
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: 

$ docker exec -i -t postgres-shop /bin/bash
[root@dcca9aecbcff /]# ls -al
total 88
drwxr-xr-x   1 root root  4096 Feb  5 09:55 .
drwxr-xr-x   1 root root  4096 Feb  5 09:55 ..
-rwxr-xr-x   1 root root     0 Feb  5 09:54 .dockerenv
lrwxrwxrwx   1 root root     7 Nov 13 01:53 bin -> usr/bin
drwxr-xr-x   5 root root   340 Feb  5 10:08 dev
drwxr-xr-x   1 root root  4096 Feb  5 09:54 etc
drwxr-xr-x   2 root root  4096 Apr 11  2018 home
lrwxrwxrwx   1 root root     7 Nov 13 01:53 lib -> usr/lib
lrwxrwxrwx   1 root root     9 Nov 13 01:53 lib64 -> usr/lib64
drwxr-xr-x   2 root root  4096 Apr 11  2018 media
drwxr-xr-x   2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x   1 root root  4096 Feb  4 00:34 opt
dr-xr-xr-x 479 root root     0 Feb  5 10:08 proc
dr-xr-x---   1 root root  4096 Feb  5 09:56 root
drwxrwxrwx   1 root root  4096 Feb  5 10:08 run
lrwxrwxrwx   1 root root     8 Nov 13 01:53 sbin -> usr/sbin
drwxr-xr-x   2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x  13 root root     0 Feb  5 10:08 sys
drwxrwxrwt   1 root root  4096 Feb  5 10:09 tmp
drwxr-xr-x   1 root root  4096 Feb  4 00:33 usr
drwxr-xr-x   1 root root  4096 Nov 13 01:54 var
[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.

 

  

Alternative titles

  1. Docker - ssh connection with run container
  2. Docker - connect to process command line
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Docker - useful wiki posts

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join