Contents
docker exec Command: Run a Command or Shell in a Container
docker exec runs a command inside a running container. To get a shell, run docker exec -it <container> sh (use bash instead of sh only if the image has bash; Alpine images don't). It is most often used to enter a container to investigate a problem. See View logs and debug for the full debugging workflow.
Syntax
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
docker exec -it web sh # Enter the container's shell
docker exec web nginx -t # Run a command and get its result
Options
docker exec doesn't have many options. Here are all the options listed by docker exec --help:
| Option | Description |
|---|---|
-i, --interactive | Keep standard input open so you can enter input |
-t, --tty | Allocate a terminal. Use with -i for interactive sessions: -it |
-u, --user USER[:GROUP] | Run as the specified user (name or UID) |
-w, --workdir PATH | Run in the specified directory inside the container |
-e, --env NAME=VALUE | Set an environment variable for this exec session |
--env-file FILE | Read environment variables from a file |
-d, --detach | Run in the background and return immediately |
--privileged | Give this command additional privileges (for debugging; don't use routinely) |
--detach-keys | Change the key sequence for detaching (default Ctrl+P, Ctrl+Q) |
The following examples run commands in an nginx container named web. It was started with the APP_MODE=demo environment variable:
docker run -d --name web -e APP_MODE=demo nginx:1.30-alpine
Run a command
$ docker exec web nginx -v
nginx version: nginx/1.30.5
$ docker exec web sh -c "ls /etc/nginx/conf.d && echo \$APP_MODE"
default.conf
demo
- The first command runs
nginx -vin the container and prints the result directly. - To use
&&, pipes, or variables, pass the command to the shell inside the container withsh -c "...". The\in\$APP_MODEprevents the host shell from substituting the variable first;demois the value inside the container.
Enter a container interactively
docker exec -it web sh
After you enter, the prompt changes to / #. You can work as you would in a regular Linux environment. Type exit to leave; the container won't stop. If the image has bash, you can use bash instead.
-u, -w, and -e
$ docker exec -u nginx web id
uid=101(nginx) gid=101(nginx) groups=101(nginx)
$ docker exec -w /usr/share/nginx/html web ls
50x.html
index.html
$ docker exec -e GREETING=hi web sh -c "echo \$GREETING"
hi
-u nginx: Run as thenginxuser (the default in the container is root). This is useful for checking file permission issues.-w /usr/share/nginx/html: Run in the specified directory, as if you had runcdfirst.-e GREETING=hi: Set an environment variable for this command only. It doesn't change the container itself.
-d: Run in the background
$ docker exec -d web sh -c "sleep 2; touch /tmp/done"
$ docker exec web ls /tmp
done
-d runs the command in the background and returns immediately. Check after 2 seconds, and /tmp/done has been created. This is useful for starting work that doesn't require you to wait for a result.
The container isn't running
$ docker stop web
web
$ docker exec web ls
Error response from daemon: container 1ec4fdc5c592b09b64e8f28869c37f89ab17163f88bfcb09e82815a44b0e1164 is not running
docker exec only works with running containers. For a stopped container, run docker start first. If the container exits as soon as it starts, you can't enter it with exec. Check docker logs or start a new container to investigate with docker run --rm -it --entrypoint sh IMAGE.
Common questions and misconceptions
You can't exec on an image
The target of docker exec must be an existing, running container. An image is just the material used to create a container, so it can't be the target of exec:
$ docker exec nginx:1.30-alpine nginx -v
Error response from daemon: No such container: nginx:1.30-alpine
nginx:1.30-alpineis an image name. Docker looks for it as a container name, so it responds withNo such container.- To run an image's command in a new container, use
docker run --rm nginx:1.30-alpine nginx -v.
Exec on a stopped container
A container's existence doesn't mean it's running. docker exec can't start a process in a stopped container:
$ docker exec job echo hi
Error response from daemon: container 1d271133e82d802099ef0728c0e511450eabac9bc85ef4721b1b8f6c3ef49019 is not running
is not runningmeans the container is currently stopped; exec only works with running containers.- If the container can keep running, start it and then use exec. If you only need to run a one-time command, use
docker run --rmto create a new container.
Run one-time commands like curl and wget in a container
Some images already include diagnostic tools. For example, this nginx container has both curl and wget, so you can use exec to make a request directly:
$ docker exec web curl -s localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, nginx is successfully installed and working.
Further configuration is required for the web server, reverse proxy,
API gateway, load balancer, content cache, or other features.</p>
<p>For online documentation and support please refer to
<a href="https://nginx.org/">nginx.org</a>.<br/>
To engage with the community please visit
<a href="https://community.nginx.org/">community.nginx.org</a>.<br/>
For enterprise grade support, professional services, additional
security features and capabilities please refer to
<a href="https://f5.com/nginx">f5.com/nginx</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
$ docker exec web wget -qO- localhost | head -n 4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
$ docker exec web sh -c 'which curl wget'
/usr/bin/curl
/usr/bin/wget
- Both
curlandwgetare present innginx:1.30-alpine; whether you can use a tool depends on the contents of the image. - The
wgetexample pipes the output toheadto show only the first four lines.
If the container is missing a tool, you can start a helper image and add it to the target container's network namespace:
$ docker run --rm --network container:web curlimages/curl:8.17.0 -s -o /dev/null -w '%{http_code}\n' http://localhost
Unable to find image 'curlimages/curl:8.17.0' locally
8.17.0: Pulling from curlimages/curl
bd9ddc54bea9: Pulling fs layer
2d35ebdb57d9: Pulling fs layer
f2f82549a04b: Pulling fs layer
2d35ebdb57d9: Download complete
bd9ddc54bea9: Download complete
f2f82549a04b: Download complete
2d35ebdb57d9: Pull complete
f2f82549a04b: Pull complete
bd9ddc54bea9: Pull complete
Digest: sha256:935d9100e9ba842cdb060de42472c7ca90cfe9a7c96e4dacb55e79e560b3ff40
Status: Downloaded newer image for curlimages/curl:8.17.0
200
--network container:weblets the helper container shareweb's network namespace, so it can reach nginx atlocalhost.--rmremoves the helper container when the command finishes. The image is downloaded the first time; afterward, you can run it directly.
Difference between docker exec and docker compose exec
Compose commands use a service name; regular docker exec uses the actual container name or ID. The container for the web service below is named proj-web-1:
$ cd /work/proj && docker compose ps --format 'table {{.Service}}\t{{.Name}}'
SERVICE NAME
web proj-web-1
$ cd /work/proj && docker compose exec web nginx -v
nginx version: nginx/1.30.6
$ docker exec proj-web-1 nginx -v
nginx version: nginx/1.30.6
$ cd /work/proj && docker compose exec -T web cat /etc/os-release | head -n 2
NAME="Alpine Linux"
ID=alpine
- In
docker compose exec web ...,webis the Compose service name. Regulardocker execneeds the actual container name, such asproj-web-1. -Tdisables pseudo-TTY allocation, which is useful for non-interactive situations such as piping output.
docker compose run creates another new container to run a command; it doesn't run exec on the existing service container:
$ cd /work/proj && docker compose run --rm web nginx -v
Container proj-web-run-26942a9370bc Creating
Container proj-web-run-26942a9370bc Created
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
nginx version: nginx/1.30.6
runcreates a new container with a name containing-run-, then removes it after the command finishes because of--rm.- The new container first runs the nginx image's entrypoint configuration process, so you'll see extra startup output in addition to the version information.
Related chapters
- View logs and debug
- docker run options
- docker cp: Copy files between the host and a container
- Common error messages:
executable file not found, TTY errors
Exercises
Exercise: Use one command to list the files in /etc/nginx in the web container as the nginx user.
Answer
docker exec -u nginx -w /etc/nginx web ls
FAQ
What's the difference between docker exec and docker run?
docker run uses an image to **create a new container**; docker exec runs another process inside a **container that is already running**. When exec ends, it doesn't affect the container itself.
Why does docker exec -it container bash report executable file not found?
The container doesn't have bash. For example, Alpine-based images only have sh. Use docker exec -it container sh instead.
Will files I modify in a container still be there after I recreate the container?
No. Changes made with exec are in the container's writable layer and disappear when you delete the container. Put changes you need to keep in a Dockerfile or in a Volume.
Can I run docker exec directly on an image?
No. The first positional argument to docker exec is the **name or ID of a running container**, not an image name. To create a new container from an image and run a command, use docker run.
Updated November 4, 2026