Creating a Conan package using a Docker runner

Warning

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

In this example we are going to see how to create the zlib/1.3.1 Conan packge inside Docker using a runner. Let’s create two profiles and a Dockerfile inside our project folder.

  1. $ cd </my/runner/folder>
  2. $ tree
  3. .
  4. ├── Dockerfile
  5. ├── docker_example_build
  6. └── docker_example_host

docker_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. dockerfile=</my/runner/folder>
  12. cache=copy
  13. remove=true

docker_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

Dockerfile

  1. FROM ubuntu:22.04
  2. RUN apt-get update \
  3. && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
  4. build-essential \
  5. cmake \
  6. python3 \
  7. python3-pip \
  8. python3-venv \
  9. && rm -rf /var/lib/apt/lists/*
  10. RUN pip install conan

In this example we are going to start from a totally clean docker, without containers or images. In addition, we are going to have the conan cache also completely empty.

  1. $ conan list "*:*"
  2. Found 0 pkg/version recipes matching * in local cache
  3. $ docker ps --all
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. $ docker images
  6. REPOSITORY TAG IMAGE ID CREATED SIZE

Now, we are going to clone and build zlib from conan-center-index and create it using our new runner definition.

  1. $ git clone https://github.com/conan-io/conan-center-index.git --depth=1
  2. $ conan create ./conan-center-index/recipes/zlib/all --version 1.3.1 -pr:h </my/runner/folder>/docker_example_host -pr:b </my/runner/folder>/docker_example_build

If we split and analyze the command output, we can see what is happening and where the commands are being executed.

  1. Standard conan execution.
  1. ======== Exporting recipe to the cache ========
  2. zlib/1.3.1: Exporting package recipe: </my/runner/folder>/conan-center-index/recipes/zlib/all/conanfile.py
  3. zlib/1.3.1: exports: File 'conandata.yml' found. Exporting it...
  4. zlib/1.3.1: Calling export_sources()
  5. zlib/1.3.1: Copied 1 '.py' file: conanfile.py
  6. zlib/1.3.1: Copied 1 '.yml' file: conandata.yml
  7. zlib/1.3.1: Copied 1 '.patch' file: 0001-fix-cmake.patch
  8. zlib/1.3.1: Exported to cache folder: /Users/conan/.conan2/p/zlib95420566fc0dd/e
  9. zlib/1.3.1: Exported: zlib/1.3.1#e20364c96c45455608a72543f3a53133 (2024-04-29 17:03:44 UTC)
  10. ======== Input profiles ========
  11. Profile host:
  12. [settings]
  13. arch=x86_64
  14. build_type=Release
  15. compiler=gcc
  16. compiler.cppstd=gnu17
  17. compiler.libcxx=libstdc++11
  18. compiler.version=11
  19. os=Linux
  20. Profile build:
  21. [settings]
  22. arch=x86_64
  23. build_type=Release
  24. compiler=gcc
  25. compiler.cppstd=gnu17
  26. compiler.libcxx=libstdc++11
  27. compiler.version=11
  28. os=Linux
  1. Build docker image
  1. **********************************************
  2. * Building the Docker image: my-conan-runner *
  3. **********************************************
  4. Dockerfile path: '</my/runner/folder>/Dockerfile'
  5. Docker build context: '</my/runner/folder>'
  6. Step 1/4 : FROM ubuntu:22.04
  7. ...
  8. ---> dba927bb0517
  9. Successfully built dba927bb0517
  10. Successfully tagged my-conan-runner:latest
  1. Save the local cache running conan cache save.
  1. ******************************************************************************************************************
  2. * Save host cache in: </my/runner/folder>/conan-center-index/recipes/zlib/all/.conanrunner/local_cache_save.tgz *
  3. ******************************************************************************************************************
  4. Found 1 pkg/version recipes matching * in local cache
  5. Saving zlib/1.3.1: p/zlib95420566fc0dd
  1. Create and initialize the docker container.
  1. *********************************
  2. * Creating the docker container *
  3. *********************************
  4. *****************************************
  5. * Container conan-runner-docker running *
  6. *****************************************
  1. Check if the container has a conan version with the runner feature.
  1. *******************************************
  2. * Running in container: "conan --version" *
  3. *******************************************
  4. Conan version 2.3.0
  1. Initialize the container conan cache using the host copy running conan cache restore.
  1. *********************************************************************************************************
  2. * Running in container: "conan cache restore "/root/conanrunner/all/.conanrunner/local_cache_save.tgz"" *
  3. *********************************************************************************************************
  4. Restore: zlib/1.3.1 in p/zlib95420566fc0dd
  5. Local Cache
  6. zlib
  7. zlib/1.3.1
  8. revisions
  9. e20364c96c45455608a72543f3a53133 (2024-04-29 17:19:32 UTC)
  10. packages
  11. recipe_folder: p/zlib95420566fc0dd
  1. Run the conan create inside the container and build zlib.
  1. *****************************************************************************************************************************************************************************************************************************************************
  2. * Running in container: "conan create /root/conanrunner/all --version 1.3.1 -pr:h /root/conanrunner/all/.conanrunner/profiles/docker_example_host_1 -pr:b /root/conanrunner/all/.conanrunner/profiles/docker_example_build_0 -f json > create.json" *
  3. *****************************************************************************************************************************************************************************************************************************************************
  4. ======== Exporting recipe to the cache ========
  5. zlib/1.3.1: Exporting package recipe: /root/conanrunner/all/conanfile.py
  6. zlib/1.3.1: exports: File 'conandata.yml' found. Exporting it...
  7. zlib/1.3.1: Calling export_sources()
  8. zlib/1.3.1: Copied 1 '.yml' file: conandata.yml
  9. zlib/1.3.1: Copied 1 '.py' file: conanfile.py
  10. zlib/1.3.1: Copied 1 '.patch' file: 0001-fix-cmake.patch
  11. zlib/1.3.1: Exported to cache folder: /root/.conan2/p/zlib95420566fc0dd/e
  12. zlib/1.3.1: Exported: zlib/1.3.1#e20364c96c45455608a72543f3a53133 (2024-04-29 17:19:32 UTC)
  13. ======== Input profiles ========
  14. Profile host:
  15. [settings]
  16. arch=x86_64
  17. build_type=Release
  18. compiler=gcc
  19. compiler.cppstd=gnu17
  20. compiler.libcxx=libstdc++11
  21. compiler.version=11
  22. os=Linux
  23. Profile build:
  24. [settings]
  25. arch=x86_64
  26. build_type=Release
  27. compiler=gcc
  28. compiler.cppstd=gnu17
  29. compiler.libcxx=libstdc++11
  30. compiler.version=11
  31. os=Linux
  32. ======== Computing dependency graph ========
  33. Graph root
  34. cli
  35. Requirements
  36. zlib/1.3.1#e20364c96c45455608a72543f3a53133 - Cache
  37. ======== Computing necessary packages ========
  38. zlib/1.3.1: Forced build from source
  39. Requirements
  40. zlib/1.3.1#e20364c96c45455608a72543f3a53133:b647c43bfefae3f830561ca202b6cfd935b56205 - Build
  41. ======== Installing packages ========
  42. zlib/1.3.1: Calling source() in /root/.conan2/p/zlib95420566fc0dd/s/src
  43. -------- Installing package zlib/1.3.1 (1 of 1) --------
  44. zlib/1.3.1: Building from source
  45. zlib/1.3.1: Package zlib/1.3.1:b647c43bfefae3f830561ca202b6cfd935b56205
  46. zlib/1.3.1: Copying sources to build folder
  47. zlib/1.3.1: Building your package in /root/.conan2/p/b/zlib8dd8e27348e8c/b
  48. zlib/1.3.1: Calling generate()
  49. zlib/1.3.1: Generators folder: /root/.conan2/p/b/zlib8dd8e27348e8c/b/build/Release/generators
  50. zlib/1.3.1: CMakeToolchain generated: conan_toolchain.cmake
  51. zlib/1.3.1: CMakeToolchain generated: /root/.conan2/p/b/zlib8dd8e27348e8c/b/build/Release/generators/CMakePresets.json
  52. zlib/1.3.1: CMakeToolchain generated: /root/.conan2/p/b/zlib8dd8e27348e8c/b/src/CMakeUserPresets.json
  53. zlib/1.3.1: Generating aggregated env files
  54. zlib/1.3.1: Generated aggregated env files: ['conanbuild.sh', 'conanrun.sh']
  55. zlib/1.3.1: Calling build()
  56. zlib/1.3.1: Apply patch (conan): separate static/shared builds, disable debug suffix
  57. zlib/1.3.1: Running CMake.configure()
  58. zlib/1.3.1: RUN: cmake -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE="generators/conan_toolchain.cmake" -DCMAKE_INSTALL_PREFIX="/root/.conan2/p/b/zlib8dd8e27348e8c/p" -DCMAKE_POLICY_DEFAULT_CMP0091="NEW" -DCMAKE_BUILD_TYPE="Release" "/root/.conan2/p/b/zlib8dd8e27348e8c/b/src"
  59. -- Using Conan toolchain: /root/.conan2/p/b/zlib8dd8e27348e8c/b/build/Release/generators/conan_toolchain.cmake
  60. -- Conan toolchain: Setting CMAKE_POSITION_INDEPENDENT_CODE=ON (options.fPIC)
  61. -- Conan toolchain: Setting BUILD_SHARED_LIBS = OFF
  62. -- The C compiler identification is GNU 11.4.0
  63. -- Detecting C compiler ABI info
  64. -- Detecting C compiler ABI info - done
  65. -- Check for working C compiler: /usr/bin/cc - skipped
  66. -- Detecting C compile features
  67. -- Detecting C compile features - done
  68. -- Looking for sys/types.h
  69. -- Looking for sys/types.h - found
  70. -- Looking for stdint.h
  71. -- Looking for stdint.h - found
  72. -- Looking for stddef.h
  73. -- Looking for stddef.h - found
  74. -- Check size of off64_t
  75. -- Check size of off64_t - done
  76. -- Looking for fseeko
  77. -- Looking for fseeko - found
  78. -- Looking for unistd.h
  79. -- Looking for unistd.h - found
  80. -- Renaming
  81. -- /root/.conan2/p/b/zlib8dd8e27348e8c/b/src/zconf.h
  82. -- to 'zconf.h.included' because this file is included with zlib
  83. -- but CMake generates it automatically in the build directory.
  84. -- Configuring done
  85. -- Generating done
  86. -- Build files have been written to: /root/.conan2/p/b/zlib8dd8e27348e8c/b/build/Release
  87. zlib/1.3.1: Running CMake.build()
  88. zlib/1.3.1: RUN: cmake --build "/root/.conan2/p/b/zlib8dd8e27348e8c/b/build/Release" -- -j16
  89. [ 12%] Building C object CMakeFiles/zlib.dir/adler32.c.o
  90. [ 12%] Building C object CMakeFiles/zlib.dir/compress.c.o
  91. [ 18%] Building C object CMakeFiles/zlib.dir/deflate.c.o
  92. [ 25%] Building C object CMakeFiles/zlib.dir/crc32.c.o
  93. [ 31%] Building C object CMakeFiles/zlib.dir/gzlib.c.o
  94. [ 37%] Building C object CMakeFiles/zlib.dir/gzread.c.o
  95. [ 43%] Building C object CMakeFiles/zlib.dir/gzclose.c.o
  96. [ 56%] Building C object CMakeFiles/zlib.dir/infback.c.o
  97. [ 56%] Building C object CMakeFiles/zlib.dir/gzwrite.c.o
  98. [ 62%] Building C object CMakeFiles/zlib.dir/inflate.c.o
  99. [ 68%] Building C object CMakeFiles/zlib.dir/inffast.c.o
  100. [ 75%] Building C object CMakeFiles/zlib.dir/trees.c.o
  101. [ 81%] Building C object CMakeFiles/zlib.dir/zutil.c.o
  102. [ 87%] Building C object CMakeFiles/zlib.dir/uncompr.c.o
  103. [ 93%] Building C object CMakeFiles/zlib.dir/inftrees.c.o
  104. [100%] Linking C static library libz.a
  105. [100%] Built target zlib
  106. zlib/1.3.1: Package 'b647c43bfefae3f830561ca202b6cfd935b56205' built
  107. zlib/1.3.1: Build folder /root/.conan2/p/b/zlib8dd8e27348e8c/b/build/Release
  108. zlib/1.3.1: Generating the package
  109. zlib/1.3.1: Packaging in folder /root/.conan2/p/b/zlib8dd8e27348e8c/p
  110. zlib/1.3.1: Calling package()
  111. zlib/1.3.1: Running CMake.install()
  112. zlib/1.3.1: RUN: cmake --install "/root/.conan2/p/b/zlib8dd8e27348e8c/b/build/Release" --prefix "/root/.conan2/p/b/zlib8dd8e27348e8c/p"
  113. -- Install configuration: "Release"
  114. -- Installing: /root/.conan2/p/b/zlib8dd8e27348e8c/p/lib/libz.a
  115. -- Installing: /root/.conan2/p/b/zlib8dd8e27348e8c/p/include/zconf.h
  116. -- Installing: /root/.conan2/p/b/zlib8dd8e27348e8c/p/include/zlib.h
  117. zlib/1.3.1: package(): Packaged 1 file: LICENSE
  118. zlib/1.3.1: package(): Packaged 2 '.h' files: zlib.h, zconf.h
  119. zlib/1.3.1: package(): Packaged 1 '.a' file: libz.a
  120. zlib/1.3.1: Created package revision fd85b1346d5377ae2465645768e62bf2
  121. zlib/1.3.1: Package 'b647c43bfefae3f830561ca202b6cfd935b56205' created
  122. zlib/1.3.1: Full package reference: zlib/1.3.1#e20364c96c45455608a72543f3a53133:b647c43bfefae3f830561ca202b6cfd935b56205#fd85b1346d5377ae2465645768e62bf2
  123. zlib/1.3.1: Package folder /root/.conan2/p/b/zlib8dd8e27348e8c/p
  124. WARN: deprecated: Usage of deprecated Conan 1.X features that will be removed in Conan 2.X:
  125. WARN: deprecated: 'cpp_info.names' used in: zlib/1.3.1
  126. ======== Launching test_package ========
  127. ======== Computing dependency graph ========
  128. Graph root
  129. zlib/1.3.1 (test package): /root/conanrunner/all/test_package/conanfile.py
  130. Requirements
  131. zlib/1.3.1#e20364c96c45455608a72543f3a53133 - Cache
  132. ======== Computing necessary packages ========
  133. Requirements
  134. zlib/1.3.1#e20364c96c45455608a72543f3a53133:b647c43bfefae3f830561ca202b6cfd935b56205#fd85b1346d5377ae2465645768e62bf2 - Cache
  135. ======== Installing packages ========
  136. zlib/1.3.1: Already installed! (1 of 1)
  137. WARN: deprecated: Usage of deprecated Conan 1.X features that will be removed in Conan 2.X:
  138. WARN: deprecated: 'cpp_info.names' used in: zlib/1.3.1
  139. ======== Testing the package ========
  140. Removing previously existing 'test_package' build folder: /root/conanrunner/all/test_package/build/gcc-11-x86_64-gnu17-release
  141. zlib/1.3.1 (test package): Test package build: build/gcc-11-x86_64-gnu17-release
  142. zlib/1.3.1 (test package): Test package build folder: /root/conanrunner/all/test_package/build/gcc-11-x86_64-gnu17-release
  143. zlib/1.3.1 (test package): Writing generators to /root/conanrunner/all/test_package/build/gcc-11-x86_64-gnu17-release/generators
  144. zlib/1.3.1 (test package): Generator 'CMakeToolchain' calling 'generate()'
  145. zlib/1.3.1 (test package): CMakeToolchain generated: conan_toolchain.cmake
  146. zlib/1.3.1 (test package): CMakeToolchain generated: /root/conanrunner/all/test_package/build/gcc-11-x86_64-gnu17-release/generators/CMakePresets.json
  147. zlib/1.3.1 (test package): CMakeToolchain generated: /root/conanrunner/all/test_package/CMakeUserPresets.json
  148. zlib/1.3.1 (test package): Generator 'CMakeDeps' calling 'generate()'
  149. zlib/1.3.1 (test package): CMakeDeps necessary find_package() and targets for your CMakeLists.txt
  150. find_package(ZLIB)
  151. target_link_libraries(... ZLIB::ZLIB)
  152. zlib/1.3.1 (test package): Generator 'VirtualRunEnv' calling 'generate()'
  153. zlib/1.3.1 (test package): Generating aggregated env files
  154. zlib/1.3.1 (test package): Generated aggregated env files: ['conanrun.sh', 'conanbuild.sh']
  155. ======== Testing the package: Building ========
  156. zlib/1.3.1 (test package): Calling build()
  157. zlib/1.3.1 (test package): Running CMake.configure()
  158. zlib/1.3.1 (test package): RUN: cmake -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE="generators/conan_toolchain.cmake" -DCMAKE_INSTALL_PREFIX="/root/conanrunner/all/test_package" -DCMAKE_POLICY_DEFAULT_CMP0091="NEW" -DCMAKE_BUILD_TYPE="Release" "/root/conanrunner/all/test_package"
  159. -- Using Conan toolchain: /root/conanrunner/all/test_package/build/gcc-11-x86_64-gnu17-release/generators/conan_toolchain.cmake
  160. -- Conan toolchain: C++ Standard 17 with extensions ON
  161. -- The C compiler identification is GNU 11.4.0
  162. -- Detecting C compiler ABI info
  163. -- Detecting C compiler ABI info - done
  164. -- Check for working C compiler: /usr/bin/cc - skipped
  165. -- Detecting C compile features
  166. -- Detecting C compile features - done
  167. -- Conan: Target declared 'ZLIB::ZLIB'
  168. -- Configuring done
  169. -- Generating done
  170. -- Build files have been written to: /root/conanrunner/all/test_package/build/gcc-11-x86_64-gnu17-release
  171. zlib/1.3.1 (test package): Running CMake.build()
  172. zlib/1.3.1 (test package): RUN: cmake --build "/root/conanrunner/all/test_package/build/gcc-11-x86_64-gnu17-release" -- -j16
  173. [ 50%] Building C object CMakeFiles/test_package.dir/test_package.c.o
  174. [100%] Linking C executable test_package
  175. [100%] Built target test_package
  176. ======== Testing the package: Executing test ========
  177. zlib/1.3.1 (test package): Running test()
  178. zlib/1.3.1 (test package): RUN: ./test_package
  179. Compressed size is: 21
  180. Compressed string is: Conan Package Manager
  181. Compressed size is: 22
  182. Compressed string is: xsKHLNLOUMRE
  183. ZLIB VERSION: 1.3.1
  1. Copy just the package created inside the container using the pkglist.json info from the previous conan create, restore this new package inside the host cache running a conan cache save and remove the container.
  1. **********************************************************************************************************************************
  2. * Running in container: "conan cache save --list=pkglist.json --file "/root/conanrunner/all"/.conanrunner/docker_cache_save.tgz" *
  3. **********************************************************************************************************************************
  4. Saving zlib/1.3.1: p/zlib95420566fc0dd
  5. Saving zlib/1.3.1:b647c43bfefae3f830561ca202b6cfd935b56205: p/b/zlib8dd8e27348e8c/p
  6. Saving zlib/1.3.1:b647c43bfefae3f830561ca202b6cfd935b56205 metadata: p/b/zlib8dd8e27348e8c/d/metadata
  7. Local Cache
  8. zlib
  9. zlib/1.3.1
  10. revisions
  11. e20364c96c45455608a72543f3a53133 (2024-04-29 17:19:32 UTC)
  12. packages
  13. b647c43bfefae3f830561ca202b6cfd935b56205
  14. revisions
  15. fd85b1346d5377ae2465645768e62bf2
  16. package_folder: p/b/zlib8dd8e27348e8c/p
  17. metadata_folder: p/b/zlib8dd8e27348e8c/d/metadata
  18. info
  19. settings
  20. os: Linux
  21. arch: x86_64
  22. compiler: gcc
  23. compiler.version: 11
  24. build_type: Release
  25. options
  26. fPIC: True
  27. shared: False
  28. recipe_folder: p/zlib95420566fc0dd
  29. ************************************************************************************************************************
  30. * Restore host cache from: </my/runner/folder>/conan-center-index/recipes/zlib/all/.conanrunner/docker_cache_save.tgz *
  31. ************************************************************************************************************************
  32. Restore: zlib/1.3.1 in p/zlib95420566fc0dd
  33. Restore: zlib/1.3.1:b647c43bfefae3f830561ca202b6cfd935b56205 in p/b/zlib8dd8e27348e8c/p
  34. Restore: zlib/1.3.1:b647c43bfefae3f830561ca202b6cfd935b56205 metadata in p/b/zlib8dd8e27348e8c/d/metadata
  35. **********************
  36. * Stopping container *
  37. **********************
  38. **********************
  39. * Removing container *
  40. **********************

If we now check the status of our conan and docker cache, we will see the new zlib package compile for Linux and the new docker image. We don’t have any container because we define remove=true

  1. $ conan list "*:*"
  2. Found 1 pkg/version recipes matching * in local cache
  3. Local Cache
  4. zlib
  5. zlib/1.3.1
  6. revisions
  7. e20364c96c45455608a72543f3a53133 (2024-04-29 17:18:07 UTC)
  8. packages
  9. b647c43bfefae3f830561ca202b6cfd935b56205
  10. info
  11. settings
  12. arch: x86_64
  13. build_type: Release
  14. compiler: gcc
  15. compiler.version: 11
  16. os: Linux
  17. options
  18. fPIC: True
  19. shared: False
  20. $ docker ps --all
  21. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  22. $ docker images
  23. REPOSITORY TAG IMAGE ID CREATED SIZE
  24. my-conan-runner latest 383b905f352e 22 minutes ago 531MB
  25. ubuntu 22.04 437ec753bef3 12 days ago 77.9MB