Terminus by Warp
Copy File To Docker Container

Copy File To Docker Container

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 your local machine to a running container.

 $ docker cp src_path container:dest_path

Where:

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

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

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

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

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:

 for file in *.txt; do docker cp $file container:/app; done

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.

 $ docker cp ./dir container:/app/

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

 $ docker cp ./file.txt container:tmp

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

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

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

Or in their absolute form:

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

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:

Where:

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

 COPY file.txt dir /app

These source paths may also contain wildcards:

 COPY *.txt /app

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:

 COPY . /app

But you cannot copy files that are outside of the build context of the Dockerfile:

 # This command will not work
 COPY ../file.txt /app

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 dir /app

[#copy-destination-path-options]COPY destination path options[#copy-destination-path-options]

The destination path may either be an absolute path:

 COPY file /app

Or a path relative to the one specified in the `WORKDIR` instruction:

 WORKDIR /app
 COPY file .

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:

 $ mkdir dir && cd dir

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.