How to Execute a Command on an Existing Docker Container
To run a command on a Docker container that is already there, we can
use the docker exec command. This lets us run commands in a
container that is already running. It’s a strong tool for managing our
Docker apps. The basic way to write it is
docker exec -it <container_name_or_id> <command>.
Here <container_name_or_id> is the name or ID of our
container. <command> is the command we want to
run.
In this article, we will look at different ways to run commands on Docker containers that are already there. We will learn how to use Docker exec to run commands in active containers. We will also see how to run commands in stopped containers. We will talk about running interactive commands and using Docker attach. We will also learn how to run shell commands in Docker containers with Docker Compose. Lastly, we will answer some common questions about these topics. Here is a short list of what we will talk about:
- How to Execute a Command on an Existing Docker Container
- Using Docker Exec to Execute Commands on Running Containers
- Executing Commands Inside Stopped Docker Containers
- How to Run Interactive Commands on Docker Containers
- Using Docker Attach to Execute Commands in Running Containers
- Executing Shell Commands in Docker Containers with Docker Compose
- Frequently Asked Questions
Using Docker Exec to Execute Commands on Running Containers
We can use the docker exec command to run a command on a
running Docker container. This command helps us run commands inside a
container that is already running. It gives us access to the container’s
environment.
Basic Syntax
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]- CONTAINER: This is the name or ID of the running container.
- COMMAND: This is the command we want to run inside the container.
- ARG: These are optional arguments for the command.
Example Usage
Run a Simple Command
To list files in the
/tmpdirectory of a container calledmy-container, we can use this command:docker exec my-container ls /tmpRun an Interactive Shell
If we want to open an interactive shell like bash in the container, we use:
docker exec -it my-container /bin/bashWe use
-itto set up a pseudo-TTY and keep the input open.Execute a Command with Environment Variables
If we need to set environment variables when running a command, we can use the
-eflag like this:docker exec -e MY_VAR=value my-container printenv MY_VAR
Common Options
-d: This runs the command in the background.--user: This lets us specify the username or UID for running the command.--privileged: This gives extra privileges to the command.
Example of Running a Command as a Different User
To run a command as a specific user inside the container, we can do:
docker exec --user username my-container whoamiChecking Running Containers
Before we run commands, we should check if our container is running. We can list all running containers with this command:
docker psFor more details about using Docker commands, we can check the Docker documentation.
Executing Commands Inside Stopped Docker Containers
To run commands in a stopped Docker container, we first need to restart the container. Docker does not let us run commands directly in a stopped container. Here is how we can do it:
Start the Stopped Container: We can start the container using the
docker startcommand. Just replace<container_name_or_id>with the name or ID of your container.docker start <container_name_or_id>Execute the Command: After we start the container, we can run the command inside it using
docker exec. The-itflags help us interact with the container.docker exec -it <container_name_or_id> <command>For example, if we want to list files in the
/appdirectory of the container, we can do:docker exec -it <container_name_or_id> ls /appStop the Container After Execution: If we want to stop the container again after running the command, we can use:
docker stop <container_name_or_id>
Remember, if we want to run a command without keeping the container running, we can use a different way with Docker. We can create a new container from the same image and run the command we need:
docker run --rm <image_name> <command>This way, we can execute commands without needing to restart a stopped container.
How to Run Interactive Commands on Docker Containers
We can run interactive commands on Docker containers by using the
docker exec command with the -it flags. This
helps us to access the terminal of a running container.
Example of Running Interactive Commands
Find the Container ID or Name: First, we need to find the container where we want to run commands. We can list running containers by using:
docker psExecute a Command: Next, we use the
docker execcommand with-itto run a command interactively. For example, to start a bash shell in a running container calledmy_container, we use:docker exec -it my_container /bin/bashIf the container has a different shell, like sh, we can run:
docker exec -it my_container /bin/sh
Running Other Commands Interactively
We can also run other commands interactively. For example, to start an interactive Python shell, we can use:
docker exec -it my_container pythonDetaching from the Container
To exit from the shell without stopping the container, we can use the
escape sequence Ctrl + P then Ctrl + Q. This
lets us leave the interactive session while the container continues to
run.
Using Docker Compose
If we are using Docker Compose, we can run interactive commands in a
service from our docker-compose.yml file. For example:
docker-compose exec my_service /bin/bashWe change my_service with the name of our service.
For more information on managing Docker containers interactively, we can check this article on Docker commands.
Using Docker Attach to Execute Commands in Running Containers
The docker attach command helps us connect our terminal
to a running Docker container. It lets us access the standard input,
output, and error streams. This is great for working with the main
process of the container. Let’s see how we can use it right.
Basic Syntax
docker attach [OPTIONS] CONTAINERExample Usage
Attach to a Running Container: To attach to a container, we need the container ID or name. For example, if our container is named
my_container:docker attach my_containerDetaching from the Container: If we want to detach without stopping the container, we can use the
CTRL + Pand thenCTRL + Qkeys. This helps us exit the attach session while keeping the container running.
Options
--no-stdin: This option does not attach STDIN.--detach-keys: This lets us change the key combination for detaching.
Limitations
- We can only attach to the main process of the container. If the main process is not interactive, like a web server, we can’t interact well.
- If we start a container with
-d(detached mode), we can still attach to it. But we won’t see any output until we do so.
Use Case Scenario
If we have a container running a shell, we can attach to it to run commands directly:
docker run -it --name my_container ubuntu /bin/bash
# After running the above, we can detach using CTRL + P, CTRL + Q
docker attach my_containerThis way, we can run commands interactively in the running container.
The docker attach command is a good tool for debugging or
monitoring in real time.
For more information on Docker commands and features, we can check this article on how to list running Docker containers.
Executing Shell Commands in Docker Containers with Docker Compose
We can execute shell commands in Docker containers using Docker
Compose. We use the docker-compose exec command for this.
This command lets us run commands in a specific service container that
we define in our docker-compose.yml file.
Basic Syntax
docker-compose exec <service_name> <command>Example
Let’s say we have a service called web in our
docker-compose.yml. We can run a shell command like
this:
docker-compose exec web ls -laRunning an Interactive Shell
If we want to open an interactive shell session like bash or sh inside a running container, we can do this:
docker-compose exec -it <service_name> /bin/bashor
docker-compose exec -it <service_name> /bin/shHandling Multiple Commands
We can also run multiple commands at once. We can chain them with
&&:
docker-compose exec <service_name> sh -c "command1 && command2"Environment Variables
If we need to send environment variables while running commands, we
can use the -e flag:
docker-compose exec -e VAR_NAME=value <service_name> <command>Example with Environment Variables
docker-compose exec -e MY_VAR=value web printenv MY_VARFor more help on Docker Compose and what it can do, we can read the article on what is Docker Compose and how does it simplify multi-container applications.
Frequently Asked Questions
How do we execute a command on a running Docker container?
To execute a command on a running Docker container, we use the
docker exec command. We write the container name and the
command we want to run after that. For example, to run a bash shell
inside a container called my_container, we can use:
docker exec -it my_container bashThis lets us interact with the container’s environment directly.
Can we execute commands in a stopped Docker container?
No, we cannot execute commands in a stopped Docker container because
it is not running. But we can start the container again using the
docker start command. Then, we can use
docker exec to run commands when it is running. To restart
a stopped container called my_container, we can use:
docker start my_containerAfter that, we can execute the command we want.
How do we run an interactive command on a Docker container?
To run an interactive command on a Docker container, we use the
docker exec command with the -it flags. This
allows us to interact with the container’s terminal. For example:
docker exec -it my_container /bin/bashThis command opens a bash shell inside the running container. We can then execute commands interactively.
What
is the difference between docker exec and
docker attach?
docker exec is for running a new command in a running
container. It allows us to open a new shell session or execute a command
without changing the main process. On the other hand,
docker attach connects our terminal to the main process of
a running container. This lets us interact with it directly. We should
use docker exec for running commands and
docker attach for interacting with the main process.
How can we execute commands in a Docker container using Docker Compose?
To execute commands in a Docker container managed by Docker Compose,
we can use the docker-compose exec command. For example, to
run a bash shell in a service named web, we use:
docker-compose exec web bashThis command lets us interact with the specified service container
directly. It works like docker exec.