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 cp
command called to easily copy files and directories between the container and the host. This article will introduce docker cp
the use of commands and common examples in detail.
docker cp command
docker cp
command 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 IDSRC_PATH
, the path of the source file or directory within the containerDEST_PATH
, the location of the target path on the host
Note: docker cp
The 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.-R
Or-r
: copy the entire directory recursively.
SRC_PATH & DEST_PATH
SRC_PATH
and DEST_PATH
are docker cp
the 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
- when
DEST_PATH
does not exist
Create DEST_PATH
the required folder and save the file to it DEST_PATH
normally
- When
DEST_PATH
not present, and ends with /
The command will report an error, the target directory must exist
- when
DEST_PATH
exists and is a file
The target is overwritten by the contents of the source file
- When
DEST_PATH
exists and is a directory
SRC_PATH
Copy files into this directory using the basename in
SRC_PATH specifies the directory
- When
DEST_PATH
present 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
-
When
DEST_PATH
exists and is a file, copying will report an error because the directory cannot be copied into a file -
When
DEST_PATH
it does not exist, a new directory is createdDEST_PATH
as path and the contents of the source directory are copied into it
Usage example
Copy files from container to host
- Host directory already exists
tomcat_muller
The files we want to usr/local/tomcat/README.md
copy to the host study
directory, 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:
- Host directory does not exist and does not
/
end with
tomcat_muller
The files we want to usr/local/tomcat/README.md
copy to the host study/test
directory, 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 study
created test
the directory under the directory and the files were successfully copied to the specified directory.
- 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.md
study/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.
- Copy to existing file
docker cp tomcat_muller:usr/local/tomcat/README.md test.txt
Run the command and the results are as follows:
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 study
on the host test.txt
to the container. The command is as follows:
docker cp test.txt tomcat_muller:/
Run the command, the result is as follows:
Copy directory from host to container
- Target directory does not exist
We want to study/hogwarts
copy the host's directory to the container's test
directory. The command is as follows:
docker cp hogwarts tomcat_muller:test/
Run the command, the result is as follows:
- The target directory is a file
We want to copy the host study/hogwarts
directory to the container muller
, the command is as follows:
docker cp hogwarts tomcat_muller:muller/
- Directory already exists
We want to copy the host study/hogwarts
directory to the container /hogwarts
, the command is as follows:
docker cp hogwarts tomcat_muller:/hogwarts/
The results of running the command are as follows:
Copy directory from container to host
We copy the directory tomcat_muller
in the container webapps.dist
to 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:
Summarize
docker cp
command 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 cp
the command's syntax and common options, and provides several examples to help you better understand its usage. Through the flexible use docker cp
of commands, we can easily perform file operations and sharing in the Docker environment.