Copy File From Container To Host
Razvan Ludosanu
Founder, learnbackend.dev
Published: 8/3/2023
The short answer
The docker cp 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
Run in Warp
Where:
- src_path is the path on the container of the file you want to copy.
- container is the name or the ID of the container you want to copy files from.
- dest_path 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 docker ps -a command, which will output the list of all the running and stopped containers.
Use Warp's Workflows feature to easily recall the syntax
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 CTRL-SHIFT-R and typing copy from container:
Then pressing ENTER to use the suggested command:
Using docker cp to copy files to the local host
Copying 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 .
Run in Warp
Alternatively, you can copy and rename at the same time, by providing the full destination path:
$ docker cp container:/app/script.js ./index.js
Run in Warp
Note that it is not possible to copy multiple files at once using the docker cp command, unless you copy the entire directory that contains them (if located in the same directory).
Copying entire directories recursively
When copying a directory, the docker cp command will behave like the Unix cp -a command, which means that the directory will be copied recursively with permission preserved if possible.
$ docker cp container:/app/server/ ./server
Run in Warp
Copying absolute and relative paths, such as a parent directory
The docker cp assumes container paths are relative to the container's root directory (i.e. /). A source path can therefore be written with or without the initial forward slash, as docker cp will see these two commands as identical:
$ docker cp container:tmp .
$ docker cp container:/tmp .
Run in Warp
Destination paths on the other hand, can be written either in their relative form:
$ docker cp container:/tmp/file.txt ./file.txt
Run in Warp
Or in their absolute form:
$ docker cp container:/tmp/file.txt /home/johndoe/Documents/file.txt
Run in Warp
You can learn more about the docker cp command in the official Docker documentation and in our article on how to copy files and directories to a running Docker container.
Written by
Razvan Ludosanu
Founder, learnbackend.dev
Filed Under
Related Articles
Override the Container Entrypoint With docker run
Learn how to override and customize the entrypoint of a Docker container using the docker run command.
The Dockerfile ARG Instruction
Learn how to define and set build-time variables for Docker images using the ARG instruction and the --build-arg flag.
Start a Docker Container
Learn how to start a new Docker container from an image in both the foreground and the background using the docker-run command.
Stop All Docker Containers
How to gracefully shutdown running containers and forcefully kill unresponsive containers with signals in Docker using the docker-stop and docker-kill commands.
Use An .env File In Docker
Learn how to write and use .env files in Docker to populate the environment of containers on startup.
Run SSH In Docker
Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.
Launch MySQL Using Docker Compose
Learn how to launch a MySQL container in Docker Compose.
Execute in a Docker Container
Learn how to execute one or multiple commands in a Docker container using the docker exec command.
Expose Docker Container Ports
Learn how to publish and expose Docker container ports using the docker run command and Dockerfiles.
Restart Containers In Docker Compose
Learn how to restart and rebuild one or more containers in Docker Compose.
Output Logs in Docker Compose
Learn how to output, monitor, customize and filter the logs of the containers related to one or more services in Docker Compose
Rename A Docker Image
Learn how to rename Docker images locally and remotely using the docker tag command.