How to use Docker to create and cross-build C and C++ Conan packages
With Docker, you can run different virtual Linux operating systems in a Linux, Mac OSX or Windows machine. It is useful to reproduce build environments, for example to automate CI processes. You can have different images with different compilers or toolchains and run containers every time is needed.
In this section you will find a list of pre-built images with common build tools and compilers as well as Conan installed.
Using Conan inside a container
$ docker run -it --rm conanio/gcc7 /bin/bash
Note
Use sudo
when needed to run docker
.
The previous code will run a shell in container. We have specified:
-it: Keep STDIN open and allocate a pseudo-tty, in other words, we want to type in the container because we are opening a bash.
--rm: Once the container exits, remove the container. Helps to keep clean or hard drive.
conanio/gcc7: Image name, check the available Docker images.
/bin/bash: The command to run
Now we are running on the conangcc7 container we can use Conan normally. In the following example we are creating a package from the recipe by cloning the repository, for OpenSSL. It is always recommended to upgrade Conan from pip first:
$ sudo pip install conan --upgrade # We make sure we are running the latest Conan version
$ git clone https://github.com/conan-io/conan-center-index
$ cd conan-center-index/recipes/openssl/1.x.x
$ conan create . 1.1.1i@
Using the images to cross build packages
You can use the available docker images (with the suffix -i386
, -armv7
and -armv7gh
)
to generate packages for those platforms.
For example, the armv7
images have a toolchain for linux ARM installed, and declared as main compiler with the
environment variables CC
and CXX
. Also, the default Conan profile (~/.conan/profiles/default
)
is adjusted to declare the correct arch (armv7
/ armv7hf
).
This process will run a native compilation inside docker, so we cannot say it is actual cross building, but if we were talking
in terms of cross compiling: the docker service is running in your machine (the build
platform) a docker
image (which is the host
platform) to generate the binaries.
To read about actual cross compiling with Conan we have a dedicated section in the docs: Cross building.
Building and uploading a package along with all its missing dependencies for Linux/armv7hf
is done in few steps:
$ git clone https://github.com/conan-io/conan-center-index
$ cd conan-center-index/recipes/openssl/1.x.x
$ docker run -it -v$(pwd):/home/conan/project --rm conanio/gcc49-armv7hf /bin/bash
# Now we are running on the conangcc49-armv7hf container
# The default profile is automatically adjusted to armv7hf
$ cat ~/.conan/profiles/default
[settings]
os=Linux
arch=armv7hf
compiler=gcc
compiler.version=4.9
compiler.libcxx=libstdc++
build_type=Release
[options]
[build_requires]
[env]
$ sudo pip install conan --upgrade # We make sure we are running the latest Conan version
$ cd project
$ conan create . user/channel --build missing
$ conan remote add myremoteARMV7 http://some.remote.url
$ conan upload "*" -r myremoteARMV7 --all
Available Docker images
We provide a set of images with the most common compilers installed that can be used to generate Conan packages for different profiles. Their dockerfiles can be found in the Conan Docker Tools repository.
Warning
The images listed below are intended for generating open-source library packages and we cannot guarantee any kind of stability. We strongly recommend using your own generated images for production environments taking these dockerfiles as a reference.
GCC images
Version |
Target Arch |
x86_64 |
|
x86 |
|
armv7 |
|
armv7hf |
|
armv7 |
|
armv7hf |
|
x86_64 |
|
x86 |
|
armv7 |
|
armv7hf |
|
x86_64 |
|
x86 |
|
armv7 |
|
armv7hf |
|
x86 |
|
x86_64 |
|
armv7 |
|
armv7hf |
Clang images
Version |
Target Arch |
x86_64 |
|
x86 |
|
x86_64 |
|
x86 |
|
x86_64 |
|
x86 |
|
x86_64 |