Open a shell in a Docker container

docker

Updated August 29, 2026

Run a command inside a running container with docker exec:

docker exec -it my-container sh

Many small images do not include Bash, so sh is more portable. If Bash is present, use docker exec -it my-container bash. -i keeps standard input open and -t allocates a terminal. A command run with exec is a new process; it does not change the container's configured startup command.

The container must be running. Check with docker ps, and use docker logs or docker inspect if it exits too quickly. Changes made inside a container's writable layer are not a deployment strategy: they disappear when the container is replaced unless stored in a mounted volume or encoded in an image build.

Treat shell access as privileged operational access. Do not paste secrets into commands that will be recorded in terminal history, and do not install debugging packages in production containers without documenting the change.

Sources

related.

Ruslan Osipov

Ruslan Osipov

About the author