Boost C++ Library Docker Container

Introduction

Boost is a famous open-source portable C++ library that follows the conventions of C++ STL and is compatible with the C++ Standard Library. Because the standard of Boost library is close to the C++ Standard Library, it has been one of the most valuable sources for additions to the Standard C++ Library.

Because Boost is so portable, we would like to run it inside a Docker container almost anywhere. In this blog post, I would like to quickly show how to build Boost C++ library Docker containers.

Boost Docker Container

We will build Boost C++ library Docker containers for both the x86-64 and aarch64 platforms for any Boost C++ library version specified. The Dockerfile and the Boost-CMake C++ examples are available on GitHub.

Set Boost Library Version

In this case, we will build Boost C++ library 1.80.0.

1
$ BOOST_VERSION=1.80.0

Build Docker Image

The following Boost Dockerfile allows us to build a container that has both Boost and CMake installed.

boost.Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
FROM ubuntu:22.04

ARG BOOST_VERSION=1.80.0
ARG CMAKE_VERSION=3.25.1
ARG NUM_JOBS=8

ENV DEBIAN_FRONTEND noninteractive

# Install package dependencies
RUN apt-get update && \
apt-get install -y --no-install-recommends \
build-essential \
software-properties-common \
autoconf \
automake \
libtool \
pkg-config \
ca-certificates \
libssl-dev \
wget \
git \
curl \
language-pack-en \
locales \
locales-all \
vim \
gdb \
valgrind && \
apt-get clean

# System locale
# Important for UTF-8
ENV LC_ALL en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US.UTF-8

# Install CMake
RUN cd /tmp && \
wget https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}.tar.gz && \
tar xzf cmake-${CMAKE_VERSION}.tar.gz && \
cd cmake-${CMAKE_VERSION} && \
./bootstrap && \
make -j${NUM_JOBS} && \
make install && \
rm -rf /tmp/*

# Install Boost
# https://www.boost.org/doc/libs/1_80_0/more/getting_started/unix-variants.html
RUN cd /tmp && \
BOOST_VERSION_MOD=$(echo $BOOST_VERSION | tr . _) && \
wget https://boostorg.jfrog.io/artifactory/main/release/${BOOST_VERSION}/source/boost_${BOOST_VERSION_MOD}.tar.bz2 && \
tar --bzip2 -xf boost_${BOOST_VERSION_MOD}.tar.bz2 && \
cd boost_${BOOST_VERSION_MOD} && \
./bootstrap.sh --prefix=/usr/local && \
./b2 install && \
rm -rf /tmp/*

To build the Boost Docker image locally, please run the following command.

1
$ docker build -f docker/boost.Dockerfile --build-arg BOOST_VERSION=${BOOST_VERSION} --tag=boost:${BOOST_VERSION} .

Build Docker Image Cross Platform

To build the Boost Docker images for both x86-64 and aarch64, please run the following command. The Boost Docker images will also be pushed to Docker Hub.

1
2
3
$ sudo apt-get install -y binfmt-support qemu-user-static
$ docker buildx create --use --name cross-platform-build
$ docker buildx build -f docker/boost.Dockerfile --platform linux/amd64,linux/arm64 -t leimao/boost:${BOOST_VERSION} --push .

Pull Docker Container

Instead of building the Boost Docker images locally, we could also pull the Boost Docker images that we just built from Docker Hub.

1
2
$ docker pull leimao/boost:${BOOST_VERSION}
$ docker tag leimao/boost:${BOOST_VERSION} boost:${BOOST_VERSION}

Run Docker Container

To build the Boost Docker container, please run the following command.

1
$ docker run -it --rm -v $(pwd):/mnt boost:${BOOST_VERSION}

Boost Examples

To show that the Boost library we just built works inside the Docker container, we will build and run two Boost C++ examples. One build with a Boost header-only library and the other one links to a Boost shared library dynamically.

Build Examples

To build the Boost examples using CMake, please run the following command.

1
2
$ cmake -B build
$ cmake --build build --config Release --parallel

Run Examples

Header-Only Example

The header-only example uses the Boost.Lambda library and will triple the input integer values.

1
2
$ echo 1 2 3 | build/header-only/Triple 
3 6 9

The library-link example uses the Boost.Regex library and will extract the subject from a piece of Email text.

1
2
$ build/library-link/SubjectExtraction < library-link/data/jayne.txt 
Will Success Spoil Rock Hunter?

References

Boost C++ Library Docker Container

https://leimao.github.io/blog/Boost-Docker/

Author

Lei Mao

Posted on

02-07-2023

Updated on

02-07-2023

Licensed under


Comments