Docker Engine 29 reference · Containers
docker exec Command (Docker CLI)
Run an extra command inside a running container, for example a shell for debugging or a one-off task.
Docker Engine 29
Syntax
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Parameters
| Parameter | Description |
|---|---|
-it |
Interactive with a terminal (for shells). |
-u USER |
Run as another user. |
-e KEY=VALUE |
Set an environment variable. |
-w DIR |
Working directory inside the container. |
Examples
docker run -d --name rc-ref-exec alpine:3.24 sleep 300 > /dev/null
docker exec rc-ref-exec cat /etc/alpine-release
docker exec -u nobody rc-ref-exec id -un
docker rm -f rc-ref-exec > /dev/null
Output
3.24.3 nobody
Run this on your computer — see “Get Started” for setup.
docker run -d --name rc-ref-exec2 alpine:3.24 sleep 300 > /dev/null
docker exec -w /etc -e GREETING=hi rc-ref-exec2 sh -c 'echo "$GREETING from $(pwd)"'
docker rm -f rc-ref-exec2 > /dev/null
Output
hi from /etc
Run this on your computer — see “Get Started” for setup.
Changes made with exec disappear when the container is replaced; fix problems in the image instead. Images without a shell need a debug container.
Related
docker run· Create and start a container from an image, with options for ports, environment, volumes, names and cleanup.docker logs· Show what a container wrote to stdout and stderr, optionally only the last lines, with timestamps, or following new output.- Logs, exec and inspect · tutorial