View Docker cp progress

Original link: https://blog.frytea.com/archives/762/

75

The docker cp command itself does not provide a progress viewing function. However, you can use a third-party tool such as pv (Pipe Viewer) to view the progress while executing docker cp. Here are the steps to use pv to view the progress of docker cp:

First, install pv. Depending on your operating system, install pv using the appropriate package manager.

 # Ubuntu/Debian: sudo apt-get install pv # CentOS/RHEL: sudo yum install pv # macOS(使用Homebrew): brew install pv

container to host

To copy files from the container to the host and view the progress, execute the following command:

 docker exec <container_name> tar -cf - <source_path_inside_container> | pv | tar -xf - -C <destination_path_on_host>

Replace <container_name> with the container name, <source_path_inside_container> with the source file/directory path inside the container, and <destination_path_on_host> with the destination file/directory path on the host.

For example, to copy the /data directory from a container named my_container to the /tmp/data directory on the host:

 docker exec my_container tar -cf - /data | pv | tar -xf - -C /tmp/data

pv will display a progress bar with transferred, remaining and total bytes.

host to container

If you want to copy from the host to the container and see the progress, you can use the following command:

 tar -cf - <source_path_on_host> | pv | docker exec -i <container_name> tar -xf - -C <destination_path_inside_container>

Replace <source_path_on_host> with the source file/directory path on the host, <container_name> with the container name, and <destination_path_inside_container> with the destination file/directory path inside the container.

For example, to copy from the /tmp/data directory on the host to the /data directory in a container named my_container:

 tar -cf - /tmp/data | pv | docker exec -i my_container tar -xf - -C /data

This command will package the files/directories on the host through the tar command, then view the progress through pv, and send the data to the container through the pipeline. In the container, the tar command will unpack the data into the specified directory. pv will display a progress bar with transferred, remaining and total bytes.

This article is transferred from: https://blog.frytea.com/archives/762/
This site is only for collection, and the copyright belongs to the original author.