Home·Terminal commands

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

FlagMeaning
-itinteractive terminal, for a shell
-u <user>run as another user, e.g. root
-w <dir>start in that directory
-e KEY=valueset a variable for this process only
-drun it in the background

Examples

docker exec -it web bash

a shell inside a running container

docker exec -u root -it web sh

as root, and sh because slim images have no bash

docker exec -it db psql -U postgres

runs 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