1DOCKER(1) JUNE 2014 DOCKER(1)
2
3
4
6 docker-cp - Copy files/folders between a container and the local
7 filesystem.
8
9
10
12 docker cp [--help] CONTAINER:SRC_PATH DEST_PATH|-
13
14
15 docker cp [--help] SRC_PATH|- CONTAINER:DEST_PATH
16
17
18
20 The docker cp utility copies the contents of SRC_PATH to the DEST_PATH.
21 You can copy from the container's file system to the local machine or
22 the reverse, from the local filesystem to the container. If - is
23 specified for either the SRC_PATH or DEST_PATH, you can also stream a
24 tar archive from STDIN or to STDOUT. The CONTAINER can be a running or
25 stopped container. The SRC_PATH or DEST_PATH can be a file or
26 directory.
27
28
29 The docker cp command assumes container paths are relative to the
30 container's / (root) directory. This means supplying the initial
31 forward slash is optional; The command sees
32 compassionate_darwin:/tmp/foo/myfile.txt and
33 compassionate_darwin:tmp/foo/myfile.txt as identical. Local machine
34 paths can be an absolute or relative value. The command interprets a
35 local machine's relative paths as relative to the current working
36 directory where docker cp is run.
37
38
39 The cp command behaves like the Unix cp -a command in that directories
40 are copied recursively with permissions preserved if possible.
41 Ownership is set to the user and primary group at the destination. For
42 example, files copied to a container are created with UID:GID of the
43 root user. Files copied to the local machine are created with the
44 UID:GID of the user which invoked the docker cp command. If you
45 specify the -L option, docker cp follows any symbolic link in the
46 SRC_PATH. docker cp does not create parent directories for DEST_PATH if
47 they do not exist.
48
49
50 Assuming a path separator of /, a first argument of SRC_PATH and second
51 argument of DEST_PATH, the behavior is as follows:
52
53
54 · SRC_PATH specifies a file
55
56
57 · DEST_PATH does not exist
58
59
60 · the file is saved to a file created at DEST_PATH
61
62
63 · DEST_PATH does not exist and ends with /
64
65
66 · Error condition: the destination directory must exist.
67
68
69 · DEST_PATH exists and is a file
70
71
72 · the destination is overwritten with the source file's
73 contents
74
75
76 · DEST_PATH exists and is a directory
77
78
79 · the file is copied into this directory using the basename
80 from SRC_PATH
81
82
83
84 · SRC_PATH specifies a directory
85
86
87 · DEST_PATH does not exist
88
89
90 · DEST_PATH is created as a directory and the contents of
91 the source directory are copied into this directory
92
93
94 · DEST_PATH exists and is a file
95
96
97 · Error condition: cannot copy a directory to a file
98
99
100 · DEST_PATH exists and is a directory
101
102
103 · SRC_PATH does not end with /.
104
105
106 · the source directory is copied into this directory
107
108
109 · SRC_PATH does end with /.
110
111
112 · the content of the source directory is copied into this
113 directory
114
115
116
117
118
119
120 The command requires SRC_PATH and DEST_PATH to exist according to the
121 above rules. If SRC_PATH is local and is a symbolic link, the symbolic
122 link, not the target, is copied by default. To copy the link target and
123 not the link, specify the -L option.
124
125
126 A colon (:) is used as a delimiter between CONTAINER and its path. You
127 can also use : when specifying paths to a SRC_PATH or DEST_PATH on a
128 local machine, for example file:name.txt. If you use a : in a local
129 machine path, you must be explicit with a relative or absolute path,
130 for example:
131
132
133 `/path/to/file:name.txt` or `./file:name.txt`
134
135
136
137 It is not possible to copy certain system files such as resources under
138 /proc, /sys, /dev, tmpfs, and mounts created by the user in the
139 container. However, you can still copy such files by manually running
140 tar in docker exec. For example (consider SRC_PATH and DEST_PATH are
141 directories):
142
143
144 $ docker exec foo tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | tar Cxf DEST_PATH -
145
146
147
148 or
149
150
151 $ tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | docker exec -i foo tar Cxf DEST_PATH -
152
153
154
155 Using - as the SRC_PATH streams the contents of STDIN as a tar archive.
156 The command extracts the content of the tar to the DEST_PATH in
157 container's filesystem. In this case, DEST_PATH must specify a
158 directory. Using - as the DEST_PATH streams the contents of the
159 resource as a tar archive to STDOUT.
160
161
162
164 -L, --follow-link=true|false
165 Follow symbol link in SRC_PATH
166
167
168 --help
169 Print usage statement
170
171
172
174 Suppose a container has finished producing some output as a file it
175 saves to somewhere in its filesystem. This could be the output of a
176 build job or some other computation. You can copy these outputs from
177 the container to a location on your local host.
178
179
180 If you want to copy the /tmp/foo directory from a container to the
181 existing /tmp directory on your host. If you run docker cp in your
182 (home) directory on the local host:
183
184
185 $ docker cp compassionate_darwin:tmp/foo /tmp
186
187
188
189 Docker creates a /tmp/foo directory on your host. Alternatively, you
190 can omit the leading slash in the command. If you execute this command
191 from your home directory:
192
193
194 $ docker cp compassionate_darwin:tmp/foo tmp
195
196
197
198 If /tmp does not exist, Docker will create it and copy the contents of
199 /tmp/foo from the container into this new directory. If /tmp already
200 exists as a directory, then Docker will copy the contents of /tmp/foo
201 from the container into a directory at /tmp/foo.
202
203
204 When copying a single file to an existing LOCALPATH, the docker cp
205 command will either overwrite the contents of LOCALPATH if it is a file
206 or place it into LOCALPATH if it is a directory, overwriting an
207 existing file of the same name if one exists. For example, this
208 command:
209
210
211 $ docker cp sharp_ptolemy:/tmp/foo/myfile.txt /test
212
213
214
215 If /test does not exist on the local machine, it will be created as a
216 file with the contents of /tmp/foo/myfile.txt from the container. If
217 /test exists as a file, it will be overwritten. Lastly, if /test exists
218 as a directory, the file will be copied to /test/myfile.txt.
219
220
221 Next, suppose you want to copy a file or folder into a container. For
222 example, this could be a configuration file or some other input to a
223 long running computation that you would like to place into a created
224 container before it starts. This is useful because it does not require
225 the configuration file or other input to exist in the container image.
226
227
228 If you have a file, config.yml, in the current directory on your local
229 host and wish to copy it to an existing directory at /etc/my-app.d in a
230 container, this command can be used:
231
232
233 $ docker cp config.yml myappcontainer:/etc/my-app.d
234
235
236
237 If you have several files in a local directory /config which you need
238 to copy to a directory /etc/my-app.d in a container:
239
240
241 $ docker cp /config/. myappcontainer:/etc/my-app.d
242
243
244
245 The above command will copy the contents of the local /config directory
246 into the directory /etc/my-app.d in the container.
247
248
249 Finally, if you want to copy a symbolic link into a container, you
250 typically want to copy the linked target and not the link itself. To
251 copy the target, use the -L option, for example:
252
253
254 $ ln -s /tmp/somefile /tmp/somefile.ln
255 $ docker cp -L /tmp/somefile.ln myappcontainer:/tmp/
256
257
258
259 This command copies content of the local /tmp/somefile into the file
260 /tmp/somefile.ln in the container. Without -L option, the
261 /tmp/somefile.ln preserves its symbolic link but not its content.
262
263
264
266 April 2014, Originally compiled by William Henry (whenry at redhat dot
267 com) based on docker.com source material and internal work. June 2014,
268 updated by Sven Dowideit ⟨SvenDowideit@home.org.au⟩ May 2015, updated
269 by Josh Hawn ⟨josh.hawn@docker.com⟩
270
271
272
273Docker Community Docker User Manuals DOCKER(1)