Manage Docker Containers by shell without SSH Login

Docker,InfrastructureDocker

Basically, there are 2 options to control containers with shells without SSH login.


Option 1. Use -t (pseudo-tty) option without ssh

Example: Create a new nginx container named “webnode“, and run it interactively. By using “-t" option that simulates a terminal, you can use shell (bash) without ssh.

$ docker container run -it --name webnode nginx bash

Result:

$ docker container run -it --name webnode nginx bash
root@43d69c39158d:/# ls
bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@43d69c39158d:/#

However, if you exit a container with the “exit" command, the container is stopped.

root@43d69c39158d:/# exit
exit
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES


Option 2. Use bash without container down

Example:

1. Start the “webnode" container.

$ docker container start webnode
webnode
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43d69c39158d nginx "/docker-entrypoint.…" 9 minutes ago Up 17 seconds 80/tcp webnode

2. With “docker container exec" command, you can add shell commands to the running “webnode" container.

$ docker container exec -it webnode bash
root@43d69c39158d:/# echo "Hi"
Hi

3. Even though you exit the container, it is still running.

root@43d69c39158d:/# exit
exit
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43d69c39158d nginx "/docker-entrypoint.…" 10 minutes ago Up About a minute 80/tcp webnode