2020-10-07 12:45:08 +01:00
|
|
|
# docker exec
|
|
|
|
|
|
|
|
> Execute a command on an already running Docker container.
|
|
|
|
> More information: <https://docs.docker.com/engine/reference/commandline/exec/>.
|
|
|
|
|
|
|
|
- Enter an interactive shell session on an already-running container:
|
|
|
|
|
2020-10-28 18:35:36 +00:00
|
|
|
`docker exec --interactive --tty {{container_name}} {{/bin/bash}}`
|
2020-10-07 12:45:08 +01:00
|
|
|
|
|
|
|
- Run a command in the background (detached) on a running container:
|
|
|
|
|
2020-10-28 18:35:36 +00:00
|
|
|
`docker exec --detach {{container_name}} {{command}}`
|
2020-10-07 12:45:08 +01:00
|
|
|
|
|
|
|
- Select the working directory for a given command to execute into:
|
|
|
|
|
2023-12-30 15:34:29 +00:00
|
|
|
`docker exec --interactive --tty --workdir {{path/to/directory}} {{container_name}} {{command}}`
|
2020-10-07 12:45:08 +01:00
|
|
|
|
2022-12-04 07:53:34 +00:00
|
|
|
- Run a command in background on existing container but keep `stdin` open:
|
2020-10-07 12:45:08 +01:00
|
|
|
|
2020-10-28 18:35:36 +00:00
|
|
|
`docker exec --interactive --detach {{container_name}} {{command}}`
|
2020-10-07 12:45:08 +01:00
|
|
|
|
2021-08-15 18:59:09 +01:00
|
|
|
- Set an environment variable in a running Bash session:
|
2020-10-07 12:45:08 +01:00
|
|
|
|
2020-10-28 18:35:36 +00:00
|
|
|
`docker exec --interactive --tty --env {{variable_name}}={{value}} {{container_name}} {{/bin/bash}}`
|
|
|
|
|
|
|
|
- Run a command as a specific user:
|
|
|
|
|
|
|
|
`docker exec --user {{user}} {{container_name}} {{command}}`
|