Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do I get into a Docker container's shell?
Docker containers provide isolated environments for applications, and accessing their shell is essential for debugging, testing, and interactive work. Once you have a Docker container, you can work with its environment just like an Ubuntu machine − execute commands, explore the file system, and deploy applications.
There are three primary methods to access a Docker container's shell, depending on the container's current state and your specific needs.
Method 1: Docker Run Command
Use this method when you want to create and run a new container with immediate shell access. The docker run command creates a container from an image and starts it with the specified command.
docker run -it --name=myubuntu ubuntu:latest bash
Command breakdown:
-
-i(interactive) − Keeps STDIN open for interaction -
-t(pseudo-TTY) − Allocates a terminal for the container -
--name=myubuntu− Assigns a custom name to the container -
bash− The command executed when the container starts
This command automatically pulls the Ubuntu image (if not available locally), creates the container, and provides immediate access to its bash shell.
Method 2: Docker Exec Command
Use this method to access an already running container. The docker exec command runs a new process inside an active container without affecting the main process.
Syntax:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Example:
docker exec -it myubuntu bash
This creates a new bash session inside the running myubuntu container. You can run multiple exec sessions simultaneously, each creating a separate shell process.
Method 3: Docker Start Command
Use this method to restart a stopped container and attach to its shell. This approach is useful when you have an exited container that you want to resume.
First, check your container's status:
docker container ps -a
Then start and attach to the stopped container:
docker start -ai myubuntu
Options explained:
-
-a(attach) − Attaches to the container's output streams -
-i(interactive) − Enables interactive mode
Comparison of Methods
| Method | Use Case | Container State | Creates New Container |
|---|---|---|---|
docker run |
Fresh container with shell | None (creates new) | Yes |
docker exec |
Access running container | Running | No |
docker start |
Resume stopped container | Stopped/Exited | No |
Common Use Cases
Debugging applications: Use docker exec to access a running production container without interrupting the main process.
Development workflow: Use docker run to quickly spin up development environments with immediate shell access.
Container maintenance: Use docker start to resume work on previously configured containers without losing changes.
Conclusion
Accessing Docker container shells is straightforward with these three methods. Use docker run for new containers, docker exec for running containers, and docker start for stopped containers. Choose the method that best fits your workflow and container state.
