docker exec

Run a command in a running container. Useful for debugging, shell access, or one-off tasks.

Syntax

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Examples

Example 1
docker exec -it my-container bash

Open interactive bash shell in the container.

Example 2
docker exec my-container ls /app

Run a one-off command (list files).

Example 3
docker exec my-container env

View environment variables inside the container.

Common flags

FlagDescription
-d, --detachRun in background
-itInteractive TTY
-e, --envSet environment variable
-w, --workdirWorking directory inside container

Tips & best practices

Related commands

View full Docker cheatsheet →