dockerexec-it web-api sh
dockerexec web-api cat/app/config.jsondockerexec-u root web-api apt-get update
Output
/ #
Note The container must be running. Use -it for an interactive shell. The -u flag lets you override the user (e.g., run as root even if the image uses a non-root user).
dockerexec-it web-api sh
dockerexec-it db-postgres bash
dockerexec-it web-api sh -c'ls -la /app && cat /app/.env'
Output
/app #
Note Alpine-based images have sh but not bash. If the container has no shell at all (distroless or scratch), you can use docker debug (Docker Desktop) or copy a static busybox binary in via docker cp.
Frequently asked questions
How does Docker handle enter container?
Docker covers this with 2 copy-ready snippets on this page. The "Execute a Command in a Running Container" snippet in Docker uses `docker exec [options] <container> <command>`.
Which command does the Docker example use?
The "Execute a Command in a Running Container" snippet uses `docker exec [options] <container> <command>`, from the Containers section of the Docker cheat sheet.
What other Docker snippets are shown for "enter container"?
Besides "Execute a Command in a Running Container", this page also shows "Open a Debug Shell".
Is there anything to watch out for?
Yes. For "Execute a Command in a Running Container": The container must be running. Use -it for an interactive shell. The -u flag lets you override the user (e.g., run as root even if the image uses a non-root user).