In some instances we start a container, but it will default to immediately running a program within and prevent immediate shell access.
This is possible since Docker version1.3
Assuming that the container is already running (for example serving a web browser) and you can’t access the shell commands we can gain shell (bash, ssh) through a new terminal and use the exec command to create a bash instance in the container. For this we need either the CONTAINER ID (e.g. 882d81d945c6 dd) or its NAME (e.g. intelligent_bhaskara) which can be obtained by the command docker ps.
Then, opening a new Terminal we issue the command using either name as shown below:
docker exec -it intelligent_bhaskara /bin/bash or with
CONTAINER ID
docker exec -it 882d81d945c6 /bin/bash
This will give immediate access to inside the container. (Of course the container must have /bin/bash as part of its instructions.)
If the terminal is “dormant” (i.e. --rm had not been part of the original command) it can be found by using docker container ls -a and then “awakened” (i.e. restarted) with docker start intelligent_bhaskara before the exec command is given.
Note
If you don’t mind closing the current container and reconnect you could by-pass the default running program by providing an entry point as discussed in this post: Docker notes – Bypass entry point
Links
- How to Get Shell Access to Running Docker ContainerHow to Get Shell Access to Running Docker Container: https://tecadmin.net/get-shell-access-to-docker-container/
- Docker reference (Exec): https://docs.docker.com/engine/reference/commandline/exec/#try-to-run-docker-exec-on-a-paused-container
- How To SSH Into A Running Docker Container And Run Commands: https://phoenixnap.com/kb/how-to-ssh-into-docker-container