Browse code

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 <josh.hawn@docker.com> (github: jlhawn)

Josh Hawn authored on 2015/05/16 03:35:41
Showing 4 changed files
... ...
@@ -68,6 +68,23 @@ Running `docker rmi` emits an **untag** event when removing an image name.  The
68 68
 
69 69
 ### What's new
70 70
 
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
+
71 88
 **New!**
72 89
 The `hostConfig` option now accepts the field `GroupAdd`, which specifies a list of additional
73 90
 groups that the container process will run as.
... ...
@@ -1039,6 +1039,8 @@ Status Codes:
1039 1039
 
1040 1040
 Copy files or folders of container `id`
1041 1041
 
1042
+**Deprecated** in favor of the `archive` endpoint below.
1043
+
1042 1044
 **Example request**:
1043 1045
 
1044 1046
     POST /containers/4fa6e0f0c678/copy HTTP/1.1
... ...
@@ -1061,6 +1063,120 @@ Status Codes:
1061 1061
 -   **404** – no such container
1062 1062
 -   **500** – server error
1063 1063
 
1064
+### Retrieving information about files and folders in a container
1065
+
1066
+`HEAD /containers/(id)/archive`
1067
+
1068
+See the description of the `X-Docker-Container-Path-Stat` header in the
1069
+folowing section.
1070
+
1071
+### Get an archive of a filesystem resource in a container
1072
+
1073
+`GET /containers/(id)/archive`
1074
+
1075
+Get an tar archive of a resource in the filesystem of container `id`.
1076
+
1077
+Query Parameters:
1078
+
1079
+- **path** - resource in the container's filesystem to archive. Required.
1080
+
1081
+    If not an absolute path, it is relative to the container's root directory.
1082
+    The resource specified by **path** must exist. To assert that the resource
1083
+    is expected to be a directory, **path** should end in `/` or  `/.`
1084
+    (assuming a path separator of `/`). If **path** ends in `/.` then this
1085
+    indicates that only the contents of the **path** directory should be
1086
+    copied. A symlink is always resolved to its target.
1087
+
1088
+    **Note**: It is not possible to copy certain system files such as resources
1089
+    under `/proc`, `/sys`, `/dev`, and mounts created by the user in the
1090
+    container.
1091
+
1092
+**Example request**:
1093
+
1094
+        GET /containers/8cce319429b2/archive?path=/root HTTP/1.1
1095
+
1096
+**Example response**:
1097
+
1098
+        HTTP/1.1 200 OK
1099
+        Content-Type: application/x-tar
1100
+        X-Docker-Container-Path-Stat: eyJuYW1lIjoicm9vdCIsInBhdGgiOiIvcm9vdCIsInNpemUiOjQwOTYsIm1vZGUiOjIxNDc0ODQwOTYsIm10aW1lIjoiMjAxNC0wMi0yN1QyMDo1MToyM1oifQ==
1101
+
1102
+        {{ TAR STREAM }}
1103
+
1104
+On success, a response header `X-Docker-Container-Path-Stat` will be set to a
1105
+base64-encoded JSON object containing some filesystem header information about
1106
+the archived resource. The above example value would decode to the following
1107
+JSON object (whitespace added for readability):
1108
+
1109
+        {
1110
+            "name": "root",
1111
+            "path": "/root",
1112
+            "size": 4096,
1113
+            "mode": 2147484096,
1114
+            "mtime": "2014-02-27T20:51:23Z"
1115
+        }
1116
+
1117
+A `HEAD` request can also be made to this endpoint if only this information is
1118
+desired.
1119
+
1120
+Status Codes:
1121
+
1122
+- **200** - success, returns archive of copied resource
1123
+- **400** - client error, bad parameter, details in JSON response body, one of:
1124
+    - must specify path parameter (**path** cannot be empty)
1125
+    - not a directory (**path** was asserted to be a directory but exists as a
1126
+      file)
1127
+- **404** - client error, resource not found, one of:
1128
+    – no such container (container `id` does not exist)
1129
+    - no such file or directory (**path** does not exist)
1130
+- **500** - server error
1131
+
1132
+### Extract an archive of files or folders to a directory in a container
1133
+
1134
+`PUT /containers/(id)/archive`
1135
+
1136
+Upload a tar archive to be extracted to a path in the filesystem of container
1137
+`id`.
1138
+
1139
+Query Parameters:
1140
+
1141
+- **path** - path to a directory in the container
1142
+    to extract the archive's contents into. Required.
1143
+
1144
+    If not an absolute path, it is relative to the container's root directory.
1145
+    The **path** resource must exist.
1146
+- **noOverwriteDirNonDir** - If "1", "true", or "True" then it will be an error
1147
+    if unpacking the given content would cause an existing directory to be
1148
+    replaced with a non-directory and vice versa.
1149
+
1150
+**Example request**:
1151
+
1152
+        PUT /containers/8cce319429b2/archive?path=/vol1 HTTP/1.1
1153
+        Content-Type: application/x-tar
1154
+
1155
+        {{ TAR STREAM }}
1156
+
1157
+**Example response**:
1158
+
1159
+        HTTP/1.1 200 OK
1160
+
1161
+Status Codes:
1162
+
1163
+- **200** – the content was extracted successfully
1164
+- **400** - client error, bad parameter, details in JSON response body, one of:
1165
+    - must specify path parameter (**path** cannot be empty)
1166
+    - not a directory (**path** should be a directory but exists as a file)
1167
+    - unable to overwrite existing directory with non-directory
1168
+      (if **noOverwriteDirNonDir**)
1169
+    - unable to overwrite existing non-directory with directory
1170
+      (if **noOverwriteDirNonDir**)
1171
+- **403** - client error, permission denied, the volume
1172
+    or container rootfs is marked as read-only.
1173
+- **404** - client error, resource not found, one of:
1174
+    – no such container (container `id` does not exist)
1175
+    - no such file or directory (**path** resource does not exist)
1176
+- **500** – server error
1177
+
1064 1178
 ## 2.2 Images
1065 1179
 
1066 1180
 ### List Images
... ...
@@ -11,12 +11,81 @@ weight=1
11 11
 
12 12
 # cp
13 13
 
14
-    Usage: docker cp CONTAINER:PATH HOSTDIR|-
14
+Copy files/folders between a container and the local filesystem.
15 15
 
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
17 18
 
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
21 20
 
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).
22 24
 
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`.
... ...
@@ -2,69 +2,150 @@
2 2
 % Docker Community
3 3
 % JUNE 2014
4 4
 # NAME
5
-docker-cp - Copy files or folders from a container's PATH to a HOSTDIR
6
-or to STDOUT.
5
+docker-cp - Copy files/folders between a container and the local filesystem.
7 6
 
8 7
 # SYNOPSIS
9 8
 **docker cp**
10 9
 [**--help**]
11
-CONTAINER:PATH HOSTDIR|-
10
+CONTAINER:PATH LOCALPATH|-
11
+LOCALPATH|- CONTAINER:PATH
12 12
 
13 13
 # DESCRIPTION
14 14
 
15
-Copy files or folders from a `CONTAINER:PATH` to the `HOSTDIR` or to `STDOUT`. 
16
-The `CONTAINER:PATH` is relative to the root of the container's filesystem. You
17
-can copy from either a running or stopped container. 
15
+In the first synopsis form, the `docker cp` utility copies the contents of
16
+`PATH` from the filesystem of `CONTAINER` to the `LOCALPATH` (or stream as
17
+a tar archive to `STDOUT` if `-` is specified).
18 18
 
19
-The `PATH` can be a file or directory. The `docker cp` command assumes all
20
-`PATH` values start at the `/` (root) directory. This means supplying the
21
-initial forward slash is optional; The command sees
19
+In the second synopsis form, the contents of `LOCALPATH` (or a tar archive
20
+streamed from `STDIN` if `-` is specified) are copied from the local machine to
21
+`PATH` in the filesystem of `CONTAINER`.
22
+
23
+You can copy to or from either a running or stopped container. The `PATH` can
24
+be a file or directory. The `docker cp` command assumes all `CONTAINER:PATH`
25
+values are relative to the `/` (root) directory of the container. This means
26
+supplying the initial forward slash is optional; The command sees
22 27
 `compassionate_darwin:/tmp/foo/myfile.txt` and
23
-`compassionate_darwin:tmp/foo/myfile.txt` as identical.
28
+`compassionate_darwin:tmp/foo/myfile.txt` as identical. If a `LOCALPATH` value
29
+is not absolute, is it considered relative to the current working directory.
30
+
31
+Behavior is similar to the common Unix utility `cp -a` in that directories are
32
+copied recursively with permissions preserved if possible. Ownership is set to
33
+the user and primary group on the receiving end of the transfer. For example,
34
+files copied to a container will be created with `UID:GID` of the root user.
35
+Files copied to the local machine will be created with the `UID:GID` of the
36
+user which invoked the `docker cp` command.
37
+
38
+Assuming a path separator of `/`, a first argument of `SRC_PATH` and second
39
+argument of `DST_PATH`, the behavior is as follows:
40
+
41
+- `SRC_PATH` specifies a file
42
+    - `DST_PATH` does not exist
43
+        - the file is saved to a file created at `DST_PATH`
44
+    - `DST_PATH` does not exist and ends with `/`
45
+        - Error condition: the destination directory must exist.
46
+    - `DST_PATH` exists and is a file
47
+        - the destination is overwritten with the contents of the source file
48
+    - `DST_PATH` exists and is a directory
49
+        - the file is copied into this directory using the basename from
50
+          `SRC_PATH`
51
+- `SRC_PATH` specifies a directory
52
+    - `DST_PATH` does not exist
53
+        - `DST_PATH` is created as a directory and the *contents* of the source
54
+           directory are copied into this directory
55
+    - `DST_PATH` exists and is a file
56
+        - Error condition: cannot copy a directory to a file
57
+    - `DST_PATH` exists and is a directory
58
+        - `SRC_PATH` does not end with `/.`
59
+            - the source directory is copied into this directory
60
+        - `SRC_PAPTH` does end with `/.`
61
+            - the *content* of the source directory is copied into this
62
+              directory
63
+
64
+The command requires `SRC_PATH` and `DST_PATH` to exist according to the above
65
+rules. If `SRC_PATH` is local and is a symbolic link, the symbolic link, not
66
+the target, is copied.
67
+
68
+A colon (`:`) is used as a delimiter between `CONTAINER` and `PATH`, but `:`
69
+could also be in a valid `LOCALPATH`, like `file:name.txt`. This ambiguity is
70
+resolved by requiring a `LOCALPATH` with a `:` to be made explicit with a
71
+relative or absolute path, for example:
72
+
73
+    `/path/to/file:name.txt` or `./file:name.txt`
74
+
75
+It is not possible to copy certain system files such as resources under
76
+`/proc`, `/sys`, `/dev`, and mounts created by the user in the container.
77
+
78
+Using `-` as the first argument in place of a `LOCALPATH` will stream the
79
+contents of `STDIN` as a tar archive which will be extracted to the `PATH` in
80
+the filesystem of the destination container. In this case, `PATH` must specify
81
+a directory.
82
+
83
+Using `-` as the second argument in place of a `LOCALPATH` will stream the
84
+contents of the resource from the source container as a tar archive to
85
+`STDOUT`.
86
+
87
+# OPTIONS
88
+**--help**
89
+  Print usage statement
90
+
91
+# EXAMPLES
92
+
93
+Suppose a container has finished producing some output as a file it saves
94
+to somewhere in its filesystem. This could be the output of a build job or
95
+some other computation. You can copy these outputs from the container to a
96
+location on your local host.
24 97
 
25
-The `HOSTDIR` refers to a directory on the host. If you do not specify an
26
-absolute path for your `HOSTDIR` value, Docker creates the directory relative to
27
-where you run the `docker cp` command. For example, suppose you want to copy the
28
-`/tmp/foo` directory from a container to the `/tmp` directory on your host. If
29
-you run `docker cp` in your `~` (home) directory on the host:
98
+If you want to copy the `/tmp/foo` directory from a container to the
99
+existing `/tmp` directory on your host. If you run `docker cp` in your `~`
100
+(home) directory on the local host:
30 101
 
31 102
 		$ docker cp compassionate_darwin:tmp/foo /tmp
32 103
 
33 104
 Docker creates a `/tmp/foo` directory on your host. Alternatively, you can omit
34
-the leading slash in the command. If you execute this command from your home directory:
105
+the leading slash in the command. If you execute this command from your home
106
+directory:
35 107
 
36 108
 		$ docker cp compassionate_darwin:tmp/foo tmp
37 109
 
38
-Docker creates a `~/tmp/foo` subdirectory.  
110
+If `~/tmp` does not exist, Docker will create it and copy the contents of
111
+`/tmp/foo` from the container into this new directory. If `~/tmp` already
112
+exists as a directory, then Docker will copy the contents of `/tmp/foo` from
113
+the container into a directory at `~/tmp/foo`.
39 114
 
40
-When copying files to an existing `HOSTDIR`, the `cp` command adds the new files to
41
-the directory. For example, this command:
115
+When copying a single file to an existing `LOCALPATH`, the `docker cp` command
116
+will either overwrite the contents of `LOCALPATH` if it is a file or place it
117
+into `LOCALPATH` if it is a directory, overwriting an existing file of the same
118
+name if one exists. For example, this command:
42 119
 
43
-		$ docker cp sharp_ptolemy:/tmp/foo/myfile.txt /tmp
120
+		$ docker cp sharp_ptolemy:/tmp/foo/myfile.txt /test
44 121
 
45
-Creates a `/tmp/foo` directory on the host containing the `myfile.txt` file. If
46
-you repeat the command but change the filename:
122
+If `/test` does not exist on the local machine, it will be created as a file
123
+with the contents of `/tmp/foo/myfile.txt` from the container. If `/test`
124
+exists as a file, it will be overwritten. Lastly, if `/tmp` exists as a
125
+directory, the file will be copied to `/test/myfile.txt`.
47 126
 
48
-		$ docker cp sharp_ptolemy:/tmp/foo/secondfile.txt /tmp
127
+Next, suppose you want to copy a file or folder into a container. For example,
128
+this could be a configuration file or some other input to a long running
129
+computation that you would like to place into a created container before it
130
+starts. This is useful because it does not require the configuration file or
131
+other input to exist in the container image.
49 132
 
50
-Your host's `/tmp/foo` directory will contain both files:
133
+If you have a file, `config.yml`, in the current directory on your local host
134
+and wish to copy it to an existing directory at `/etc/my-app.d` in a container,
135
+this command can be used:
51 136
 
52
-		$ ls /tmp/foo
53
-		myfile.txt secondfile.txt
54
-		
55
-Finally, use '-' to write the data as a `tar` file to STDOUT.
137
+		$ docker cp config.yml myappcontainer:/etc/my-app.d
56 138
 
57
-# OPTIONS
58
-**--help**
59
-  Print usage statement
139
+If you have several files in a local directory `/config` which you need to copy
140
+to a directory `/etc/my-app.d` in a container:
60 141
 
61
-# EXAMPLES
62
-An important shell script file, created in a bash shell, is copied from
63
-the exited container to the current dir on the host:
142
+		$ docker cp /config/. myappcontainer:/etc/my-app.d
64 143
 
65
-    # docker cp c071f3c3ee81:setup.sh .
144
+The above command will copy the contents of the local `/config` directory into
145
+the directory `/etc/my-app.d` in the container.
66 146
 
67 147
 # HISTORY
68 148
 April 2014, Originally compiled by William Henry (whenry at redhat dot com)
69 149
 based on docker.com source material and internal work.
70 150
 June 2014, updated by Sven Dowideit <SvenDowideit@home.org.au>
151
+May 2015, updated by Josh Hawn <josh.hawn@docker.com>