How To Run Commands Inside Docker Containers: A Step-by-Step Guide

install Docker on Fedora 13

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 by Ctrl + 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 like bash or sh.
  • Don’t confuse docker run (creates new container) with docker 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.

🧠 FAQ — Running Commands Inside Docker Containers

1. Why would I want to run commands inside a Docker container?
Running commands inside a container lets you install software, debug issues, inspect files, or configure settings within the isolated environment of the container. It’s essential for development and troubleshooting workflows.

2. How do I run a command when I start a new container?
You can include the command directly after the image name in the docker run command. For example:

docker run ubuntu echo "Hello from inside!"

This starts an Ubuntu container, runs the command, then exits.

3. How can I start an interactive shell in a new container?
Use docker run -it <image> bash to launch a new container and open an interactive shell session. The -i flag keeps input open and -t allocates a terminal.

4. What’s the best way to run a command inside a container that’s already running?
Use docker exec <container_name_or_ID> <command>. For interactive shells, add -it (e.g., docker exec -it my_container bash). This lets you run commands without stopping the container.

5. What’s the difference between docker exec and docker attach?
docker exec runs a new command or shell inside the container without affecting the container’s main process.
docker attach connects to the container’s main process’s input/output streams and doesn’t start a new process, which can behave differently depending on what the container is running.

6. Can I define a command in a Dockerfile instead of running it manually?
Yes — using CMD or ENTRYPOINT in a Dockerfile lets you specify a default command that runs when the container starts. This is useful for automating recurring tasks.

7. What if the container exits immediately after a command?
If the specified command finishes quickly and there’s nothing else keeping the container running, the container exits automatically. To keep it alive for interactive use, start a shell or define a long-running process.

8. Do I need to install bash in the image before I can use it?
Yes — if the container image doesn’t include bash (common in minimal images like Alpine), you may need to use another shell such as sh or install bash first.

9. Can I run multiple commands at once?
Yes — you can chain commands in one docker exec or docker run call using shell syntax (e.g., sh -c "apt update && apt install curl"). This runs multiple commands in sequence.

10. Is running commands inside a container safe?
It’s safe for local development and debugging, but for production containers, avoid interactive sessions — instead automate tasks through images and orchestration tools. Use proper access controls and only expose necessary interfaces.

(Visited 246 times, 1 visits today)

You may also like