Docker Containers
Estimated reading: 4 minutes 292 views

How to Enter Docker Containers with docker exec and attach

Introduction – Why Learn to Access Docker Containers?

As you work with Docker, there are times when you need to “get inside” a running container — to troubleshoot issues, inspect logs, configure services, or run manual commands. Fortunately, Docker provides powerful CLI commands to help you interact with containers just like any other Linux system.

In this guide, you’ll learn:

  • How to access a running Docker container using docker exec and docker attach
  • The differences between both methods
  • When to use each approach
  • Common FAQs and troubleshooting tips

Step 1: Identify the Running Container

Before entering a container, you must know which ones are currently running.

List Running Containers

docker ps

Sample Output:

CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         NAMES
f9a8c1234bcd   ubuntu    "/bin/bash"   5 minutes ago   Up 5 minutes   my_ubuntu_container

Note: Record the CONTAINER ID or NAME (e.g., my_ubuntu_container). You’ll need it in the next steps.


Method 1: Using docker exec (Recommended )

The docker exec command is the most commonly used and safest way to access the internal shell of a running container.

Command:

docker exec -it <container_id_or_name> /bin/bash

If the container doesn’t support bash, try:

docker exec -it <container_id_or_name> /bin/sh

Example:

docker exec -it my_ubuntu_container /bin/bash

Explanation:

FlagPurpose
execRuns a command in the container
-iKeeps STDIN open (interactive)
-tAllocates a pseudo-TTY (terminal interface)
/bin/bashLaunches a Bash shell session

Use this method when:

  • You want to start a new shell session
  • You need to execute admin-level commands inside the container
  • The container runs a background service (e.g., NGINX, Node.js)

Method 2: Using docker attach

The docker attach command connects your terminal to the container’s main process. It does not spawn a new shell — you’ll see the live output and input of the primary process.

Command:

docker attach <container_id_or_name>

Example:

docker attach my_ubuntu_container

Important:

  • If you press Ctrl + C, the main container process may stop.
  • You are “attached” to whatever process was launched with the container (not always a shell).
  • To safely detach without stopping the container, press:
Ctrl + P, then Ctrl + Q

Use this method when:

  • The main container process is an interactive shell
  • You want to resume an ongoing session (like a terminal or REPL)

Comparison Table – docker exec vs docker attach

Featuredocker execdocker attach
Opens new session Yes No (connects to existing process)
Risk of killing container Safe to exit Yes (if Ctrl+C is sent)
Best for debugging Yes Only if shell is the main process
Detach safely Just exit or Ctrl+D Ctrl + P then Ctrl + Q
Preferred method Yes (general use) For specific interactive sessions only

Summary – Enter Docker Containers with docker exec and attach

Getting inside a running Docker container allows you to explore, fix, or configure its internal environment. Based on your use case, choose the method wisely.

Key Takeaways:

  • Use docker exec to open a new terminal session inside a container
  • Use docker attach to connect to the container’s main process
  • Always check whether the container has /bin/bash or fallback to /bin/sh
  • Safely detach from docker attach using Ctrl + P and Ctrl + Q

These tools are essential for container-level debugging, auditing, and management in DevOps workflows.


Frequently Asked Questions (FAQs)

What if /bin/bash is not available in the container?

Use /bin/sh instead, which is usually available in lightweight containers like Alpine:

docker exec -it <container_id_or_name> /bin/sh

How do I detach from docker attach without stopping the container?

Use this keyboard sequence:

Ctrl + P, then Ctrl + Q

This keeps the container running in the background.


Can I access a stopped container?

No. The container must be running to get inside.

Start it first:

docker start <container_id_or_name>

Then run docker exec or docker attach.


What is the difference between docker exec and docker attach?

Featuredocker execdocker attach
Spawns new shell Yes No
Safe to exit Yes May kill container
Use caseDebugging or new sessionResume main process

Share Now :
Share

How to Get Inside a Running Docker Container

Or Copy Link

CONTENTS
Scroll to Top