2015-06-21 16:41:38 -04:00
|
|
|
# cp
|
|
|
|
|
2023-01-06 13:04:05 -05:00
|
|
|
<!---MARKER_GEN_START-->
|
2016-07-07 14:43:18 -04:00
|
|
|
Copy files/folders between a container and the local filesystem
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2016-07-07 14:43:18 -04:00
|
|
|
Use '-' as the source to read a tar archive from stdin
|
|
|
|
and extract it to a directory destination in a container.
|
|
|
|
Use '-' as the destination to stream a tar archive of a
|
|
|
|
container source to stdout.
|
|
|
|
|
2023-01-06 13:04:05 -05:00
|
|
|
### Aliases
|
|
|
|
|
|
|
|
`docker container cp`, `docker cp`
|
|
|
|
|
|
|
|
### Options
|
|
|
|
|
|
|
|
| Name | Type | Default | Description |
|
|
|
|
|:----------------------|:-----|:--------|:-------------------------------------------------------------------------------------------------------------|
|
|
|
|
| `-a`, `--archive` | | | Archive mode (copy all uid/gid information) |
|
|
|
|
| `-L`, `--follow-link` | | | Always follow symbol link in SRC_PATH |
|
|
|
|
| `-q`, `--quiet` | | | Suppress progress output during copy. Progress output is automatically suppressed if no terminal is attached |
|
|
|
|
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
|
2023-01-06 13:04:05 -05:00
|
|
|
<!---MARKER_GEN_END-->
|
2015-10-01 03:56:39 -04:00
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
## Description
|
|
|
|
|
2015-10-01 03:56:39 -04:00
|
|
|
The `docker cp` utility copies the contents of `SRC_PATH` to the `DEST_PATH`.
|
|
|
|
You can copy from the container's file system to the local machine or the
|
|
|
|
reverse, from the local filesystem to the container. If `-` is specified for
|
|
|
|
either the `SRC_PATH` or `DEST_PATH`, you can also stream a tar archive from
|
|
|
|
`STDIN` or to `STDOUT`. The `CONTAINER` can be a running or stopped container.
|
2016-02-16 22:54:45 -05:00
|
|
|
The `SRC_PATH` or `DEST_PATH` can be a file or directory.
|
2015-10-01 03:56:39 -04:00
|
|
|
|
2016-10-19 13:25:45 -04:00
|
|
|
The `docker cp` command assumes container paths are relative to the container's
|
2015-10-01 03:56:39 -04:00
|
|
|
`/` (root) directory. This means supplying the initial forward slash is optional;
|
|
|
|
The command sees `compassionate_darwin:/tmp/foo/myfile.txt` and
|
|
|
|
`compassionate_darwin:tmp/foo/myfile.txt` as identical. Local machine paths can
|
|
|
|
be an absolute or relative value. The command interprets a local machine's
|
|
|
|
relative paths as relative to the current working directory where `docker cp` is
|
|
|
|
run.
|
|
|
|
|
|
|
|
The `cp` command behaves like the Unix `cp -a` command in that directories are
|
2015-05-15 14:35:41 -04:00
|
|
|
copied recursively with permissions preserved if possible. Ownership is set to
|
2015-10-01 03:56:39 -04:00
|
|
|
the user and primary group at the destination. For example, files copied to a
|
|
|
|
container are created with `UID:GID` of the root user. Files copied to the local
|
|
|
|
machine are created with the `UID:GID` of the user which invoked the `docker cp`
|
2017-06-21 06:20:02 -04:00
|
|
|
command. However, if you specify the `-a` option, `docker cp` sets the ownership
|
|
|
|
to the user and primary group at the source.
|
|
|
|
If you specify the `-L` option, `docker cp` follows any symbolic link
|
2023-12-13 09:16:56 -05:00
|
|
|
in the `SRC_PATH`. `docker cp` doesn't create parent directories for
|
|
|
|
`DEST_PATH` if they don't exist.
|
2015-05-15 14:35:41 -04:00
|
|
|
|
|
|
|
Assuming a path separator of `/`, a first argument of `SRC_PATH` and second
|
2015-10-01 03:56:39 -04:00
|
|
|
argument of `DEST_PATH`, the behavior is as follows:
|
2015-05-15 14:35:41 -04:00
|
|
|
|
|
|
|
- `SRC_PATH` specifies a file
|
2015-10-01 03:56:39 -04:00
|
|
|
- `DEST_PATH` does not exist
|
|
|
|
- the file is saved to a file created at `DEST_PATH`
|
|
|
|
- `DEST_PATH` does not exist and ends with `/`
|
2015-05-15 14:35:41 -04:00
|
|
|
- Error condition: the destination directory must exist.
|
2015-10-01 03:56:39 -04:00
|
|
|
- `DEST_PATH` exists and is a file
|
|
|
|
- the destination is overwritten with the source file's contents
|
|
|
|
- `DEST_PATH` exists and is a directory
|
2015-05-15 14:35:41 -04:00
|
|
|
- the file is copied into this directory using the basename from
|
|
|
|
`SRC_PATH`
|
|
|
|
- `SRC_PATH` specifies a directory
|
2015-10-01 03:56:39 -04:00
|
|
|
- `DEST_PATH` does not exist
|
|
|
|
- `DEST_PATH` is created as a directory and the *contents* of the source
|
2015-05-15 14:35:41 -04:00
|
|
|
directory are copied into this directory
|
2015-10-01 03:56:39 -04:00
|
|
|
- `DEST_PATH` exists and is a file
|
2015-05-15 14:35:41 -04:00
|
|
|
- Error condition: cannot copy a directory to a file
|
2015-10-01 03:56:39 -04:00
|
|
|
- `DEST_PATH` exists and is a directory
|
2017-01-13 08:54:44 -05:00
|
|
|
- `SRC_PATH` does not end with `/.` (that is: _slash_ followed by _dot_)
|
2015-05-15 14:35:41 -04:00
|
|
|
- the source directory is copied into this directory
|
2017-01-13 08:54:44 -05:00
|
|
|
- `SRC_PATH` does end with `/.` (that is: _slash_ followed by _dot_)
|
2015-05-15 14:35:41 -04:00
|
|
|
- the *content* of the source directory is copied into this
|
|
|
|
directory
|
|
|
|
|
2015-10-01 03:56:39 -04:00
|
|
|
The command requires `SRC_PATH` and `DEST_PATH` to exist according to the above
|
2015-05-15 14:35:41 -04:00
|
|
|
rules. If `SRC_PATH` is local and is a symbolic link, the symbolic link, not
|
2016-10-19 13:25:45 -04:00
|
|
|
the target, is copied by default. To copy the link target and not the link, specify
|
2015-10-01 03:56:39 -04:00
|
|
|
the `-L` option.
|
2015-05-15 14:35:41 -04:00
|
|
|
|
2015-10-01 03:56:39 -04:00
|
|
|
A colon (`:`) is used as a delimiter between `CONTAINER` and its path. You can
|
|
|
|
also use `:` when specifying paths to a `SRC_PATH` or `DEST_PATH` on a local
|
|
|
|
machine, for example `file:name.txt`. If you use a `:` in a local machine path,
|
|
|
|
you must be explicit with a relative or absolute path, for example:
|
2015-05-15 14:35:41 -04:00
|
|
|
|
|
|
|
`/path/to/file:name.txt` or `./file:name.txt`
|
|
|
|
|
2022-03-09 05:14:58 -05:00
|
|
|
## Examples
|
|
|
|
|
|
|
|
Copy a local file into container
|
|
|
|
|
|
|
|
```console
|
2022-04-01 15:31:14 -04:00
|
|
|
$ docker cp ./some_file CONTAINER:/work
|
2022-03-09 05:14:58 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
Copy files from container to local path
|
|
|
|
|
|
|
|
```console
|
|
|
|
$ docker cp CONTAINER:/var/logs/ /tmp/app_logs
|
|
|
|
```
|
|
|
|
|
2024-04-26 14:16:51 -04:00
|
|
|
Copy a file from container to stdout. Note `cp` command produces a tar stream
|
2022-03-09 05:14:58 -05:00
|
|
|
|
|
|
|
```console
|
|
|
|
$ docker cp CONTAINER:/var/logs/app.log - | tar x -O | grep "ERROR"
|
|
|
|
```
|
|
|
|
|
|
|
|
### Corner cases
|
|
|
|
|
2023-12-13 09:16:56 -05:00
|
|
|
It isn't possible to copy certain system files such as resources under
|
2024-02-13 07:40:53 -05:00
|
|
|
`/proc`, `/sys`, `/dev`, [tmpfs](container_run.md#tmpfs), and mounts created by
|
2016-05-23 05:10:14 -04:00
|
|
|
the user in the container. However, you can still copy such files by manually
|
2017-02-07 18:42:48 -05:00
|
|
|
running `tar` in `docker exec`. Both of the following examples do the same thing
|
|
|
|
in different ways (consider `SRC_PATH` and `DEST_PATH` are directories):
|
2016-05-23 05:10:14 -04:00
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2017-07-29 14:44:50 -04:00
|
|
|
$ docker exec CONTAINER tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | tar Cxf DEST_PATH -
|
2017-02-07 18:42:48 -05:00
|
|
|
```
|
2016-05-23 05:10:14 -04:00
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2017-07-29 14:44:50 -04:00
|
|
|
$ tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | docker exec -i CONTAINER tar Cxf DEST_PATH -
|
2017-02-07 18:42:48 -05:00
|
|
|
```
|
2015-05-15 14:35:41 -04:00
|
|
|
|
2015-10-01 03:56:39 -04:00
|
|
|
Using `-` as the `SRC_PATH` streams the contents of `STDIN` as a tar archive.
|
|
|
|
The command extracts the content of the tar to the `DEST_PATH` in container's
|
|
|
|
filesystem. In this case, `DEST_PATH` must specify a directory. Using `-` as
|
2016-02-16 22:54:45 -05:00
|
|
|
the `DEST_PATH` streams the contents of the resource as a tar archive to `STDOUT`.
|