From 1e25d58a11f235c3e7a1dc63b0b6ad4a711a26e4 Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Mon, 16 May 2022 22:59:15 +0200 Subject: [PATCH] Alpine image (#1382) --- docker/Dockerfile | 36 +++++++++++++++ docker/Dockerfile.alpine.base | 15 ++++++ docker/README.md | 24 ++++++++++ docker/build.sh | 86 ++++++++++++++++++++++++++++++++++ docker/grpc/CMakeLists.txt | 84 +++++++++++++++++++++++++++++++++ docker/grpc/Dockerfile | 21 +++++++++ docker/thrift/CMakeLists.txt | 87 +++++++++++++++++++++++++++++++++++ docker/thrift/Dockerfile | 21 +++++++++ 8 files changed, 374 insertions(+) create mode 100644 docker/Dockerfile create mode 100644 docker/Dockerfile.alpine.base create mode 100644 docker/README.md create mode 100644 docker/build.sh create mode 100644 docker/grpc/CMakeLists.txt create mode 100644 docker/grpc/Dockerfile create mode 100644 docker/thrift/CMakeLists.txt create mode 100644 docker/thrift/Dockerfile diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000000..118cac62f9 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,36 @@ +ARG BASE_IMAGE=ubuntu:latest +ARG GRPC_IMAGE=grpc-${BASE_IMAGE} +ARG THRIFT_IMAGE=thrift-${BASE_IMAGE} + +FROM ${GRPC_IMAGE} as grpc +FROM ${THRIFT_IMAGE} as thrift + +FROM base-${BASE_IMAGE}-dev AS otel-cpp + +ARG CORES=${nproc} +ARG OTEL_GIT_TAG=v1.3.0 + +COPY --from=grpc / /usr/local +COPY --from=thrift / /usr/local + +#install opentelemetry-cpp +RUN git clone --recurse-submodules -j ${CORES} --depth=1 \ + -b ${OTEL_GIT_TAG} https://github.com/open-telemetry/opentelemetry-cpp.git \ + && cd opentelemetry-cpp \ + && mkdir -p build && cd build \ + && cmake \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE \ + -DWITH_ZIPKIN=ON \ + -DWITH_JAEGER=ON \ + -DCMAKE_INSTALL_PREFIX=/opt/third_party/install \ + -DBUILD_TESTING=OFF \ + -DWITH_OTLP=ON \ + -DWITH_OTLP_GRPC=ON \ + -DBUILD_SHARED_LIBS=ON \ + .. \ + && cmake --build . -j ${CORES} --target install + +FROM scratch as final + +COPY --from=otel-cpp /opt/third_party/install / diff --git a/docker/Dockerfile.alpine.base b/docker/Dockerfile.alpine.base new file mode 100644 index 0000000000..2595efa70f --- /dev/null +++ b/docker/Dockerfile.alpine.base @@ -0,0 +1,15 @@ +ARG BASE_IMAGE=alpine:latest +ARG CORES=${nproc} + +FROM ${BASE_IMAGE} as final + +RUN apk update + +RUN apk add --update alpine-sdk \ + && apk add cmake openssl openssl-dev g++ \ + curl-dev git autoconf libtool linux-headers \ + boost-dev libevent-dev openssl-dev + +RUN mkdir -p /opt/third_party/install + +WORKDIR /opt diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 0000000000..165667117c --- /dev/null +++ b/docker/README.md @@ -0,0 +1,24 @@ +# OpenTelemetry library builder + +**How to use this build script:** + +The build script `build.sh` can be used to build OpenTelemetry and +it's dependencies within the desired base image. +The final image only contains the necessary files in `/`. + +```sh +bash build.sh -b alpine -j ${nproc} -g v1.43.2 -o v1.3.0 -t 0.14.1 + +# copy to current dir +docker create -ti --name otel otel-cpp- bash +docker cp otel:/ ./ +docker rm -f otel + +docker create -ti --name grpc grpc- bash +docker cp grpc:/ ./ +docker rm -f grpc + +docker create -ti --name thrift thrift- bash +docker cp thrift:/ ./ +docker rm -f thrift +``` diff --git a/docker/build.sh b/docker/build.sh new file mode 100644 index 0000000000..53773b739f --- /dev/null +++ b/docker/build.sh @@ -0,0 +1,86 @@ +#!/bin/bash + +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +set -o errexit +set -o nounset +set -o pipefail + +Help() +{ + # Display Help + echo "Add description of the script functions here." + echo + echo "Syntax: scriptTemplate [-b|o|g|j]" + echo "options:" + echo "b Base image" + echo "o OpenTelemetry-cpp git tag" + echo "h Print Help." + echo "g gRPC git tag" + echo "t thrift version" + echo "j Parallel jobs" + echo + echo "how to use:" + echo + echo "docker create -ti --name otel otel-cpp- bash" + echo "docker cp otel:/ ./" + echo "docker rm -f otel" + echo + echo "or:" + echo + echo "COPY --from=otel-cpp- /usr" +} + +base_image=${base_image:="alpine"} +grpc_git_tag=${grpc_git_tag:="v1.43.2"} +thrift_version=${thrift_version:="0.14.1"} +otel_git_tag=${otel_git_tag:="v1.3.0"} +cores=${cores:=1} + +while getopts ":h:b:o:g:j:t:" option; do + case $option in + h) # display Help + Help + exit;; + b) # base image + base_image=$OPTARG + ;; + o) # OpenTelemetry-cpp git tag + otel_git_tag=$OPTARG + ;; + g) # gRPC git tag + grpc_git_tag=$OPTARG + ;; + t) # thrfit version + thrift_version=$OPTARG + ;; + j) # number of cores + cores=$OPTARG + ;; + \?) + Help + exit;; + esac +done + +docker build -t base-${base_image}-dev -f Dockerfile.${base_image}.base . + +pushd grpc/ +docker build --build-arg BASE_IMAGE=base-${base_image}-dev \ + --build-arg GRPC_GIT_TAG=${grpc_git_tag} \ + --build-arg CORES=${cores} \ + -t grpc-${base_image} -f Dockerfile . +popd + +pushd thrift/ +docker build --build-arg BASE_IMAGE=base-${base_image}-dev \ + --build-arg THRIFT_VERSION=${thrift_version} \ + --build-arg CORES=${cores} \ + -t thrift-${base_image} -f Dockerfile . +popd + +docker build --build-arg BASE_IMAGE=${base_image} \ + --build-arg CORES=${cores} \ + --build-arg OTEL_GIT_TAG=${otel_git_tag} \ + -t otel-cpp-${base_image} -f Dockerfile . diff --git a/docker/grpc/CMakeLists.txt b/docker/grpc/CMakeLists.txt new file mode 100644 index 0000000000..8cb69cc6c3 --- /dev/null +++ b/docker/grpc/CMakeLists.txt @@ -0,0 +1,84 @@ +cmake_minimum_required(VERSION 3.11 FATAL_ERROR) + +project( + dependencies + LANGUAGES CXX + VERSION 0.0.1) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_FLAGS "-O2") + +set(CMAKE_BUILD_TYPE + Release + CACHE STRING "Build type" FORCE) + +include(GNUInstallDirs) + +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY + ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY + ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY + ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}) + +set(INSTALL_LIBDIR + ${CMAKE_INSTALL_LIBDIR} + CACHE PATH "directory for libraries") +set(INSTALL_BINDIR + ${CMAKE_INSTALL_BINDIR} + CACHE PATH "directory for executables") +set(INSTALL_INCLUDEDIR + ${CMAKE_INSTALL_INCLUDEDIR} + CACHE PATH "directory for header files") + +set(DEF_INSTALL_CMAKEDIR share/cmake/${PROJECT_NAME}) +set(INSTALL_CMAKEDIR + ${DEF_INSTALL_CMAKEDIR} + CACHE PATH "directory for CMake files") + +set_property(DIRECTORY PROPERTY EP_BASE ${CMAKE_BINARY_DIR}/subs) + +set(STAGED_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/stage) +message(STATUS "${PROJECT_NAME} staged install: ${STAGED_INSTALL_PREFIX}") + +find_package(OpenSSL REQUIRED) +message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}") +message("OpenSSL libraries: ${OPENSSL_LIBRARIES}") + +set(GRPC_GIT_TAG + "v1.45.2" + CACHE STRING "gRPC version") + +include(ExternalProject) +ExternalProject_Add( + grpc + GIT_REPOSITORY https://github.com/grpc/grpc.git + GIT_TAG ${GRPC_GIT_TAG} + GIT_SHALLOW 1 + UPDATE_COMMAND "" + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX} + -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DgRPC_SSL_PROVIDER=package + -DOPENSSL_ROOT_DIR=OpenSSL + -DgRPC_BUILD_TESTS=OFF + -DBUILD_SHARED_LIBS=ON + -DgRPC_BUILD_GRPC_NODE_PLUGIN=OFF + -DgRPC_BUILD_GRPC_OBJECTIVE_C_PLUGIN=OFF + -DgRPC_BUILD_GRPC_PHP_PLUGIN=OFF + -DgRPC_BUILD_GRPC_PHP_PLUGIN=OFF + -DgRPC_BUILD_GRPC_PYTHON_PLUGIN=OFF + -DgRPC_BUILD_GRPC_RUBY_PLUGIN=OFF + CMAKE_CACHE_ARGS -DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS} + TEST_AFTER_INSTALL 0 + DOWNLOAD_NO_PROGRESS 1 + LOG_CONFIGURE 1 + LOG_BUILD 0 + LOG_INSTALL 1) + +install( + DIRECTORY ${STAGED_INSTALL_PREFIX}/ + DESTINATION . + USE_SOURCE_PERMISSIONS) diff --git a/docker/grpc/Dockerfile b/docker/grpc/Dockerfile new file mode 100644 index 0000000000..fee3082b2d --- /dev/null +++ b/docker/grpc/Dockerfile @@ -0,0 +1,21 @@ +ARG BASE_IMAGE=ubuntu:latest +FROM ${BASE_IMAGE} AS grpc + +ARG CORES=${nproc} +ARG GRPC_GIT_TAG=v1.45.2 + +RUN mkdir mkdir -p /opt/third_party/grpc + +WORKDIR /opt/third_party/grpc + +ADD CMakeLists.txt /opt/third_party/grpc + +RUN mkdir build \ + && cd build \ + && cmake -DCMAKE_INSTALL_PREFIX=/opt/third_party/install \ + -DGRPC_GIT_TAG=${GRPC_GIT_TAG} /opt/third_party/grpc \ + && cmake --build . -j ${CORES} --target install + +FROM scratch as final + +COPY --from=grpc /opt/third_party/install / diff --git a/docker/thrift/CMakeLists.txt b/docker/thrift/CMakeLists.txt new file mode 100644 index 0000000000..0028dc48c7 --- /dev/null +++ b/docker/thrift/CMakeLists.txt @@ -0,0 +1,87 @@ +cmake_minimum_required(VERSION 3.11 FATAL_ERROR) + +project( + dependencies + LANGUAGES CXX + VERSION 0.0.1) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_FLAGS "-O2") + +set(CMAKE_BUILD_TYPE + Release + CACHE STRING "Build type" FORCE) + +include(GNUInstallDirs) + +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY + ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY + ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY + ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}) + +set(INSTALL_LIBDIR + ${CMAKE_INSTALL_LIBDIR} + CACHE PATH "directory for libraries") +set(INSTALL_BINDIR + ${CMAKE_INSTALL_BINDIR} + CACHE PATH "directory for executables") +set(INSTALL_INCLUDEDIR + ${CMAKE_INSTALL_INCLUDEDIR} + CACHE PATH "directory for header files") + +set(DEF_INSTALL_CMAKEDIR share/cmake/${PROJECT_NAME}) +set(INSTALL_CMAKEDIR + ${DEF_INSTALL_CMAKEDIR} + CACHE PATH "directory for CMake files") + +set_property(DIRECTORY PROPERTY EP_BASE ${CMAKE_BINARY_DIR}/subs) + +set(STAGED_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/stage) +message(STATUS "${PROJECT_NAME} staged install: ${STAGED_INSTALL_PREFIX}") + +find_package(OpenSSL REQUIRED) +message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}") +message("OpenSSL libraries: ${OPENSSL_LIBRARIES}") + +set(THRIFT_VERSION + "0.14.1" + CACHE STRING "thrift version") + +include(ExternalProject) +ExternalProject_Add( + thrift + URL https://github.com/apache/thrift/archive/refs/tags/v${THRIFT_VERSION}.tar.gz + UPDATE_COMMAND "" + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX} + -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE + -DBUILD_COMPILER=OFF + -DBUILD_CPP=ON + -DBUILD_LIBRARIES=ON + -DBUILD_NODEJS=OFF + -DBUILD_PYTHON=OFF + -DBUILD_JAVASCRIPT=OFF + -DBUILD_C_GLIB=OFF + -DBUILD_JAVA=OFF + -DBUILD_TESTING=OFF + -DBUILD_TUTORIALS=OFF + -DWITH_STDTHREADS=ON + -DWITH_BOOSTTHREADS=OFF + -DWITH_BOOST_FUNCTIONAL=OFF + -DWITH_BOOST_SMART_PTR=OFF + CMAKE_CACHE_ARGS -DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS} + TEST_AFTER_INSTALL 0 + DOWNLOAD_NO_PROGRESS 1 + LOG_CONFIGURE 1 + LOG_BUILD 0 + LOG_INSTALL 1) + +install( + DIRECTORY ${STAGED_INSTALL_PREFIX}/ + DESTINATION . + USE_SOURCE_PERMISSIONS) diff --git a/docker/thrift/Dockerfile b/docker/thrift/Dockerfile new file mode 100644 index 0000000000..2c57199f3f --- /dev/null +++ b/docker/thrift/Dockerfile @@ -0,0 +1,21 @@ +ARG BASE_IMAGE=ubuntu:latest +FROM ${BASE_IMAGE} AS thrift + +ARG CORES=${nproc} +ARG THRIFT_VERSION=0.14.1 + +RUN mkdir mkdir -p /opt/third_party/thrift + +WORKDIR /opt/third_party/th + +ADD CMakeLists.txt /opt/third_party/thrift + +RUN mkdir build \ + && cd build \ + && cmake -DCMAKE_INSTALL_PREFIX=/opt/third_party/install \ + -DTHRIFT_VERSION=${THRIFT_VERSION} /opt/third_party/thrift \ + && cmake --build . -j ${CORES} --target install + +FROM scratch as final + +COPY --from=thrift /opt/third_party/install /