How to Get Shell Access to Running Docker Container

Channel: Linux
Abstract: $ sudo docker exec -it happy_tecadmin bashwrite tutorials about how to create or delete docker containers. This tutorial will help you to get shell ac

In our previous docker tutorials your learned about how to Install Docker on CentOS or Install Docker on Debian based system. Also, write tutorials about how to create or delete docker containers. This tutorial will help you to get shell access to your running docker container.

#1. Using Docker Attach

You can get bash shell access in your docker container with attach command. But your docker container must be started with /bin/bash.

Use below syntax to get shell access of docker container.

$ sudo docker attach <CONTAINER ID/NAME>

For example, your docker container is running with id 76debad837d2 and name happy_tecadmin. Use one of the following commands with attach method.

Using Container ID:

$ sudo docker attach 76debad837d2

Using Container Name:

$ sudo docker attach happy_tecadmin
#2. Using Docker Exec

If docker container was not started with /bin/bash command. Then you can’t use attach command. Now, you need to use exec command to create bash in your container. Make sure you are using Docker version >= 1.3.

Use below syntax to get shell access of docker container.

$ sudo docker exec -it <CONTAINER ID/NAME> bash

For example, your docker container is running with id 76debad837d2 and name happy_tecadmin. Use one of the following commands with exec method.

Using Container ID:

$ sudo docker exec -it 76debad837d2  bash

Using Container Name:

$ sudo docker exec -it happy_tecadmin bash

Ref From: tecadmin

Related articles