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:

  1. 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.
  2. Container: The name or ID of the container where the command will be executed.
  3. COMMAND: The command to be executed in the container.
  4. ARG: Parameters of the command.

Give an example:

  1. Execute the command “ls -l” in the container named “webserver” and display the output:
    docker exec webserver ls -l
  2. Run the command “service apache2 restart” in the container named “webserver” in the background mode.
  3. 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.

bannerAds