Executing Commands in Containers in Docker

From the Docker cheat sheet ยท Container Management ยท verified Jul 2026

Executing Commands in Containers

Run commands inside running containers with docker exec and copy files with docker cp

bash
# Run a command in a running container
docker exec my-container ls /app

# Open an interactive shell
docker exec -it my-container sh
docker exec -it my-container bash

# Copy files between host and container
docker cp my-container:/app/logs ./logs
docker cp ./config.yml my-container:/app/config.yml
๐Ÿ’ก docker exec -it opens an interactive terminal โ€” use sh for Alpine, bash for Debian/Ubuntu
โšก Use docker cp to quickly grab log files or inject config without rebuilding
๐Ÿ“Œ docker stats shows live resource usage โ€” essential for debugging memory/CPU issues
๐ŸŸข docker exec -d runs the command in the background without attaching to it
execcpdebug

More Docker tasks

Back to the full Docker cheat sheet