Attach Shell to Docker container

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:

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

This can be verified as:

Or complete container id can be verified as:

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

Attach Using Truncated Container Id

Attach Using Unique Numbers from Container Id

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!

Be Sociable, Share!
  • Tweet

6 thoughts on “Attach Shell to Docker container

  1. I like to use it with the container name too:

    $ docker run –name=couchbase -d couchbase/server

    $ docker exec -it couchbase bash

  2. Hello Arun,

    thank you for sharing this knowledge.
    This article was very helpfull for me.

  3. I have read through some similar topics! However, your post has given me a very special impression, unlike other posts. I hope you continue to have valuable articles like this or more to share with everyone!

  4. If you are on the wait to play fireboy and watergirl online free games. As these games are of high rated games which you can enjoy online in your android phones or in your pc with the help of keys.

Leave a Reply

Your email address will not be published. Required fields are marked *