Docker 运行器

警告

此功能为实验性功能,可能会有破坏性更改。有关更多信息,请参阅Conan 稳定性章节。

如何使用 Docker 运行器

要在 Docker 容器内运行 Conan,您需要在主机 profile 中定义一个 [runner] 部分,使用以下字段

  • type (必需): 定义我们要使用的运行器类型,在本例中为 docker

  • dockerfile (可选, 默认为 None): Dockerfile 的绝对路径,如果您想构建 Docker 镜像。

  • image (可选, 默认为 conan-runner-default): 您想要从 Docker 镜像仓库下载的 Docker 镜像名称,或者如果您定义了 dockerfile 路径,则为构建的镜像名称。

  • cache (可选, 默认为 clean): Docker 容器如何(或不)使用主机的 Conan 缓存。

    • clean: 使用空缓存。

    • copy: 使用 conan cache save/restore 命令将主机缓存复制到容器内。

    • shared: 将主机的 Conan 缓存挂载为共享卷。

  • remove (可选, 默认为 false): truefalse。在运行 Conan 命令后移除容器。

  • configfile (可选, 默认为 None): 包含额外参数的配置文件的绝对路径 (有关更多信息,请参阅额外配置部分)。

注意

  • 您只能使用位于 conan profiles 文件夹内的 profile。

  • Shared 缓存可能会导致权限问题,具体取决于容器内外用户。我们建议使用 copy 缓存,即使它的设置速度可能稍慢。

  • runner profile 部分不会影响包 ID。

额外配置

如果您需要更多地控制容器的构建和执行,您可以在 configfile yaml 文件中定义更多参数。

image: image_name # The image to build or run.
build:
    dockerfile: /dockerfile/path # Dockerfile path.
    build_context: /build/context/path # Path within the build context to the Dockerfile.
    build_args: # A dictionary of build arguments
        foo: bar
    cacheFrom: # A list of images used for build cache resolution
        - image_1
run:
    name: container_name # The name for this container.
    containerEnv: # Environment variables to set inside the container.
        env_var_1: env_value
    containerUser: user_name # Username or UID to run commands as inside the container.
    privileged: False # Run as privileged
    capAdd: # Add kernel capabilities.
        - SYS_ADMIN
        - MKNOD
    securityOpt: # A list of string values to customize labels for MLS systems, such as SELinux.
        - opt_1
    mount: # A dictionary to configure volumes mounted inside the container.
        /home/user1/: # The host path or a volume name
            bind: /mnt/vol2 # The path to mount the volume inside the container
            mode: rw # rw to mount the volume read/write, or ro to mount it read-only.
    network: my-network # Specifies the network for the container.

如何在 runner 中运行 conan create

注意

docker runner 功能仅受 conan create 命令支持。conan install --build 命令不受支持。

在以下链接中,您可以找到一些关于如何使用 conan docker runner 的示例