Sub topic 9
Choosing user, working directory, and environment
This chapter assumes you can start containers with docker run, list them with docker ps, and run a basic docker exec. Here you will control three things about the process that docker exec starts: which user it runs as, which directory it starts in, and which environment variables it sees.
What the defaults are
A process started with docker exec inherits defaults from the container's configuration. It runs as the container's configured user, which is often root unless the image sets a USER. It starts in the container's configured working directory. It also gets the container's environment variables. The options below override these defaults for one process only. They do not change the container.
| Option | Long form | Effect |
|---|---|---|
| `-u` | `--user` | Run as this user (name or UID, optionally `user:group`) |
| `-w` | `--workdir` | Start in this directory inside the container |
| `-e` | `--env` | Set an environment variable for this process |
| `-d` | `--detach` | Run in the background and return immediately |
Combining the options
The following command runs env as root, in /etc/nginx, with an extra variable:
docker exec -u root -w /etc/nginx -e DEBUG=1 web envThe output lists the container's usual variables plus DEBUG=1. If you ran docker exec web env afterwards, DEBUG would be missing. The variable existed only for that one process.
Choosing a user
Use -u when the default user lacks permission for what you want to do. A common case is an image that runs as an unprivileged user, where you need root to inspect a protected file or install a debugging tool.
The reverse also matters. If you want to reproduce what the application sees, run as the application's user. A file that root can read may be unreadable to the application, and a command run as root will hide that problem.
docker exec -u root web id
docker exec -u 1000:1000 web idThe first command prints root's identity. The second prints UID and GID 1000, even if no user with that number exists in the container.
Choosing a working directory
Use -w for commands that depend on relative paths, such as running a script or a build tool from the project folder. The directory must exist in the container, or the command fails.
docker exec -w /etc/nginx web ls conf.dSetting environment variables
Use -e once per variable. You can also pass a variable name without a value, such as -e HOME. Docker then copies the value from your shell on the host.
docker exec -e LOG_LEVEL=debug -e REGION=eu-west-1 web envFor many variables, --env-file reads them from a file.
Detached mode
With -d, docker exec starts the command and returns at once. The command keeps running inside the container. You will not see its output, so this suits long jobs that write their own files or logs.
docker exec -d web sh -c 'sleep 300'Common errors
- A variable set with
-eis missing later. It applies to one exec process only. To change the environment permanently, recreate the container withdocker run -eor an environment setting in Compose. OCI runtime exec failed: ... no such file or directory. The-wdirectory, or the command itself, does not exist in the container.- Quoting problems.
docker exec web echo $HOMEexpands$HOMEon your host, before Docker runs. Wrap it with single quotes so the container's shell expands it instead:
docker exec web sh -c 'echo $HOME'Exercise
A container runs as an unprivileged user. You run docker exec -e MODE=test web sh -c 'echo $MODE' and it prints test. A second command, docker exec web sh -c 'echo $MODE', prints an empty line. Why? Which option would you add to the first command to run it as root?
Show answer
The -e option sets the variable only for the process started by that one exec call, so the second exec does not see it. To run as root, add -u root.
Exercise
Why does docker exec web echo $HOME print your host's home directory instead of the container's?
Show answer
Your host shell expands $HOME before Docker runs the command. Use single quotes with a shell inside the container: docker exec web sh -c 'echo $HOME'.