docker/exec

Execute Command

Run a command in a running container.

container
debugging

Command

docker exec

Explanation

docker exec allows direct command execution in containers. It’s useful for debugging or administration. The -it flags open an interactive terminal. Without them, the command runs non-interactively.

Common Use Cases

  • Debug running containers
  • Inspect file systems or logs inside containers
  • Run administrative or maintenance tasks

Best Practices

  • Always use -it for shells
  • Limit exec to debugging or emergency access

Common Mistakes to Avoid

  • Running exec on stopped containers
  • Omitting -it and losing terminal interactivity

Troubleshooting

Problem: Command fails with 'container not found'

Solution: Ensure container is running (check with docker ps).

Examples

Start interactive bash session

docker exec -it my_container bash

Run command in container

docker exec my_container ls /app