Software Testing | Detailed explanation of Docker cp command: Copy files/folders between Docker containers and hosts

Insert image description here

Introduction

Docker is a popular containerization platform that allows developers to build, package, and deploy applications in a self-contained, portable environment. When using Docker, it is often necessary to copy and share files between the Docker container and the host. Docker provides a docker cpcommand called to easily copy files and directories between the container and the host. This article will introduce docker cpthe use of commands and common examples in detail.

docker cp command

docker cpcommand is a command provided by Docker for copying files and directories between the host and the container. Its syntax is as follows:

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
  • CONTAINER, can be a running or stopped container, either name or ID
  • SRC_PATH, the path of the source file or directory within the container
  • DEST_PATH, the location of the target path on the host

Note: docker cpThe command assumes that the container path is relative to the / (root) directory of the container, and the host path is relative to the current directory where the docker cp command is executed.

optionsAvailable parameters

  • -a: Keep the copied files or directories with their original attributes, including owner, permissions, etc.
  • -L: If SRC_PATH is a symbolic link, copies the file or directory pointed to by the link.
  • -p: Preserves the timestamp of the source file or directory.
  • -ROr -r: copy the entire directory recursively.

SRC_PATH & DEST_PATH

SRC_PATHand DEST_PATHare docker cpthe key parts of the command. Whether the path is correct is related to whether the command can run successfully. Let's analyze the various situations of these two paths and explain whether they can be copied successfully under various circumstances.

SRC_PATH when specifying a file
  1. when DEST_PATHdoes not exist

Create DEST_PATHthe required folder and save the file to it DEST_PATH normally

  1. When DEST_PATH not present, and ends with /

The command will report an error, the target directory must exist

  1. when DEST_PATHexists and is a file

The target is overwritten by the contents of the source file

  1. When DEST_PATH exists and is a directory

SRC_PATHCopy files into this directory using the basename in

SRC_PATH specifies the directory
  1. When DEST_PATHpresent and a directory, replication will copy normally
  • SRC_PATH does not end with /., the source directory is copied to this directory
  • SRC_PATH ends with /. into which the contents of the source directory are copied
  1. When DEST_PATHexists and is a file, copying will report an error because the directory cannot be copied into a file

  2. When DEST_PATH it does not exist, a new directory is created DEST_PATH as path and the contents of the source directory are copied into it

Usage example

Copy files from container to host
  1. Host directory already exists

tomcat_mullerThe files we want to usr/local/tomcat/README.mdcopy to the host studydirectory, the command is as follows:

docker cp tomcat_muller:usr/local/tomcat/README.md ./

Run the command, the result is as shown below, successfully copied to the specified directory:

Insert image description here

  1. Host directory does not exist and does not /end with

tomcat_mullerThe files we want to usr/local/tomcat/README.mdcopy to the host study/testdirectory, the command is as follows:

docker cp tomcat_muller:usr/local/tomcat/README.md ./test

Run the command. The result is as shown below. We successfully studycreated testthe directory under the directory and the files were successfully copied to the specified directory.

Insert image description here

  1. The hosts directory does not exist and /ends with

We want to copy the file to the path of the host tomcat_muller. The command is as follows:usr/local/tomcat/README.mdstudy/test/

docker cp tomcat_muller:usr/local/tomcat/README.md ./test/

Run the command, and the result is as shown below. Because the path does not exist, an error will be reported. There is no such directory.

Insert image description here

  1. Copy to existing file
docker cp tomcat_muller:usr/local/tomcat/README.md test.txt

Run the command and the results are as follows:

Insert image description here

The operation of copying directories is similar to that of copying files, so we won’t go into details here.

Copy files from host to container

We want to copy the files studyon the host test.txtto the container. The command is as follows:

docker cp test.txt tomcat_muller:/

Run the command, the result is as follows:
Insert image description here

Copy directory from host to container
  1. Target directory does not exist

We want to study/hogwartscopy the host's directory to the container's testdirectory. The command is as follows:

docker cp hogwarts tomcat_muller:test/

Run the command, the result is as follows:

Insert image description here

  1. The target directory is a file

We want to copy the host study/hogwartsdirectory to the container muller, the command is as follows:

docker cp hogwarts tomcat_muller:muller/
  1. Directory already exists

We want to copy the host study/hogwartsdirectory to the container /hogwarts, the command is as follows:

docker cp hogwarts tomcat_muller:/hogwarts/

The results of running the command are as follows:

Insert image description here

Copy directory from container to host

We copy the directory tomcat_mullerin the container webapps.distto the host usr/local/study, the command is as follows:

docker cp tomcat_muller:/usr/local/tomcat/webapps.dist /home/muller/study

Run the command, the result is as follows:

Insert image description here

Summarize

docker cpcommand is a convenient tool for copying files and directories between Docker containers and the host machine. With simple commands and parameters, we can easily transfer files between containers and hosts. This article describes docker cpthe command's syntax and common options, and provides several examples to help you better understand its usage. Through the flexible use docker cpof commands, we can easily perform file operations and sharing in the Docker environment.

For more technical information, please click!

Guess you like

Origin blog.csdn.net/Tester_muller/article/details/131678630