Terminus by Warp
Copy File From Container To Host

Copy File From Container To Host

Razvan Ludosanu
Razvan Ludosanu
Founder, learnbackend.dev

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.

 $ docker cp container:src_path dest_path 

Where:

  • [.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:

 $ docker cp container:/app/file.txt .

Alternatively, you can copy and rename at the same time, by providing the full destination path:

 $ docker cp container:/app/script.js ./index.js

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.

 $ docker cp container:/app/server/ ./server

[#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:

 $ docker cp container:tmp .
 $ docker cp container:/tmp .

Destination paths on the other hand, can be written either in their relative form:

 $ docker cp container:/tmp/file.txt ./file.txt

Or in their absolute form:

 $ docker cp container:/tmp/file.txt /home/johndoe/Documents/file.txt

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.

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own
brew install --cask warp
Copied!
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist or join the Windows waitlist
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.