Copy File To Docker Container
Last Updated on 2023-03-02
The short answer
The [.inline-code]docker cp[.inline-code] command allows you to copy files and directories from your local machine to a running container.
- [.inline-code]src_path[.inline-code] is the path on your local machine 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 to.
- [.inline-code]dest_path[.inline-code] is the path in the container of the directory you want to copy files to.
To get the name or ID of the container you want to copy files to, you can use the [.inline-code]docker ps[.inline-code] command, which will output the list of all the running containers.
[#warp-workflows]Use Warp's Workflows feature to easily recall the syntax[#warp-workflows]
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 files[.inline-code]:
Then pressing [.inline-code]ENTER[.inline-code] to use the suggested command:
[#copy-files-to-docker-containers]Using [.inline-code]docker cp[.inline-code] to copy files to docker containers[#copy-files-to-docker-containers]
[#copy-individual-files]Copying individual files[#copy-individual-files]
To copy a file to a container, you can either use its relative or absolute path, and specify the destination directory as follows:
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 either use a [.inline-code]for[.inline-code] loop:
Or 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.
[#copying-paths]Copying absolute and relative paths, such as a parent directory[#copying-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 destination 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:
Source 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.
[#copying-files-build-time]Copying files at build-time using a Dockerfile[#copying-files-build-time]
A Dockerfile is a text file that contains all the necessary instructions for building a Docker image. These instructions can be used for installing and configuring command line tools, declaring environment variables, exposing ports, copying files from the local environment, and so on.
To copy files and directory at build-time, you can use the [.inline-code]COPY[.inline-code] instruction:
- [.inline-code]src[.inline-code] is the path on your local machine of the file(s) you want to copy.
- [.inline-code]dest[.inline-code] is the path on the container you want to copy the file to.
[#copy-source-path-options]COPY source path options[#copy-source-path-options]
Unlike with the [.inline-code]docker cp[.inline-code] command, the [.inline-code]COPY[.inline-code] instruction allows you to copy multiple sources at once–which can be files, directories, or both–by separating them with a whitespace character:
These source paths may also contain wildcards:
It is important to note that the paths of the source files and directories will be interpreted as relative to the source of the context of the build, which means that only the files contained in the directory where the Dockerfile is located will be copied.
You can therefore copy the entire directory of the build context using the dot notation:
But you cannot copy files that are outside of the build context of the Dockerfile:
Also note that when copying a directory, only the content of the directory will be copied, but not the directory itself.
For example, the following command will copy the content of the [.inline-code]dir[.inline-code] directory into the [.inline-code]/app[.inline-code] directory:
[#copy-destination-path-options]COPY destination path options[#copy-destination-path-options]
The destination path may either be an absolute path:
Or a path relative to the one specified in the `WORKDIR` instruction:
The [.inline-code]WORKDIR[.inline-code] instruction is used to set the working directory for instructions such as [.inline-code]COPY[.inline-code], [.inline-code]ADD[.inline-code], [.inline-code]RUN[.inline-code], etc. If the specified directory doesn't exist, it will be automatically created. In Unix, this is the equivalent of the following command: