DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Keep Your Application Secrets Secret
  • 3 Easy Steps for a (Dev)Containerized Microservice With Jolie and Docker
  • Containerize Gradle Apps and Deploy to Kubernetes With JKube Kubernetes Gradle Plugin
  • Deploy an ASP.NET Core Application in the IBM Cloud Code Engine

Trending

  • The Architecture That Keeps Netflix and Slack Always Online
  • The 7 Biggest Cloud Misconfigurations That Hackers Love (and How to Fix Them)
  • Vibe Coding: Conversational Software Development - Part 2, In Practice
  • API Standards Are Data Standards
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Attach a Shell to Docker Container [quick tip]

Attach a Shell to Docker Container [quick tip]

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.

By 
Arun Gupta user avatar
Arun Gupta
·
Nov. 28, 15 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
8.9K Views

Join the DZone community and get the full member experience.

Join For Free

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:


1 2 3 4   > docker run -d couchbase/server 02061ddf0a3d1b2806a1ee6e354f4064d9d2ff4d84d8c96c0273c8883917a92f  

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

This can be verified as:


1 2 3 4 5   > 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:


1 2 3 4 5   > 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


1 2 3 4   > docker exec -it 02061ddf0a3d1b2806a1ee6e354f4064d9d2ff4d84d8c96c0273c8883917a92f bash root@02061ddf0a3d:/#  

Attach Using Truncated Container Id


1 2 3 4   > docker exec -it 02061ddf0a3d bash root@02061ddf0a3d:/#  

Attach Using Unique Numbers From Container Id


1 2 3 4   > 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!

Docker (software) shell

Published at DZone with permission of Arun Gupta. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Keep Your Application Secrets Secret
  • 3 Easy Steps for a (Dev)Containerized Microservice With Jolie and Docker
  • Containerize Gradle Apps and Deploy to Kubernetes With JKube Kubernetes Gradle Plugin
  • Deploy an ASP.NET Core Application in the IBM Cloud Code Engine

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: