docker exec Command Reference
Run a command inside a running container.
docker exec executes a new process inside an already-running container. In CI it drives database migrations, runs test suites against a service container, or inspects state for debugging. Drop -t in pipelines to avoid TTY errors.
Common flags
-i, --interactive- keep stdin open-t, --tty- allocate a TTY (omit in non-interactive CI)-e KEY=value- set an environment variable for the exec-w, --workdir- working directory for the command-u, --user- user to run as inside the container-d, --detach- run the command in the background
Example
shell
docker exec -e RAILS_ENV=test app-web bin/rails db:migrate
docker exec -w /app -u node app-web npm run lintIn CI
Use docker exec (without -t) to run migrations or tests inside a service container started by docker run -d or compose up -d. Adding -t in a pipeline triggers "the input device is not a TTY"; use -i alone or neither.
Key takeaways
- exec runs in an existing container; run starts a new one.
- Omit -t in CI to avoid TTY allocation errors.
- -w and -u control the working directory and user for the command.
Related guides
docker run Command ReferenceReference for docker run in CI: --rm, -e, -v, --network, -w, and --entrypoint flags for creating and starting…
docker ps Command ReferenceReference for docker ps in CI: list running or all containers with -a, -q, --filter, and --format to inspect…
docker logs Command ReferenceReference for docker logs in CI: fetch container stdout and stderr with --tail, --since, --timestamps, and -f…