使用 Docker runner 配置文件参数化 Dockerfile

警告

此功能处于实验阶段,可能会发生重大更改。有关更多信息,请参阅Conan 稳定性部分。

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

例如,您可以在构建步骤中添加参数或在启动容器时添加环境变量。

要使用它,您只需将其添加到主机配置文件中。

[settings]
...
[runner]
type=docker
configfile=</my/runner/folder>/configfile
cache=copy
remove=false

使用方法

让我们在项目文件夹中创建一个 Dockerfile,一个 cmake_lib myparamlib,就像“使用 Docker runner 创建 Conan 包”示例一样,以及两个配置文件。

$ cd </my/runner/folder>
$ mkdir myparamlib
$ cd myparamlib
$ conan new cmake_lib -d name=myparamlib -d version=0.1
$ cd </my/runner/folder>
$ tree
.
├── CMakeLists.txt
├── conanfile.py
├── include
│   └── myparamlib.h
├── src
│   └── myparamlib.cpp
└── test_package
    ├── CMakeLists.txt
    ├── conanfile.py
    └── src
        └── example.cpp
ARG BASE_IMAGE
FROM $BASE_IMAGE
RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        build-essential \
        cmake \
        python3 \
        python3-pip \
        python3-venv \
    && rm -rf /var/lib/apt/lists/*
RUN pip install conan

配置文件

image: my-conan-runner-image
build:
    dockerfile: </my/runner/folder>
    build_context: </my/runner/folder>
    build_args:
        BASE_IMAGE: ubuntu:22.04
run:
    name: my-conan-runner-container
$ cd </my/runner/folder>/myparamlib
$ tree
.
...
├── Dockerfile
...
├── configfile
...

docker_param_example_host 配置文件

[settings]
arch=x86_64
build_type=Release
compiler=gcc
compiler.cppstd=gnu17
compiler.libcxx=libstdc++11
compiler.version=11
os=Linux

[runner]
type=docker
configfile=</my/runner/folder>/myparamlib/configfile
cache=copy
remove=false

docker_param_example_build 配置文件

[settings]
arch=x86_64
build_type=Release
compiler=gcc
compiler.cppstd=gnu17
compiler.libcxx=libstdc++11
compiler.version=11
os=Linux

现在是时候创建我们的新库了。

$ conan create . --version 0.1 -pr:h docker_param_example_host -pr:b docker_param_example_build

...

****************************************************
* Building the Docker image: my-conan-runner-image *
****************************************************

Dockerfile path: '</my/runner/folder>/myparamlib/Dockerfile'
Docker build context: '</my/runner/folder>/myparamlib'

Step 1/5 : ARG BASE_IMAGE

Step 2/5 : FROM $BASE_IMAGE

...

Successfully built caa8071cdff7
Successfully tagged my-conan-runner-image:latest

...

**************************************************************************************************************************************************************************
* Running in container: "conan create /root/conanrunner/myparamlib --version 0.1 -pr:h docker_param_example_host -pr:b docker_param_example_build -f json > create.json" *
**************************************************************************************************************************************************************************

...

[ 50%] Building CXX object CMakeFiles/example.dir/src/example.cpp.o
[100%] Linking CXX executable example
[100%] Built target example

======== Testing the package: Executing test ========
myparamlib/0.1 (test package): Running test()
myparamlib/0.1 (test package): RUN: ./example
myparamlib/0.1: Hello World Release!
myparamlib/0.1: __x86_64__ defined
myparamlib/0.1: _GLIBCXX_USE_CXX11_ABI 1
myparamlib/0.1: __cplusplus201703
myparamlib/0.1: __GNUC__11
myparamlib/0.1: __GNUC_MINOR__4
myparamlib/0.1 test_package


**********************************************************************************************
* Restore host cache from: </my/runner/folder>/myparamlib/.conanrunner/docker_cache_save.tgz *
**********************************************************************************************

Saving myparamlib/0.1: mypar36e44205a36b9
Saving myparamlib/0.1:8631cf963dbbb4d7a378a64a6fd1dc57558bc2fe: b/mypare0dc449d4125d/p
Saving myparamlib/0.1:8631cf963dbbb4d7a378a64a6fd1dc57558bc2fe metadata: b/mypare0dc449d4125d/d/metadata

如果我们现在检查 Conan 缓存的状态,我们将看到新的 myparamlib 包。

$ conan list "*:*"
Found 1 pkg/version recipes matching * in local cache
Local Cache
myparamlib
    myparamlib/0.1
    revisions
        11cb359a0526fe9ce3cfefb59c5d1953 (2024-07-08 12:47:21 UTC)
        packages
            8631cf963dbbb4d7a378a64a6fd1dc57558bc2fe
            info
                settings
                arch: x86_64
                build_type: Release
                compiler: gcc
                compiler.cppstd: gnu17
                compiler.libcxx: libstdc++11
                compiler.version: 11
                os: Linux
                options
                fPIC: True
                shared: False