docker exec
Packages and runtimes
Usage
docker exec -it <container> <command>Runs another process inside a container that is already running; it fails on a stopped one, and slim images have no bash, so you go in with sh.
Common flags
| Flag | Meaning |
|---|---|
| -it | interactive terminal, for a shell |
| -u <user> | run as another user, e.g. root |
| -w <dir> | start in that directory |
| -e KEY=value | set a variable for this process only |
| -d | run it in the background |
Examples
docker exec -it web basha shell inside a running container
docker exec -u root -it web shas root, and sh because slim images have no bash
docker exec -it db psql -U postgresruns a client that lives inside the container
The question is always what goes where: inside this project, or on the whole machine.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does docker exec do?
Runs another process inside a container that is already running; it fails on a stopped one, and slim images have no bash, so you go in with sh.
Q. How do I type it?
docker exec -it <container> <command> — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
5 are listed here; the full set is in man docker. This command sits under Packages and runtimes.
Related commands
man page: man docker-exec