Docker Exec Command: Usage Guide
In Docker, the exec command is used to execute commands within a running container. The basic syntax for using the exec command is as follows:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Among them:
- Options are optional parameters used to specify certain choices, such as -it for interactive command execution or -d for running a command in the background.
- Container: The name or ID of the container where the command will be executed.
- COMMAND: The command to be executed in the container.
- ARG: Parameters of the command.
Give an example:
- Execute the command “ls -l” in the container named “webserver” and display the output:
docker exec webserver ls -l - Run the command “service apache2 restart” in the container named “webserver” in the background mode.
- Start a bash shell interactively within the container named “webserver” using the following command: docker exec -it webserver bash
With the above commands, you can execute various commands in a Docker container to manage and debug applications within the container.