Running Commands Inside Docker Containers: A Step-by-Step Guide

Docker containers are lightweight, portable, and widely used for application development and deployment. Often, you’ll need to run commands inside a container — whether for troubleshooting, configuration, or software installation. In this guide, we’ll show you how to run commands in Docker containers using different methods.
Prerequisites
Before you begin, make sure you have:
- Docker installed and running on your system.
- A basic understanding of the command line.
- A Docker container running or an image available.
1. Run a Command During Container Startup
You can run a command in a Docker container at the time you start it using the docker run
command:
$ docker run ubuntu echo "Hello from inside the container"
Explanation:
docker run
starts a new container.ubuntu
is the image used.echo "Hello from inside the container"
is the command executed inside the container.
If the container runs and exits immediately (like in the example above), it’s because the command finished executing.
2. Run an Interactive Shell in a Container
To interact with a container via an interactive shell session (e.g., bash), use:
$ docker run -it ubuntu bash
Flags:
-i
: Keeps STDIN open.-t
: Allocates a pseudo-TTY.ubuntu
: The base image.bash
: The shell you want to run inside the container.
Once inside, you can run commands like:
$ apt update
$ apt install curl
To exit the session, type exit
.
3. Execute a Command in a Running Container
If the container is already running, use docker exec
to run a command inside it:
$ docker exec <container_name_or_id> ls /
For example:
$ docker exec my_container ls /
To start a bash session in a running container:
$ docker exec -it my_container bash
This is useful for debugging or checking logs/files inside the container.
4. Use docker attach
to Connect to a Running Container
$ docker attach <container_name_or_id>
This attaches your terminal to the container’s main process. However, unlike exec
, attach
connects to the container’s STDIN/STDOUT, which may be blocked or limited depending on how the container was started.
⚠️ Press
Ctrl + P
followed byCtrl + Q
to detach without stopping the container.
5. Run a Command in Dockerfile with CMD
or ENTRYPOINT
When building your own image, you can define the default command that runs when the container starts:
Example Dockerfile:
FROM ubuntu
CMD ["echo", "Hello from CMD"]
Or using ENTRYPOINT
:
FROM ubuntu
ENTRYPOINT ["echo", "Hello from ENTRYPOINT"]
Then build and run:
$ docker build -t my-image .
$ docker run my-image
6. Best Practices
- Prefer
docker exec
for troubleshooting running containers. - Use
-it
for interactive commands likebash
orsh
. - Don’t confuse
docker run
(creates new container) withdocker exec
(uses existing container). - Always use container names or IDs clearly for better management.
Conclusion
Running commands in a Docker container is essential for development, debugging, and configuration. Whether you’re executing a one-off command or starting a long-lived shell session, Docker provides flexible options to meet your needs.
Need more help? Drop a comment or explore Docker official documentation for deeper insights.