|
| 1 | +.. _examples_runners_docker_configfile_build_args: |
| 2 | + |
| 3 | +Using a docker runner configfile to parameterize a Dockerfile |
| 4 | +============================================================= |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +.. code-block:: bash |
| 9 | +
|
| 10 | + $ cd </my/runner/folder> |
| 11 | + $ tree |
| 12 | + . |
| 13 | + ├── Dockerfile |
| 14 | + ├── configfile |
| 15 | + ├── docker_example_build |
| 16 | + └── docker_example_host |
| 17 | +
|
| 18 | +``docker_example_host`` profile |
| 19 | + |
| 20 | +.. code-block:: text |
| 21 | +
|
| 22 | + [settings] |
| 23 | + arch=x86_64 |
| 24 | + build_type=Release |
| 25 | + compiler=gcc |
| 26 | + compiler.cppstd=gnu17 |
| 27 | + compiler.libcxx=libstdc++11 |
| 28 | + compiler.version=11 |
| 29 | + os=Linux |
| 30 | + [runner] |
| 31 | + type=docker |
| 32 | + configfile=</my/runner/folder>/configfile |
| 33 | + cache=copy |
| 34 | + remove=false |
| 35 | +
|
| 36 | +``docker_example_build`` profile |
| 37 | + |
| 38 | +.. code-block:: text |
| 39 | +
|
| 40 | + [settings] |
| 41 | + arch=x86_64 |
| 42 | + build_type=Release |
| 43 | + compiler=gcc |
| 44 | + compiler.cppstd=gnu17 |
| 45 | + compiler.libcxx=libstdc++11 |
| 46 | + compiler.version=11 |
| 47 | + os=Linux |
| 48 | +
|
| 49 | +``configfile`` |
| 50 | + |
| 51 | +.. code-block:: yaml |
| 52 | +
|
| 53 | + image: my-conan-runner-image |
| 54 | + build: |
| 55 | + dockerfile: </my/runner/folder> |
| 56 | + build_context: </my/runner/folder> |
| 57 | + build_args: |
| 58 | + BASE_IMAGE: ubuntu:22.04 |
| 59 | + run: |
| 60 | + name: my-conan-runner-container |
| 61 | +
|
| 62 | +
|
| 63 | +.. code-block:: docker |
| 64 | + :caption: Dockerfile |
| 65 | +
|
| 66 | + ARG BASE_IMAGE |
| 67 | + FROM $BASE_IMAGE |
| 68 | + RUN apt-get update \ |
| 69 | + && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ |
| 70 | + build-essential \ |
| 71 | + cmake \ |
| 72 | + python3 \ |
| 73 | + python3-pip \ |
| 74 | + python3-venv \ |
| 75 | + && rm -rf /var/lib/apt/lists/* |
| 76 | + RUN pip install conan |
| 77 | +
|
| 78 | +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. |
| 79 | + |
| 80 | +.. code-block:: bash |
| 81 | +
|
| 82 | + $ conan list "*:*" |
| 83 | + Found 0 pkg/version recipes matching * in local cache |
| 84 | +
|
| 85 | + $ docker ps --all |
| 86 | + CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
| 87 | +
|
| 88 | + $ docker images |
| 89 | + REPOSITORY TAG IMAGE ID CREATED SIZE |
| 90 | +
|
| 91 | +
|
| 92 | +Now, we are going to clone and build zlib from conan-center-index and create it using our new runner definition. |
| 93 | + |
| 94 | +.. code-block:: bash |
| 95 | + |
| 96 | + $ git clone https://github.com/conan-io/conan-center-index.git --depth 1 |
| 97 | + $ 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 |
| 98 | +
|
| 99 | + ... |
| 100 | +
|
| 101 | + ┌──────────────────────────────────────────────────┐ |
| 102 | + | Building the Docker image: my-conan-runner-image | |
| 103 | + └──────────────────────────────────────────────────┘ |
| 104 | +
|
| 105 | + Dockerfile path: '</my/runner/folder>/Dockerfile' |
| 106 | + Docker build context: '</my/runner/folder>' |
| 107 | +
|
| 108 | + Step 1/5 : ARG BASE_IMAGE |
| 109 | +
|
| 110 | + Step 2/5 : FROM $BASE_IMAGE |
| 111 | +
|
| 112 | + ... |
| 113 | +
|
| 114 | + Successfully built 286df085400f |
| 115 | + Successfully tagged my-conan-runner-image:latest |
| 116 | +
|
| 117 | + ... |
| 118 | +
|
| 119 | + ┌───────────────────────────────┐ |
| 120 | + | Creating the docker container | |
| 121 | + └───────────────────────────────┘ |
| 122 | +
|
| 123 | +
|
| 124 | + ┌─────────────────────────────────────────┐ |
| 125 | + | Container my-conan-runner-image running | |
| 126 | + └─────────────────────────────────────────┘ |
| 127 | +
|
| 128 | +
|
| 129 | + ┌─────────────────────────────────────────┐ |
| 130 | + | Running in container: "conan --version" | |
| 131 | + └─────────────────────────────────────────┘ |
| 132 | +
|
| 133 | + ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ |
| 134 | + | Restore host cache from: </my/runner/folder>/conan-center-index/recipes/zlib/all/.conanrunner/docker_cache_save.tgz | |
| 135 | + └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ |
| 136 | +
|
| 137 | + Restore: zlib/1.3.1 in p/zlib95420566fc0dd |
| 138 | + Restore: zlib/1.3.1:b647c43bfefae3f830561ca202b6cfd935b56205 in p/zlibd59462fc4358e/p |
| 139 | + Restore: zlib/1.3.1:b647c43bfefae3f830561ca202b6cfd935b56205 metadata in p/zlibd59462fc4358e/d/metadata |
| 140 | +
|
| 141 | + ┌────────────────────┐ |
| 142 | + | Stopping container | |
| 143 | + └────────────────────┘ |
| 144 | +
|
| 145 | +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. |
| 146 | + |
| 147 | +.. code-block:: bash |
| 148 | +
|
| 149 | + $ conan list "*:*" |
| 150 | + Found 1 pkg/version recipes matching * in local cache |
| 151 | + Local Cache |
| 152 | + zlib |
| 153 | + zlib/1.3.1 |
| 154 | + revisions |
| 155 | + e20364c96c45455608a72543f3a53133 (2024-04-29 17:18:07 UTC) |
| 156 | + packages |
| 157 | + b647c43bfefae3f830561ca202b6cfd935b56205 |
| 158 | + info |
| 159 | + settings |
| 160 | + arch: x86_64 |
| 161 | + build_type: Release |
| 162 | + compiler: gcc |
| 163 | + compiler.version: 11 |
| 164 | + os: Linux |
| 165 | + options |
| 166 | + fPIC: True |
| 167 | + shared: False |
| 168 | +
|
| 169 | + $ docker ps --all |
| 170 | + CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
| 171 | + 1379072ae424 my-conan-runner-image "/bin/bash -c 'while…" 17 seconds ago Exited (137) 2 seconds ago my-conan-runner-image |
| 172 | +
|
| 173 | + $ docker images |
| 174 | + REPOSITORY TAG IMAGE ID CREATED SIZE |
| 175 | + my-conan-runner latest 383b905f352e 22 minutes ago 531MB |
| 176 | + ubuntu 22.04 437ec753bef3 12 days ago 77.9MB |
| 177 | +
|
| 178 | +If we run the ``conan create`` command again we will see how Conan reuses the previous container because we have set ``remove=False``. |
| 179 | + |
| 180 | +.. code-block:: bash |
| 181 | + |
| 182 | + $ 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 |
| 183 | +
|
| 184 | + ... |
| 185 | +
|
| 186 | + ┌───────────────────────────────┐ |
| 187 | + | Starting the docker container | |
| 188 | + └───────────────────────────────┘ |
| 189 | +
|
| 190 | + ... |
0 commit comments