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-community/conan-openssl
$ cd conan-openssl
$ conan create . user/channel
Using the images to cross-build packages
You can use the images -i386
, -armv7
and -armv7gh
to cross build
conan packages.
The armv7
images have a cross 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
).
Cross-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-community/conan-openssl
$ cd conan-openssl
$ 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
os_build=Linux
arch=armv7hf
arch_build=x86_64
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
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 |
The Dockerfiles for all these images can be found here.