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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
-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

Run commands in a running container
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.

Working directory and environment
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.

Was this page helpful?