C/C++ applications and build system scripts are monolith in nature. Achieving binary compatibility is becoming extremely difficult as applications include source information from a variety of local and third party sources, targeting a variety of platforms. Even classically monolithic applications may include a complex dependency graph, including transitive dependencies statically or dynamically linked. Another factor that adds to the complexity is that the deployment framework for different technology types is moving towards using Docker containers and the adoption of Kubernetes or Kubernetes variants.
In this blog post, we’ll focus on improving the C++ application build and deployment experience by using Conan to build your App using modularized dependencies. We’ll then proceed to use OpenShift S2I to Dockerize your C++ applications. Also, to streamline the deployment, we’ve added JFrog Artifactory.
To get familiar with the process, we’ve created an example that displays the process of building, containerizing and running your C++ applications using Conan. We’ll build and containerize the C++ application that’s hosted on Github using the cpp–conan-builder:0.1 builder image.
Next, we can run the containerized C++ app,
timer-app:0.1, that we got from the above command. Docker Image that includes both the output binary and the scripts responsible for running the application.
Let’s get started by creating the builder image.
Step 1: Creating a Builder Image for Conan
The source code for this sample project is available in Github.
- Clone the Conan builder image, and generate the builder image.
The builder image includes Conan, gcc 7.x, make and other tools required for building C++ projects.
The following Docker file corresponds to the builder image.
The builder image packages S2I scripts that are responsible for building and running the application.
Step 2: Creating a Reproducible Image Using S2I Scripts
S2I allows you to easily create reproducible Docker images using your C++ source code as input and produces a new image that runs the application as output. It includes the following two main scripts:
- Assemble Script: Builds the C++ project and generates the binary file. We use Conan to avoid rebuilding C++ dependencies including the transitive dependencies.
- Run Script: Runs the application.
Our sample C++ project relies on the Poco library. Poco depends on OpenSSL, and OpenSSL depends upon zlib.
Using Conan, the dependencies are downloaded automatically from the conan-center by default which removes the need to build Poco, OpenSSL and zlib.
For more information on Conan and the related project, click here.
2a. Running the Assemble Script
2b. Running the Builder Script
The builder script exposes the parameters to configure Conan to download packages from Artifactory. It is recommended to either use the multi-step build pattern or use short-lived access tokens to authenticate with Artifactory as displayed below.
Run the C++ containerized app created in the previous step.
The following script runs when the container starts:
A Peek Behind the Scenes: Running C++ Containerized Apps
The following five steps are triggered by the Source to Image command:
- The container for the builder image starts and includes the required set of tools for compiling the C++ projects.
- A Git clone of the project is executed.
- The assemble (build script) is executed that is responsible for building the project using Conan. In this step, the pre-compiled packages of dependencies are downloaded from Conan repositories (Artifactory, conan-center).
Once completed, the output binary is created.
- A new Docker manifest is created that has an extra Docker image layer that includes the output binary generated in step 3. This image is tagged using the name specified in the S2I command.
- The run script is executed when the containerized application starts.
Now that you’re up and running, hop over to learn more about Conan and Artifactory.