Docker Exec Basics
Introduction to Docker Exec Command
Docker exec
is a powerful command-line tool that enables direct interaction with running containers. It allows administrators and developers to execute commands inside active Docker containers, providing seamless management and troubleshooting capabilities.
Core Functionality
The docker exec
command enables users to:
- Run specific commands within a running container
- Access container's shell environment
- Perform administrative tasks and debugging
graph LR
A[Docker Host] --> B[Running Container]
B --> |docker exec| C[Command Execution]
Basic Syntax and Usage
The standard syntax for docker exec
is:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Command Options
Option |
Description |
Example |
-i |
Interactive mode |
Keep STDIN open |
-t |
Allocate pseudo-TTY |
Enable terminal interaction |
-u |
Specify user |
Run command as specific user |
Practical Code Examples
Executing Simple Commands
## Run ls command inside a container
docker exec my_container ls /app
## Interactive bash shell
docker exec -it my_container /bin/bash
Running Commands as Different Users
## Execute command as root user
docker exec -u root my_container whoami
## Execute command as specific user
docker exec -u developer my_container python --version
The docker exec
command provides flexible container interaction, supporting various Linux container management scenarios with minimal complexity.