Copy File From Container To Host
Last Updated on 2023-03-15
The short answer
The [.inline-code]docker cp[.inline-code] command allows you to copy files and directories from a container's file system to your local machine, whether the container is running or stopped.
- [.inline-code]src_path[.inline-code] is the path on the container of the file you want to copy.
- [.inline-code]container[.inline-code] is the name or the ID of the container you want to copy files from.
- [.inline-code]dest_path[.inline-code] is the path on your local machine of the directory you want to copy files to.
To get the name or ID of the container you want to copy files from, you can use the [.inline-code]docker ps -a[.inline-code] command, which will output the list of all the running and stopped containers.
[#easily-recall]Use Warp's Workflows feature to easily recall the syntax[#easily-recall]
If you’re using Warp as your terminal and you need to quickly retrieve this command, you can use Warp's Workflows feature by pressing [.inline-code]CTRL-SHIFT-R[.inline-code] and typing [.inline-code]copy from container[.inline-code]:
Then pressing [.inline-code]ENTER[.inline-code] to use the suggested command:
[#docker-cp-local-host]Using [.inline-code]docker cp[.inline-code] to copy files to the local host[#docker-cp-local-host]
[#individual-files]Copying individual files[#individual-files]
To copy a file from a container to your local host, you can specify the destination directory using either its relative or absolute path:
Alternatively, you can copy and rename at the same time, by providing the full destination path:
Note that it is not possible to copy multiple files at once using the [.inline-code]docker cp[.inline-code] command, unless you copy the entire directory that contains them (if located in the same directory).
[#entire-directories]Copying entire directories recursively[#entire-directories]
When copying a directory, the [.inline-code]docker cp[.inline-code] command will behave like the Unix [.inline-code]cp -a[.inline-code] command, which means that the directory will be copied recursively with permission preserved if possible.
[#absolute-and-relative-paths]Copying absolute and relative paths, such as a parent directory[#absolute-and-relative-paths]
The [.inline-code]docker cp[.inline-code] assumes container paths are relative to the container's root directory (i.e. [.inline-code]/[.inline-code]). A source path can therefore be written with or without the initial forward slash, as [.inline-code]docker cp[.inline-code] will see these two commands as identical:
Destination paths on the other hand, can be written either in their relative form:
Or in their absolute form:
You can learn more about the [.inline-code]docker cp[.inline-code] command in the official Docker documentation and in our article on how to copy files and directories to a running Docker container.