Skip to content
Latchkey

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 lint

In 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →