Docker Exec Command
# Docker exec Command
[Docker Command Manual](#)
* * *
The `docker exec` command is used to execute a new command inside a running container. This is very useful for debugging, running additional processes, or performing management operations within the container.
### Syntax
docker exec CONTAINER COMMAND [ARG...]
### Common Parameters
* **`-d, --detach`**: Run command in the background.
* **`--detach-keys`**: Override the key sequence for detaching a container.
* **`-e, --env`**: Set environment variables.
* **`--env-file`**: Read in a file of environment variables.
* **`-i, --interactive`**: Keep STDIN open.
* **`--privileged`**: Give extended privileges to this command.
* **`--user, -u`**: Run the command as the specified user.
* **`--workdir, -w`**: Working directory inside the container.
### Examples
Run a command inside the container:
docker exec my_container ls /app
Execute the `ls /app` command inside the running `my_container` container, listing the contents of the `/app` directory.
Run a command in interactive mode:
docker exec -it my_container /bin/bash
Start an interactive Bash shell inside the running `my_container` container. `-i` keeps STDIN open, and `-t` allocates a pseudo-TTY.
Run a command in the background:
docker exec -d my_container touch /app/newfile.txt
Execute the `touch /app/newfile.txt` command in the background inside the running `my_container` container, creating a new file.
Set environment variables:
docker exec -e MY_ENV_VAR=my_value my_container env
Execute the `env` command inside the running `my_container` container, setting the environment variable `MY_ENV_VAR` to the value `my_value`.
Run a command as a specified user:
docker exec -u user123 my_container whoami
Execute the `whoami` command inside the running `my_container` container as the user `user123`.
Specify the working directory:
docker exec -w /app my_container pwd
Execute the `pwd` command inside the running `my_container` container with `/app` as the working directory.
### Use Cases
* **Debugging Containers**: Enter the container to debug and troubleshoot issues.
* **Management Tasks**: Run additional administrative tasks or maintenance operations inside the container.
* **Monitoring and Inspection**: Execute monitoring and inspection commands inside the container to get runtime status and logs.
### Summary
The `docker exec` command is a very powerful tool in Docker, allowing users to execute additional commands inside a running container. It is extremely useful for debugging, managing, and maintaining containers, helping users to more flexibly control and operate the internal environment of a container.
* * Docker Command Manual](#)
YouTip