Sub topic 7
Running commands in a container with docker exec
This chapter assumes you can start a container with docker run, know the difference between foreground and detached mode, and can list containers with docker ps. You should also be comfortable with a shell and with processes.
What docker exec does
docker exec starts a new process inside a container that is already running. The container's main process, the one started by docker run, keeps going and is not affected.
The command has this form:
docker exec [options] CONTAINER COMMAND [arguments]CONTAINER is a name or ID. COMMAND must exist inside the container's image. The new process sees the container's filesystem, network, and environment variables.
Two limits to remember:
- The container must be running.
docker execfails on a stopped container. - The command is not part of the image. Nothing it does to the container's writable layer survives if you remove the container.
Running a one-off command
Start a container, then run a single command in it:
docker run -d --name web -p 8080:80 nginx:1.27
docker exec web nginx -v
docker exec web ls /usr/share/nginx/htmlEach docker exec prints the command's output and exits. The exit code of docker exec is the exit code of the command, so you can use it in scripts:
docker exec web test -f /etc/nginx/nginx.conf && echo "config found"Opening an interactive shell
To work inside the container by hand, combine two flags:
-ikeeps standard input open so you can type.-tallocates a pseudo-terminal, so the shell shows a prompt and handles line editing.
docker exec -it web shYou get a prompt inside the container. Type exit to leave. Only the shell ends. The container keeps running.
Use sh unless you know the image includes bash. Many small images, such as Alpine-based ones, have no bash.
If you pipe data into a command, use -i without -t:
echo "hello" | docker exec -i web sh -c 'cat > /tmp/note.txt'
docker exec web cat /tmp/note.txtUseful options
| Option | Purpose | Example |
|---|---|---|
| `-e KEY=value` | Set an environment variable for this command only | `docker exec -e DEBUG=1 web env` |
| `-w /path` | Set the working directory | `docker exec -w /etc/nginx web ls` |
| `-u user` | Run as a given user | `docker exec -u root web id` |
| `-d` | Run the command in the background | `docker exec -d web sleep 300` |
Environment variables set with -e apply only to that exec process. They do not change the container's main process.
Common use: working with a database
With the PostgreSQL container from the earlier chapter, you can open the database client directly:
docker exec -it db psql -U postgresThis runs psql inside the container, so you do not need to install a client on the host.
Common errors
- "container is not running": The container has exited. Run
docker ps -ato see its state anddocker logsto find out why. Start it again, or create a new one. - "executable file not found in $PATH": The program does not exist in the image. Try
shinstead ofbash, or check the image for the tool. - "the input device is not a TTY": You used
-twhere no terminal is attached, for example in a cron job or CI step. Drop-t. - Changes vanish: Files you create with
execare in the container's writable layer. Put lasting data in a volume, and put lasting software in the image.
Exercise
A container named api is running. You want to list the files in /app as the root user, without opening a shell. Which command do you use, and why is -t not needed?
Show answer
Use docker exec -u root api ls /app. The command prints its output and exits, so no interactive terminal is needed. -it is only needed when you type into a process, such as a shell.
Exercise
You run docker exec -it web bash and get "executable file not found in $PATH". The container is running. What is the likely cause and what do you try next?
Show answer
The image does not include bash. Try docker exec -it web sh, which most images provide.