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

How to copy files from host to Docker container?

1个答案

1

To copy files from the host to a Docker container, you can use the docker cp command. This command allows you to copy files or directories from the Docker host to the container, or from the container to the Docker host. Here is the basic usage of this command:

Copying from Host to Container

Suppose you have a file named example.txt in the current directory on the host, and you want to copy it to the /usr/share/data directory inside the Docker container named mycontainer. You can use the following command:

bash
docker cp example.txt mycontainer:/usr/share/data/example.txt

The format of this command is docker cp <source path> <target path>. In this example, example.txt is the source file path, and mycontainer:/usr/share/data/example.txt specifies the container name and target path.

Practical Example

Let's explore a practical scenario. Suppose you are developing a web application and need to copy a configuration file from your development environment to a running Docker container. You can place the configuration file on your host and use the docker cp command to copy it to the appropriate location inside the container.

For example, your configuration file named config.json is located in your working directory, and you need to copy it to the /app/config directory inside the container named webapp. The command would be:

bash
docker cp config.json webapp:/app/config/config.json

After executing this command, the application inside the container will be able to access the updated configuration file without rebuilding the image or restarting the container.

In this way, the docker cp command provides a simple and effective method to quickly transfer necessary files and data into Docker containers during development and deployment. This is highly effective for rapid iteration and testing, significantly improving development efficiency.

2024年8月10日 00:23 回复

你的答案