DOCKER - 13 Have a Shell in a Container
In this lesson we step inside a container to manipulate it from the inside. Looking at containers from the outside is convenient, but sometimes you need a live shell. Docker gives us several commands for that: docker container run -it to start a new container with an interactive terminal, and docker container exec to attach a shell to a container that is already running.
Opening a shell with docker container run
The -it flag combines --interactive and --tty to give us an interactive terminal. After the image name we override the default command — here we ask for bash:
docker container run -it --name proxy nginx bash
The prompt switches to root@<container-id> — you are root inside the container, not on the host. From there ls -al, package installs and configuration tweaks are all fair game. When you type exit, the container stops, because a container only lives as long as its main command. Since we replaced nginx with bash, leaving bash terminates the container. The same trick with Ubuntu shows how minimal a container image is: apt-get update && apt-get install -y curl is needed even to get a tool like curl. To re-enter an existing stopped container, use docker container start -ai <name>.
exec into a running container
When a container is already running something useful (MySQL, Nginx), we don't want to replace its main process. Use exec to spawn an extra one alongside it:
docker container exec -it mysql bash
This launches a second process inside the running MySQL container without affecting mysqld. Exit the bash and the MySQL container keeps running normally — that's exactly the behaviour you want for administrative tasks.
- nginx / ubuntu — full bash available out of the box
- alpine — only ~5 MB, no bash, use
shinstead apk— Alpine's package manager (you canapk add bashif you really need it)
Alpine is a great example of how stripped-down images can be: docker container run -it alpine bash fails with "bash not found" because the image doesn't ship bash at all. Switching the command to sh works. The takeaway: you can only run programs that exist inside the image, so always check what shells and tools are present before exec-ing in.
Summary
This lesson explains how to access a shell inside a Docker container using docker container run with the -it flags to get an interactive terminal. It covers the differences between Linux distributions running in containers versus traditional virtual machines or physical machines, and demonstrates how to use docker container exec to enter an already-running container. Key examples include accessing nginx and Ubuntu containers, understanding why containers stop when the shell exits versus when using exec on running containers.
Key points
- Use docker container run -it <image> bash to start a new container with an interactive shell access
- Container Linux distributions are typically minimal compared to full VM or physical machine installations, so administrative tasks may require installing additional packages
- The root prompt (#) in a container indicates the root user within that specific container, not the host system
- Use docker container exec -it <container-name> bash to launch a shell inside an already-running container like MySQL
- When exiting a shell from docker run, the container stops because the shell is the main process; when exiting from docker exec, the container continues running because exec launches a secondary process
FAQ
How do I access a shell inside a container that is already running, such as a MySQL database container?
Use the docker container exec command with -it flags: docker container exec -it <container-name> bash. This launches a new shell process inside the running container without stopping it.
Why does my container stop when I exit the shell after using docker container run?
Because you started the container with docker container run bash, the bash shell is the main process. When you exit bash, the container stops. To keep a container running while accessing it, use docker container exec instead.
What is the difference between running docker container run versus docker container exec?
docker container run starts a new container and launches a process inside it (the container stops when that process exits). docker container exec launches an additional process inside an already-running container (the container keeps running when you exit that process).