Attach Shell to Docker container

Posted by: Arun Gupta on November 19, 2015

Docker Logo

Often there is a need to attach a shell to an already running Docker container. This is very useful, especially for debugging. This blog will explain how to attach a shell to an already running Docker container.

So let’s say you run a Couchbase Docker container in detached mode:

> docker run -d couchbase/server
02061ddf0a3d1b2806a1ee6e354f4064d9d2ff4d84d8c96c0273c8883917a92f

Running the container gives you the complete container id, 02061ddf0a3d1b2806a1ee6e354f4064d9d2ff4d84d8c96c0273c8883917a92f in this case.

This can be verified as:

> docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                        NAMES
02061ddf0a3d        couchbase/server    "/entrypoint.sh couch"   38 seconds ago      Up 37 seconds       8091-8093/tcp, 11207/tcp, 11210-11211/tcp, 18091-18092/tcp   clever_hypatia

Or complete container id can be verified as:

> docker ps --no-trunc
CONTAINER ID                                                       IMAGE               COMMAND                             CREATED              STATUS              PORTS                                                        NAMES
02061ddf0a3d1b2806a1ee6e354f4064d9d2ff4d84d8c96c0273c8883917a92f   couchbase/server    "/entrypoint.sh couchbase-server"   About a minute ago   Up 59 seconds       8091-8093/tcp, 11207/tcp, 11210-11211/tcp, 18091-18092/tcp   clever_hypatia

Attach Shell to Docker Container

Bash shell can be attached to an already running container using docker exec -it {CID} bash. The {CID} can be the complete container id, truncated container id, or even the first few digits of the container ids amongst all the currently running containers.

So the following three commands are equivalent in our case:

Attach Using Complete Container Id

> docker exec -it 02061ddf0a3d1b2806a1ee6e354f4064d9d2ff4d84d8c96c0273c8883917a92f bash
root@02061ddf0a3d:/#

Attach Using Truncated Container Id

> docker exec -it 02061ddf0a3d bash
root@02061ddf0a3d:/#

Attach Using Unique Numbers from Container Id

> docker exec -it 0 bash
root@02061ddf0a3d:/#

In this case, just specifying 0 would work because this is the only running container. If multiple containers are running that have 0 as the starting number then the first few digits that make the container id unique are required.

Enjoy!

The post Attach Shell to Docker container appeared first on Miles to go 3.0 ....

Arun Gupta

About Arun Gupta

Arun Gupta is Director of Developer Advocacy at Red Hat and focuses on JBoss Middleware. As a founding member of the Java EE team at Sun Microsystems, he spread the love for technology all around the world. At Oracle, he led a cross-functional team to drive the global launch of the Java EE 7 platform through strategy, planning, and execution of content, marketing campaigns, and program. He is a prolific blogger since 2005 and have authored 1500+ blogs on technology. Arun has extensive speaking experience in ~40 countries on myriad topics and is a JavaOne Rockstar. He also founded the Devoxx4Kids chapter in the USA and continues to promoting technology education amongst kids. An author of a best-selling book, an avid runner, a globe trotter, a Java Champion, JUG leader, he is easily accessible at @arungupta.