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

Sharing a local folder with a Docker container

You can share a local folder with your container, for example a project:

$ git clone https://github.com/conan-community/conan-openssl
$ cd conan-openssl
$ docker run -it -v$(pwd):/home/conan/project --rm conanio/gcc7 /bin/bash
  • v$(pwd):/home/conan/project: We are mapping the current directory (conan-openssl) to the container /home/conan/project directory, so anything we change in this shared folder, will be reflected in our host machine.

# Now we are running on the conangcc7 container
$ 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 myremote http://some.remote.url
$ conan upload "*" -r myremote --all

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-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
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

conanio/gcc49 (GCC 4.9)

x86_64

conanio/gcc49-i386 (GCC 4.9)

x86

conanio/gcc49-armv7 (GCC 4.9)

armv7

conanio/gcc49-armv7hf (GCC 4.9)

armv7hf

conanio/gcc5-armv7 (GCC 5)

armv7

conanio/gcc5-armv7hf (GCC 5)

armv7hf

conanio/gcc5 (GCC 5)

x86_64

conanio/gcc5-i386 (GCC 5)

x86

conanio/gcc5-armv7 (GCC 5)

armv7

conanio/gcc5-armv7hf (GCC 5)

armv7hf

conanio/gcc6 (GCC 6)

x86_64

conanio/gcc6-i386 (GCC 6)

x86

conanio/gcc6-armv7 (GCC 6)

armv7

conanio/gcc6-armv7hf: (GCC 6)

armv7hf

conanio/gcc7-i386 (GCC 7)

x86

conanio/gcc7 (GCC 7)

x86_64

conanio/gcc7-armv7 (GCC 7)

armv7

conanio/gcc7-armv7hf (GCC 7)

armv7hf

Clang images

Version

Target Arch

conanio/clang38 (Clang 3.8)

x86_64

conanio/clang39-i386 (Clang 3.9)

x86

conanio/clang39 (Clang 3.9)

x86_64

conanio/clang40-i386 (Clang 4)

x86

conanio/clang40 (Clang 4)

x86_64

conanio/clang50-i386 (Clang 5)

x86

conanio/clang50 (Clang 5)

x86_64