📜  docker exec bin sh - CSS (1)

📅  最后修改于: 2023-12-03 15:30:31.613000             🧑  作者: Mango

Docker Exec

Introduction

Docker exec is a command used to run a command inside a container. It is a powerful tool for interacting with running containers and debugging issues.

Syntax
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Options
  • -d : Detach from the container after running the command
  • -i : Interactive mode (keep STDIN open even if not attached)
  • -t : Allocate a pseudo-TTY
  • --user <USERNAME> : Run the command as a specific user
  • --workdir <DIRECTORY> : Set the working directory for the command
  • --env <KEY=VALUE> : Set an environment variable
Examples
Run a command inside a container

To run a command inside a container, use the following syntax:

docker exec <CONTAINER_NAME> <COMMAND>

For example, to run the ls command inside a container, use the following command:

docker exec my_container ls
Run a command as a specific user

To run a command as a specific user, use the --user option:

docker exec --user 1001 my_container ls
Run a command with environment variables

To run a command with environment variables, use the --env option:

docker exec --env MY_VAR=my_value my_container ls
Conclusion

Docker exec is a powerful tool for interacting with running containers. It allows you to run commands inside a container, as well as set environment variables and work in specific directories. By using this command, you can easily debug issues and interact with your containers in real-time.