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.

In this example we are going to see how to use a docker runner configfile to define our Dockerfile base image. Let’s create two profiles and a Dockerfile inside our project folder.

  1. $ cd </my/runner/folder>
  2. $ tree
  3. .
  4. ├── Dockerfile
  5. ├── configfile
  6. ├── docker_example_build
  7. └── 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. configfile=</my/runner/folder>/configfile
  12. cache=copy
  13. remove=false

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

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

Dockerfile

  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

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
  3. ...
  4. ****************************************************
  5. * Building the Docker image: my-conan-runner-image *
  6. ****************************************************
  7. Dockerfile path: '</my/runner/folder>/Dockerfile'
  8. Docker build context: '</my/runner/folder>'
  9. Step 1/5 : ARG BASE_IMAGE
  10. Step 2/5 : FROM $BASE_IMAGE
  11. ...
  12. Successfully built 286df085400f
  13. Successfully tagged my-conan-runner-image:latest
  14. ...
  15. *********************************
  16. * Creating the docker container *
  17. *********************************
  18. *******************************************
  19. * Container my-conan-runner-image running *
  20. *******************************************
  21. *******************************************
  22. * Running in container: "conan --version" *
  23. *******************************************
  24. ************************************************************************************************************************
  25. * Restore host cache from: </my/runner/folder>/conan-center-index/recipes/zlib/all/.conanrunner/docker_cache_save.tgz *
  26. ************************************************************************************************************************
  27. Restore: zlib/1.3.1 in p/zlib95420566fc0dd
  28. Restore: zlib/1.3.1:b647c43bfefae3f830561ca202b6cfd935b56205 in p/zlibd59462fc4358e/p
  29. Restore: zlib/1.3.1:b647c43bfefae3f830561ca202b6cfd935b56205 metadata in p/zlibd59462fc4358e/d/metadata
  30. **********************
  31. * Stopping container *
  32. **********************

If we now check the status of our Conan and docker cache, we will see the zlib package compiled for Linux and the new docker image and container.

  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. 1379072ae424 my-conan-runner-image "/bin/bash -c 'while…" 17 seconds ago Exited (137) 2 seconds ago my-conan-runner-image
  23. $ docker images
  24. REPOSITORY TAG IMAGE ID CREATED SIZE
  25. my-conan-runner latest 383b905f352e 22 minutes ago 531MB
  26. ubuntu 22.04 437ec753bef3 12 days ago 77.9MB

If we run the conan create command again we will see how Conan reuses the previous container because we have set remove=False.

  1. $ 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
  2. ...
  3. *********************************
  4. * Starting the docker container *
  5. *********************************
  6. ...