乐闻世界logo
搜索文章和话题

What is the purpose of the "docker exec" command?

1个答案

1

The 'docker exec' command is primarily used to execute commands inside a running Docker container. This feature is highly valuable as it enables users to interact with the container even after it has been started and is operational.

For example, if you have a running database container and need to execute a query or perform maintenance operations within the database, you can use the 'docker exec' command to start a database client command-line tool, such as 'mysql' or 'psql', directly inside the container.

The specific command format is as follows:

shell
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Where:

  • OPTIONS can include flags that control command behavior, such as -i to keep STDIN open, -t to allocate a pseudo-terminal, and others.
  • CONTAINER is the name or ID of the target container where the command will be executed.
  • COMMAND is the command to be executed inside the container.
  • ARG... are the arguments passed to the command.

For example, suppose you have a container named mycontainer running an Ubuntu system, and you want to view the current working directory inside the container. You can use the following command:

shell
docker exec -it mycontainer pwd

This will execute the pwd command inside the mycontainer container and display the current working directory.

Additionally, 'docker exec' is frequently used to start an interactive shell session, allowing users to interact directly with the container's internal environment as if operating on their local machine. For example:

shell
docker exec -it mycontainer /bin/bash

This command launches /bin/bash inside the mycontainer container in an interactive mode, enabling users to manually execute additional commands within the container.

In summary, 'docker exec' is a powerful tool provided by Docker for managing and maintaining running containers.

2024年8月9日 14:07 回复

你的答案