← 返回命令列表

Linux command

docker-exec 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Run command in container

docker exec [container] [command]

Open interactive shell

docker exec -it [container] /bin/bash

Run as specific user

docker exec -u [root] [container] [command]

Set environment variable

docker exec -e [VAR=value] [container] [command]

Run in specific directory

docker exec -w [/path] [container] [command]

说明

docker exec runs a new command in a running container's existing environment, creating a new process within the container's namespaces and cgroups. This is fundamentally different from docker run, which creates an entirely new container. The most common use case is opening an interactive shell for debugging with docker exec -it container /bin/bash, which provides direct access to the container's filesystem and running processes. Commands executed via docker exec inherit the container's environment but can be customized with user, working directory, and environment variable options. The --privileged flag grants extended capabilities useful for system administration tasks, though it should be used cautiously as it reduces container isolation.

参数

-d, --detach
Run command in background.
-i, --interactive
Keep STDIN open.
-t, --tty
Allocate pseudo-TTY.
-u, --user _user_
Username or UID.
-w, --workdir _dir_
Working directory inside container.
-e, --env _list_
Set environment variables.
--env-file _file_
Read environment variables from a file.
--detach-keys _sequence_
Override the key sequence for detaching from the container.
--privileged
Give extended privileges.

FAQ

What is the docker-exec command used for?

docker exec runs a new command in a running container's existing environment, creating a new process within the container's namespaces and cgroups. This is fundamentally different from docker run, which creates an entirely new container. The most common use case is opening an interactive shell for debugging with docker exec -it container /bin/bash, which provides direct access to the container's filesystem and running processes. Commands executed via docker exec inherit the container's environment but can be customized with user, working directory, and environment variable options. The --privileged flag grants extended capabilities useful for system administration tasks, though it should be used cautiously as it reduces container isolation.

How do I run a basic docker-exec example?

Run `docker exec [container] [command]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -d, --detach do in docker-exec?

Run command in background.