docker-exec
Execute commands inside a running container
TLDR
View documentation for the original command
SYNOPSIS
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
PARAMETERS
-d, --detach
Run command in background of the container
--detach-keys
Select escape sequence for detaching a container
-e, --env
Set environment variables
--env-file
Read in a file of environment variables
-i, --interactive
Keep STDIN open even if not attached
--privileged
Give extended privileges to this command
-t, --tty
Allocate a pseudo-TTY
-u, --user
Username or UID (format: <user>[:<group>] or <UID>[:<GID>])
-w, --workdir
Working directory inside the container
DESCRIPTION
The docker exec command allows users to run arbitrary commands inside a running Docker container without stopping or restarting it. This is essential for debugging, maintenance, and interactive sessions in containerized environments.
It attaches to the container's process namespace, executing the specified command as a child process of the container's PID 1. Common use cases include checking logs with /bin/sh, installing packages, or running scripts.
For interactive shells, combine -it flags to allocate a TTY and keep STDIN open, mimicking a native shell experience. Non-interactive commands run detached with -d.
Security is managed via --user to specify run-as user/group, preventing root escalation. Environment variables can be passed with -e or files via --env-file. The container must be running; use docker ps to verify.
This command enhances DevOps workflows by enabling live inspections, configuration tweaks, and troubleshooting in production-like setups, promoting immutable infrastructure principles.
CAVEATS
Container must be running (docker start if stopped). No support for stopped/exited containers. Privileged mode poses security risks. Interactive mode requires proper terminal setup.
COMMON USAGE
docker exec -it <container> /bin/bash for interactive shell.
docker exec <container> ls /app to list files.
HISTORY
Introduced in Docker 1.3.0 (2014) to address limitations of docker run for running containers. Evolved with Docker Engine; key enhancements in 17.06 for better security and multi-platform support.
SEE ALSO
docker(1), docker-run(1), docker-attach(1), docker-start(1)


