NAME 名称

podman-cp - Copy files/folders between a container and the local filesystem
podman-cp - 在容器和本地文件系统之间复制文件/文件夹

SYNOPSIS 概要

podman cp [options] [container:]src_path [container:]dest_path
podman cp [选项] [容器:]src_path [容器:]dest_path

podman container cp [options] [container:]src_path [container:]dest_path
podman 容器 cp [选项] [容器:]src_path [容器:]dest_path

DESCRIPTION 描述

podman cp allows copying the contents of src_path to the dest_path. Files can be copied from a container to the local machine and vice versa or between two containers. If - is specified for either the SRC_PATH or DEST_PATH, one can also stream a tar archive from STDIN or to STDOUT.
podman cp 允许将 src_path 的内容复制到 dest_path。文件可以在容器和本地计算机之间相互复制,也可以在两个容器之间复制。如果为 - 指定了 SRC_PATHDEST_PATH 中的任何一个,还可以从 STDIN 流式传输 tar 存档文件或传输到 STDOUT

The containers can be either running or stopped and the src_path or dest_path can be a file or directory.
容器可以是运行中的或已停止的,src_path 或 dest_path 可以是文件或目录。

IMPORTANT: The podman cp command assumes container paths are relative to the container’s root directory (/), which means supplying the initial forward slash is optional and therefore sees compassionate_darwin:/tmp/foo/myfile.txt and compassionate_darwin:tmp/foo/myfile.txt as identical.
重要提示:podman cp 命令假定容器路径是相对于容器的根目录( / )的,这意味着提供初始斜杠是可选的,因此 compassionate_darwin:/tmp/foo/myfile.txtcompassionate_darwin:tmp/foo/myfile.txt 视为相同。

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 podman cp is run.
本地机器路径可以是绝对值,也可以是相对值。该命令将本地机器的相对路径解释为相对于运行 podman cp 的当前工作目录。

Assuming a path separator of /, a first argument of src_path and second argument of dest_path, the behavior is as follows:
假设路径分隔符为 / ,第一个参数为 src_path,第二个参数为 dest_path,则行为如下:

src_path specifies a file:
src_path 指定一个文件:

  • dest_path does not exist 目标路径不存在

    • the file is saved to a file created at dest_path (note that parent directory must exist).
      文件保存在 dest_path 创建的文件中(请注意父目录必须存在)。

  • dest_path exists and is a file
    dest_path 存在且为文件

    • the destination is overwritten with the source file’s contents.
      目的地被源文件的内容覆盖。

  • dest_path exists and is a directory
    dest_path 存在且是一个目录。

    • the file is copied into this directory using the base name from src_path.
      文件被复制到该目录中,使用来自 src_path 的基本名称。

src_path specifies a directory:
src_path 指定一个目录:

  • dest_path does not exist dest_path 不存在

    • dest_path is created as a directory and the contents of the source directory are copied into this directory.
      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 ends with / src_path 以 / 结尾

      • the source directory is copied into this directory.
        源目录被复制到此目录中。

    • src_path ends with /. (i.e., slash followed by dot)
      src_path 以 /. 结尾(即,斜杠后跟点)。

      • the content of the source directory is copied into this directory.
        源目录的内容被复制到此目录中。

The command requires src_path and dest_path to exist according to the above rules.
根据上述规则,该命令需要存在 src_path 和 dest_path。

If src_path is local and is a symbolic link, the symbolic target, is copied by default.
如果 src_path 是本地的符号链接,默认情况下会复制符号目标。

A colon ( : ) is used as a delimiter between a container and its path, it can also be used when specifying paths to a src_path or dest_path on a local machine, for example, file:name.txt.
冒号(:)用作容器和其路径之间的分隔符,也可用于指定本地机器上的 src_path 或 dest_path 的路径,例如, file:name.txt

IMPORTANT: while using a colon ( : ) in a local machine path, one must be explicit with a relative or absolute path, for example: /path/to/file:name.txt or ./file:name.txt
重要提示:在本地机器路径中使用冒号(:)时,必须明确指定相对路径或绝对路径,例如: /path/to/file:name.txt./file:name.txt

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 the container. In this case, dest_path must specify a directory. Using - as the dest_path streams the contents of the resource (can be a directory) as a tar archive to STDOUT.
使用 - 作为 src_path 将 STDIN 的内容流式传输为 tar 存档。该命令将 tar 的内容提取到容器中的 DEST_PATH 中。在这种情况下,dest_path 必须指定一个目录。使用 - 作为 dest_path 将资源的内容(可以是目录)流式传输为 tar 存档到 STDOUT

Note that podman cp ignores permission errors when copying from a running rootless container. The TTY devices inside a rootless container are owned by the host’s root user and hence cannot be read inside the container’s user namespace.
注意, podman cp 在从运行中的无根容器复制时会忽略权限错误。无根容器内部的 TTY 设备由主机的 root 用户拥有,因此无法在容器的用户命名空间内读取。

Further note that podman cp does not support globbing (e.g., cp dir/*.txt). To copy multiple files from the host to the container use xargs(1) or find(1) (or similar tools for chaining commands) in conjunction with podman cp. To copy multiple files from the container to the host, use podman mount CONTAINER and operate on the returned mount point instead (see ALTERNATIVES below).
还要注意, podman cp 不支持通配符(例如, cp dir/*.txt )。要从主机复制多个文件到容器,请使用 xargs(1) 或 find(1)(或类似的链接命令的工具),并与 podman cp 结合使用。要从容器复制多个文件到主机,请使用 podman mount CONTAINER 并在返回的挂载点上操作(请参阅下面的 ALTERNATIVES)。

OPTIONS 选项

--archive, -a --archive, -a

Archive mode (copy all UID/GID information). When set to true, files copied to a container have changed ownership to the primary UID/GID of the container. When set to false, maintain UID/GID from archive sources instead of changing them to the primary UID/GID of the destination container. The default is true.
存档模式(复制所有 UID/GID 信息)。当设置为 true 时,复制到容器中的文件的所有权会更改为容器的主要 UID/GID。当设置为 false 时,保留来自存档源的 UID/GID,而不是将它们更改为目标容器的主要 UID/GID。默认值为 true。

--overwrite --覆盖 ¶

Allow directories to be overwritten with non-directories and vice versa. By default, podman cp errors out when attempting to overwrite, for instance, a regular file with a directory.
允许目录被非目录文件覆盖,反之亦然。默认情况下, podman cp 在尝试用目录覆盖常规文件时会报错。

ALTERNATIVES 替代方案 ¶

Podman has much stronger capabilities than just podman cp to achieve copying files between the host and containers.
Podman 具有比仅使用 podman cp 更强大的功能,可以在主机和容器之间复制文件。

Using standard podman-mount(1) and podman-unmount(1) takes advantage of the entire linux tool chain, rather than just cp.
使用标准的 podman-mount(1)和 podman-unmount(1)可以充分利用整个 Linux 工具链,而不仅仅是 cp。

copying contents out of a container or into a container, can be achieved with a few simple commands. For example:
将内容从容器复制出来或复制到容器中,可以通过几个简单的命令实现。例如:

To copy the /etc/foobar directory out of a container and onto /tmp on the host, the following commands can be executed:
要将 /etc/foobar 目录从容器复制到主机上的 /tmp ,可以执行以下命令:

mnt=$(podman mount CONTAINERID)
cp -R ${mnt}/etc/foobar /tmp
podman umount CONTAINERID

To untar a tar ball into a container, following commands can be executed:
要将一个 tar 包解压到容器中,可以执行以下命令:

mnt=$(podman mount CONTAINERID)
tar xf content.tgz -C ${mnt}
podman umount CONTAINERID

To install a package into a container that does not have dnf installed, following commands can be executed:
将软件包安装到没有安装 dnf 的容器中,可以执行以下命令:

mnt=$(podman mount CONTAINERID)
dnf install --installroot=${mnt} httpd
chroot ${mnt} rm -rf /var/log/dnf /var/cache/dnf
podman umount CONTAINERID

By using podman mount and podman unmount, one can use all of the standard linux tools for moving files into and out of containers, not just the cp command.
通过 podman mountpodman unmount ,可以使用所有标准的 Linux 工具来将文件移入和移出容器,而不仅仅是 cp 命令。

EXAMPLES 示例

Copy a file from the host to a container:
将文件从主机复制到容器中:

podman cp /myapp/app.conf containerID:/myapp/app.conf

Copy a file from a container to a directory on another container:
将文件从一个容器复制到另一个容器上的目录:

podman cp containerID1:/myfile.txt containerID2:/tmp

Copy a directory on a container to a directory on the host:
将容器中的目录复制到主机上的目录:

podman cp containerID:/myapp/ /myapp/

Copy the contents of a directory on a container to a directory on the host:
将容器中的目录的内容复制到主机上的目录:

podman cp containerID:/home/myuser/. /home/myuser/

Copy a directory on a container into a directory on another:
将容器中的一个目录复制到另一个目录中:

podman cp containerA:/myapp containerB:/newapp

Stream a tar archive from STDIN to a container:
STDIN 流式传输一个 tar 存档到容器:

podman cp - containerID:/myfiles.tar.gz < myfiles.tar.gz

SEE ALSO 参见 ¶

podman(1), podman-mount(1), podman-unmount(1)