Skip to content

Commit e54b1e0

Browse files
author
Josh Hawn
committed
docs: Updated for docker cp and its API changes
Documented changes to API to enable new `docker cp` behavior. Added documentation on `docker cp` usage and behavior. Docker-DCO-1.1-Signed-off-by: Josh Hawn <[email protected]> (github: jlhawn)
1 parent 418135e commit e54b1e0

File tree

4 files changed

+323
-40
lines changed

4 files changed

+323
-40
lines changed

docs/reference/api/docker_remote_api.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ Running `docker rmi` emits an **untag** event when removing an image name. The
6868

6969
### What's new
7070

71+
`GET /containers/(id)/archive`
72+
73+
**New!**
74+
Get an archive of filesystem content from a container.
75+
76+
`PUT /containers/(id)/archive`
77+
78+
**New!**
79+
Upload an archive of content to be extracted to an
80+
existing directory inside a container's filesystem.
81+
82+
`POST /containers/(id)/copy`
83+
84+
**Deprecated!**
85+
This copy endpoint has been deprecated in favor of the above `archive` endpoint
86+
which can be used to download files and directories from a container.
87+
7188
**New!**
7289
The `hostConfig` option now accepts the field `GroupAdd`, which specifies a list of additional
7390
groups that the container process will run as.

docs/reference/api/docker_remote_api_v1.20.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,8 @@ Status Codes:
10391039

10401040
Copy files or folders of container `id`
10411041

1042+
**Deprecated** in favor of the `archive` endpoint below.
1043+
10421044
**Example request**:
10431045

10441046
POST /containers/4fa6e0f0c678/copy HTTP/1.1
@@ -1061,6 +1063,120 @@ Status Codes:
10611063
- **404** – no such container
10621064
- **500** – server error
10631065

1066+
### Retrieving information about files and folders in a container
1067+
1068+
`HEAD /containers/(id)/archive`
1069+
1070+
See the description of the `X-Docker-Container-Path-Stat` header in the
1071+
folowing section.
1072+
1073+
### Get an archive of a filesystem resource in a container
1074+
1075+
`GET /containers/(id)/archive`
1076+
1077+
Get an tar archive of a resource in the filesystem of container `id`.
1078+
1079+
Query Parameters:
1080+
1081+
- **path** - resource in the container's filesystem to archive. Required.
1082+
1083+
If not an absolute path, it is relative to the container's root directory.
1084+
The resource specified by **path** must exist. To assert that the resource
1085+
is expected to be a directory, **path** should end in `/` or `/.`
1086+
(assuming a path separator of `/`). If **path** ends in `/.` then this
1087+
indicates that only the contents of the **path** directory should be
1088+
copied. A symlink is always resolved to its target.
1089+
1090+
**Note**: It is not possible to copy certain system files such as resources
1091+
under `/proc`, `/sys`, `/dev`, and mounts created by the user in the
1092+
container.
1093+
1094+
**Example request**:
1095+
1096+
GET /containers/8cce319429b2/archive?path=/root HTTP/1.1
1097+
1098+
**Example response**:
1099+
1100+
HTTP/1.1 200 OK
1101+
Content-Type: application/x-tar
1102+
X-Docker-Container-Path-Stat: eyJuYW1lIjoicm9vdCIsInBhdGgiOiIvcm9vdCIsInNpemUiOjQwOTYsIm1vZGUiOjIxNDc0ODQwOTYsIm10aW1lIjoiMjAxNC0wMi0yN1QyMDo1MToyM1oifQ==
1103+
1104+
{{ TAR STREAM }}
1105+
1106+
On success, a response header `X-Docker-Container-Path-Stat` will be set to a
1107+
base64-encoded JSON object containing some filesystem header information about
1108+
the archived resource. The above example value would decode to the following
1109+
JSON object (whitespace added for readability):
1110+
1111+
{
1112+
"name": "root",
1113+
"path": "/root",
1114+
"size": 4096,
1115+
"mode": 2147484096,
1116+
"mtime": "2014-02-27T20:51:23Z"
1117+
}
1118+
1119+
A `HEAD` request can also be made to this endpoint if only this information is
1120+
desired.
1121+
1122+
Status Codes:
1123+
1124+
- **200** - success, returns archive of copied resource
1125+
- **400** - client error, bad parameter, details in JSON response body, one of:
1126+
- must specify path parameter (**path** cannot be empty)
1127+
- not a directory (**path** was asserted to be a directory but exists as a
1128+
file)
1129+
- **404** - client error, resource not found, one of:
1130+
– no such container (container `id` does not exist)
1131+
- no such file or directory (**path** does not exist)
1132+
- **500** - server error
1133+
1134+
### Extract an archive of files or folders to a directory in a container
1135+
1136+
`PUT /containers/(id)/archive`
1137+
1138+
Upload a tar archive to be extracted to a path in the filesystem of container
1139+
`id`.
1140+
1141+
Query Parameters:
1142+
1143+
- **path** - path to a directory in the container
1144+
to extract the archive's contents into. Required.
1145+
1146+
If not an absolute path, it is relative to the container's root directory.
1147+
The **path** resource must exist.
1148+
- **noOverwriteDirNonDir** - If "1", "true", or "True" then it will be an error
1149+
if unpacking the given content would cause an existing directory to be
1150+
replaced with a non-directory and vice versa.
1151+
1152+
**Example request**:
1153+
1154+
PUT /containers/8cce319429b2/archive?path=/vol1 HTTP/1.1
1155+
Content-Type: application/x-tar
1156+
1157+
{{ TAR STREAM }}
1158+
1159+
**Example response**:
1160+
1161+
HTTP/1.1 200 OK
1162+
1163+
Status Codes:
1164+
1165+
- **200** – the content was extracted successfully
1166+
- **400** - client error, bad parameter, details in JSON response body, one of:
1167+
- must specify path parameter (**path** cannot be empty)
1168+
- not a directory (**path** should be a directory but exists as a file)
1169+
- unable to overwrite existing directory with non-directory
1170+
(if **noOverwriteDirNonDir**)
1171+
- unable to overwrite existing non-directory with directory
1172+
(if **noOverwriteDirNonDir**)
1173+
- **403** - client error, permission denied, the volume
1174+
or container rootfs is marked as read-only.
1175+
- **404** - client error, resource not found, one of:
1176+
– no such container (container `id` does not exist)
1177+
- no such file or directory (**path** resource does not exist)
1178+
- **500** – server error
1179+
10641180
## 2.2 Images
10651181

10661182
### List Images

docs/reference/commandline/cp.md

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,81 @@ weight=1
1111

1212
# cp
1313

14-
Usage: docker cp CONTAINER:PATH HOSTDIR|-
14+
Copy files/folders between a container and the local filesystem.
1515

16-
Copy files/folders from the PATH to the HOSTDIR.
16+
Usage: docker cp [options] CONTAINER:PATH LOCALPATH|-
17+
docker cp [options] LOCALPATH|- CONTAINER:PATH
1718

18-
Copy files or folders from a container's filesystem to the directory on the
19-
host. Use '-' to write the data as a tar file to `STDOUT`. `CONTAINER:PATH` is
20-
relative to the root of the container's filesystem.
19+
--help Print usage statement
2120

21+
In the first synopsis form, the `docker cp` utility copies the contents of
22+
`PATH` from the filesystem of `CONTAINER` to the `LOCALPATH` (or stream as
23+
a tar archive to `STDOUT` if `-` is specified).
2224

25+
In the second synopsis form, the contents of `LOCALPATH` (or a tar archive
26+
streamed from `STDIN` if `-` is specified) are copied from the local machine to
27+
`PATH` in the filesystem of `CONTAINER`.
28+
29+
You can copy to or from either a running or stopped container. The `PATH` can
30+
be a file or directory. The `docker cp` command assumes all `CONTAINER:PATH`
31+
values are relative to the `/` (root) directory of the container. This means
32+
supplying the initial forward slash is optional; The command sees
33+
`compassionate_darwin:/tmp/foo/myfile.txt` and
34+
`compassionate_darwin:tmp/foo/myfile.txt` as identical. If a `LOCALPATH` value
35+
is not absolute, is it considered relative to the current working directory.
36+
37+
Behavior is similar to the common Unix utility `cp -a` in that directories are
38+
copied recursively with permissions preserved if possible. Ownership is set to
39+
the user and primary group on the receiving end of the transfer. For example,
40+
files copied to a container will be created with `UID:GID` of the root user.
41+
Files copied to the local machine will be created with the `UID:GID` of the
42+
user which invoked the `docker cp` command.
43+
44+
Assuming a path separator of `/`, a first argument of `SRC_PATH` and second
45+
argument of `DST_PATH`, the behavior is as follows:
46+
47+
- `SRC_PATH` specifies a file
48+
- `DST_PATH` does not exist
49+
- the file is saved to a file created at `DST_PATH`
50+
- `DST_PATH` does not exist and ends with `/`
51+
- Error condition: the destination directory must exist.
52+
- `DST_PATH` exists and is a file
53+
- the destination is overwritten with the contents of the source file
54+
- `DST_PATH` exists and is a directory
55+
- the file is copied into this directory using the basename from
56+
`SRC_PATH`
57+
- `SRC_PATH` specifies a directory
58+
- `DST_PATH` does not exist
59+
- `DST_PATH` is created as a directory and the *contents* of the source
60+
directory are copied into this directory
61+
- `DST_PATH` exists and is a file
62+
- Error condition: cannot copy a directory to a file
63+
- `DST_PATH` exists and is a directory
64+
- `SRC_PATH` does not end with `/.`
65+
- the source directory is copied into this directory
66+
- `SRC_PAPTH` does end with `/.`
67+
- the *content* of the source directory is copied into this
68+
directory
69+
70+
The command requires `SRC_PATH` and `DST_PATH` to exist according to the above
71+
rules. If `SRC_PATH` is local and is a symbolic link, the symbolic link, not
72+
the target, is copied.
73+
74+
A colon (`:`) is used as a delimiter between `CONTAINER` and `PATH`, but `:`
75+
could also be in a valid `LOCALPATH`, like `file:name.txt`. This ambiguity is
76+
resolved by requiring a `LOCALPATH` with a `:` to be made explicit with a
77+
relative or absolute path, for example:
78+
79+
`/path/to/file:name.txt` or `./file:name.txt`
80+
81+
It is not possible to copy certain system files such as resources under
82+
`/proc`, `/sys`, `/dev`, and mounts created by the user in the container.
83+
84+
Using `-` as the first argument in place of a `LOCALPATH` will stream the
85+
contents of `STDIN` as a tar archive which will be extracted to the `PATH` in
86+
the filesystem of the destination container. In this case, `PATH` must specify
87+
a directory.
88+
89+
Using `-` as the second argument in place of a `LOCALPATH` will stream the
90+
contents of the resource from the source container as a tar archive to
91+
`STDOUT`.

0 commit comments

Comments
 (0)