?
This document uses PHP Chinese website manual Release
復(fù)制容器和本地文件系統(tǒng)之間的文件/文件夾
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Name, shorthand | Default | Description |
---|---|---|
--archive, -a | false | Archive mode (copy all uid/gid information) |
--follow-link, -L | false | Always follow symbol link in SRC_PATH |
命令 | 描述 |
---|---|
docker | Docker CLI的基本命令。 |
docker cp
實用程序?qū)?nèi)容復(fù)制SRC_PATH
到DEST_PATH
。您可以從容器的文件系統(tǒng)復(fù)制到本地機器或從本地文件系統(tǒng)復(fù)制到容器。如果-
為SRC_PATH
or 指定DEST_PATH
,您也可以從一個STDIN
或多個流向tar存檔STDOUT
。該CONTAINER
可以是運行或停止容器。該SRC_PATH
或DEST_PATH
可以是文件或目錄。
該docker cp
命令假定容器路徑與容器的/
(根)目錄相關(guān)。這意味著提供初始正斜杠是可選的; 該命令看起來compassionate_darwin:/tmp/foo/myfile.txt
和compassionate_darwin:tmp/foo/myfile.txt
完全相同。本地機器路徑可以是絕對值或相對值。該命令將本地機器的相對路徑解釋為相對于當(dāng)前運行的工作目錄docker cp
。
該cp
命令的行為與 Unix cp -a
命令類似,即在可能的情況下保留權(quán)限時遞歸復(fù)制目錄。所有權(quán)設(shè)置為目標(biāo)用戶和主要組。例如,復(fù)制到容器的文件是由UID:GID
root用戶創(chuàng)建的。復(fù)制到本地機器的文件是由UID:GID
調(diào)用該docker cp
命令的用戶創(chuàng)建的。但是,如果指定了該-a
選項,docker cp
則將所有權(quán)設(shè)置為源用戶和主組。如果您指定了該-L
選項,請docker cp
遵循中的任何符號鏈接SRC_PATH
。docker cp
并沒有創(chuàng)建父目錄DEST_PATH
,如果它們不存在。
假設(shè)一個路徑分隔符為/
,第一個參數(shù)SRC_PATH
和第二個參數(shù)DEST_PATH
的行為如下所示:
SRC_PATH
指定一個文件
該文件被保存到創(chuàng)建的文件中 DEST_PATH
DEST_PATH
不存在
- `DEST_PATH` does not exist and ends with `/` - Error condition: the destination directory must exist.
- `DEST_PATH` exists and is a file - the destination is overwritten with the source file’s contents
- `DEST_PATH` exists and is a directory - the file is copied into this directory using the basename from `SRC_PATH`
SRC_PATH
指定一個目錄
DEST_PATH
被創(chuàng)建為一個目錄,源目錄的內(nèi)容被復(fù)制到這個目錄中
DEST_PATH
不存在
- `DEST_PATH` exists and is a file - Error condition: cannot copy a directory to a file
- `DEST_PATH` exists and is a directory - `SRC_PATH` does not end with `/.` (that is: _slash_ followed by _dot_) - the source directory is copied into this directory
- `SRC_PATH` does end with `/.` (that is: _slash_ followed by _dot_) - the _content_ of the source directory is copied into this directory
命令需要SRC_PATH
和DEST_PATH
按照上述規(guī)則存在。如果SRC_PATH
是本地的并且是符號鏈接,則默認(rèn)情況下復(fù)制符號鏈接而不是目標(biāo)。要復(fù)制鏈接目標(biāo)而不是鏈接,請指定該-L
選項。
冒號(:
)用作CONTAINER
它和路徑之間的分隔符。例如,您也可以:
在指定路徑到本地機器SRC_PATH
或DEST_PATH
本地機器時使用file:name.txt
。如果在本地機器路徑中使用:
,則必須使用相對路徑或絕對路徑進行顯式指定,例如:
`/path/to/file:name.txt` or `./file:name.txt`
這是不可能復(fù)制某些系統(tǒng)文件,如在資源/proc
,/sys
,/dev
在容器中的用戶創(chuàng)建,tmpfs 的,和坐騎。但是,您仍然可以通過手動運行tar
來復(fù)制這些文件docker exec
。以下兩個示例以不同的方式執(zhí)行相同的操作(考慮SRC_PATH
并且DEST_PATH
是目錄):
$ docker exec foo tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | tar Cxf DEST_PATH -
$ tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | docker exec -i foo tar Cxf DEST_PATH -
使用-
作為SRC_PATH
流的內(nèi)容STDIN
作為一個 tar 歸檔。該命令將tar的內(nèi)容提取到DEST_PATH
容器的文件系統(tǒng)中。在這種情況下,DEST_PATH
必須指定一個目錄。使用-
作為DEST_PATH
流的資源作為一個tar歸檔的內(nèi)容STDOUT
。