Using a docker runner configfile to parameterize a Dockerfile

Warning

This feature is experimental and subject to breaking changes. See the Conan stability section for more information.

If you need more control over the build and execution of the container, you can define more parameters inside a configfile yaml.

For example, you can add arguments in the build step or environment variables when you launch the container.

To use it, you just need to add it in the host profile.

  1. [settings]
  2. ...
  3. [runner]
  4. type=docker
  5. configfile=</my/runner/folder>/configfile
  6. cache=copy
  7. remove=false

How to use

Let’s create a Dockerfile inside your project folder, a cmake_lib myparamlib like the “Creating a Conan package using a Docker runner” example and two profiles.

  1. $ cd </my/runner/folder>
  2. $ mkdir myparamlib
  3. $ cd myparamlib
  4. $ conan new cmake_lib -d name=myparamlib -d version=0.1
  5. $ cd </my/runner/folder>
  6. $ tree
  7. .
  8. ├── CMakeLists.txt
  9. ├── conanfile.py
  10. ├── include
  11. └── myparamlib.h
  12. ├── src
  13. └── myparamlib.cpp
  14. └── test_package
  15. ├── CMakeLists.txt
  16. ├── conanfile.py
  17. └── src
  18. └── example.cpp
  1. ARG BASE_IMAGE
  2. FROM $BASE_IMAGE
  3. RUN apt-get update \
  4. && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
  5. build-essential \
  6. cmake \
  7. python3 \
  8. python3-pip \
  9. python3-venv \
  10. && rm -rf /var/lib/apt/lists/*
  11. RUN pip install conan

configfile

  1. image: my-conan-runner-image
  2. build:
  3. dockerfile: </my/runner/folder>
  4. build_context: </my/runner/folder>
  5. build_args:
  6. BASE_IMAGE: ubuntu:22.04
  7. run:
  8. name: my-conan-runner-container
  1. $ cd </my/runner/folder>/myparamlib
  2. $ tree
  3. .
  4. ...
  5. ├── Dockerfile
  6. ...
  7. ├── configfile
  8. ...

docker_param_example_host profile

  1. [settings]
  2. arch=x86_64
  3. build_type=Release
  4. compiler=gcc
  5. compiler.cppstd=gnu17
  6. compiler.libcxx=libstdc++11
  7. compiler.version=11
  8. os=Linux
  9. [runner]
  10. type=docker
  11. configfile=</my/runner/folder>/myparamlib/configfile
  12. cache=copy
  13. remove=false

docker_param_example_build profile

  1. [settings]
  2. arch=x86_64
  3. build_type=Release
  4. compiler=gcc
  5. compiler.cppstd=gnu17
  6. compiler.libcxx=libstdc++11
  7. compiler.version=11
  8. os=Linux

Now it’s time to create our new library.

  1. $ conan create . --version 0.1 -pr:h docker_param_example_host -pr:b docker_param_example_build
  2. ...
  3. ****************************************************
  4. * Building the Docker image: my-conan-runner-image *
  5. ****************************************************
  6. Dockerfile path: '</my/runner/folder>/myparamlib/Dockerfile'
  7. Docker build context: '</my/runner/folder>/myparamlib'
  8. Step 1/5 : ARG BASE_IMAGE
  9. Step 2/5 : FROM $BASE_IMAGE
  10. ...
  11. Successfully built caa8071cdff7
  12. Successfully tagged my-conan-runner-image:latest
  13. ...
  14. **************************************************************************************************************************************************************************
  15. * 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" *
  16. **************************************************************************************************************************************************************************
  17. ...
  18. [ 50%] Building CXX object CMakeFiles/example.dir/src/example.cpp.o
  19. [100%] Linking CXX executable example
  20. [100%] Built target example
  21. ======== Testing the package: Executing test ========
  22. myparamlib/0.1 (test package): Running test()
  23. myparamlib/0.1 (test package): RUN: ./example
  24. myparamlib/0.1: Hello World Release!
  25. myparamlib/0.1: __x86_64__ defined
  26. myparamlib/0.1: _GLIBCXX_USE_CXX11_ABI 1
  27. myparamlib/0.1: __cplusplus201703
  28. myparamlib/0.1: __GNUC__11
  29. myparamlib/0.1: __GNUC_MINOR__4
  30. myparamlib/0.1 test_package
  31. **********************************************************************************************
  32. * Restore host cache from: </my/runner/folder>/myparamlib/.conanrunner/docker_cache_save.tgz *
  33. **********************************************************************************************
  34. Saving myparamlib/0.1: mypar36e44205a36b9
  35. Saving myparamlib/0.1:8631cf963dbbb4d7a378a64a6fd1dc57558bc2fe: b/mypare0dc449d4125d/p
  36. Saving myparamlib/0.1:8631cf963dbbb4d7a378a64a6fd1dc57558bc2fe metadata: b/mypare0dc449d4125d/d/metadata

If we now check the status of our conan cache, we will see the new myparamlib pacakge.

  1. $ conan list "*:*"
  2. Found 1 pkg/version recipes matching * in local cache
  3. Local Cache
  4. myparamlib
  5. myparamlib/0.1
  6. revisions
  7. 11cb359a0526fe9ce3cfefb59c5d1953 (2024-07-08 12:47:21 UTC)
  8. packages
  9. 8631cf963dbbb4d7a378a64a6fd1dc57558bc2fe
  10. info
  11. settings
  12. arch: x86_64
  13. build_type: Release
  14. compiler: gcc
  15. compiler.cppstd: gnu17
  16. compiler.libcxx: libstdc++11
  17. compiler.version: 11
  18. os: Linux
  19. options
  20. fPIC: True
  21. shared: False