How to use Docker Cp to copy files between host and containers -
Need to get files in or out of a Docker container? The docker cp
The command allows you to copy between host and container file systems so that you can add configuration details, create backups, and restore existing data.
In this section:
Basic syntax
docker cp
accepts source and destination paths as its two arguments:
docker cp example.txt my-container: /example.txt
Here example.txt
is copying your working directory to /example.txt
my-container
container. You can reverse the two arguments to copy /example.txt
out of the container and into your working directory.
The argument referring to the container path must be preceded by a container identifier or name followed by a colon (:
). You can find the ID or name of a running container with docker ps
.
Each docker cp
The command needs a local file system path and a container path - you cannot copy directly between two containers. Use a multi-step procedure if you need to, copying first from the source container to your file system, then from the new local path to the target container.
Copying entire directories
docker cp
can also recursively copy directories:
docker cp / home / demo / website apache-container: / var / www / html /.
Docker will copy everything to /home/demo/website
and transfer it to /var/www/html
.
Copy behavior
When you copy a file, Docker creates a new file at the destination if it doesn't already exist. The existing files are overwritten with the new content. When the destination is a directory, the file is copied there using the source file name. An exception is when the specified destination ends with a /
, designating a directory, but the path does not already exist. In this scenario, an error will be generated.
The process is a bit more complicated for directory copies. A new directory will be created at the destination with the contents of the source directory, if the destination path does not already exist. When it exists, the behavior differs depending on whether or not you have included a /.
component in the path.
/.
is present - The source directory is copied to the existing destination directory./.
is not present - The Teneur from the source directory is copied to the destination.
The subtle distinction dictates whether a new subdirectory is created inside the destination.
Order Limitations
Despite its name, docker cp
is not a full implementation of the cp
shell command. The cp
flags are not supported except for -a
et -L
:
-a
- Archive mode, which preserves user and group details on copied files.-L
- Follow symbolic links in the source directory to copy the content of the link targets, rather than the links themselves.
For more advanced use cases where selective copying is required, you will need to use a different approach.
Use link mounts to copy files
Docker volumes provide another way to move files between containers and your host. Binding a local directory into a container allows you to access its contents from your host file system, eliminating the need to use docker cp
.
docker run -v / example / host / directory: / container / path my-image: latest
The content of /example/host/directory
path are mounted in the container filesystem at /container/path
. You can interact with these files outside of Docker using familiar tools such as cp
, rsync
, and your graphics file browser.
This technique is only useful when working with a single container directory. It does not work well when copying from arbitrary locations because you need to know in advance which paths you will use when creating the container.
You should also be wary of file system permissions: files created in the container will usually be owned by root
. This can create difficult scenarios on the host where you cannot edit or delete files inside the linked directory. Use the chown
command on the host and inside the container to change ownership depending on the environment if needed.
What about COPY
in Dockerfiles?
docker cp
can sometimes be confused with the COPY
instructions in Dockerfiles. It's important to recognize that these two features serve very different use cases.
COPY
cannot be used to move files between your host and a running container. This is to put files in images during the construction process:
COPY / home / me / my-website / var / www / html /.
Here, the website source code is copied into an image as part of a build. It is a unique process. Each container started from the image would include the website source as it was at run time docker build
.
docker cp
allows you to replace this source code with a newer version once a container is running. COPY
instructions are used to make files part of a static image; cp
the orders interact with the containers directly.
When to copy files with Docker?
Manually copying files from your host to a Docker container, or vice versa, should be a relatively rare occurrence. Images are meant to be self-sufficient, so they should contain everything you need to start an instance. The configuration is usually managed through environment variables.
Containers that need to persistently store data should use Docker volumes. Volumes allow data to survive any container, so you don't need to docker cp
before replacing an instance. When performing backups, copy the volumes from your host, instead of removing the files from the containers.
docker cp
is particularly useful when debugging containers or when working in a development environment. Sometimes you have to manually inject a temporary configuration file or extract a buried log. Using docker cp
is faster and more convenient than rebuilding the entire image each time you change the code.
Always remember that the copied files in containers will only persist as long as the container is alive. Starting another container from the same image will give you a clean slate, minus the files you added with docker cp
.
In this section:
docker cp
allows you to move files between your host and your Docker containers. It works with files and directories but lacks most of the advanced shell features cp
order.
Regular use of docker cp
indicates a potential deviation from best container practices. It's wise to treat it as a handy tool for development, rather than an integral part of working with containers. Long-term file persistence should be implemented with volumes as they are first-class components in the Docker ecosystem.