From b51a07af249aa6f6e0b28a0433b1506e18d69219 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Thu, 29 Oct 2020 18:35:36 +0100 Subject: [BUILD] add CMake support to build docker images based on Ubuntu 20.04 --- CMakeLists.txt | 13 ++++++++++++- README.md | 28 ++++++++++++++++++++++++++++ cmake/DockerImage.cmake | 25 +++++++++++++++++++++++++ cmake/FindDocker.cmake | 20 ++++++++++++++++++++ pkg/docker/Dockerfile | 28 ++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 cmake/DockerImage.cmake create mode 100644 cmake/FindDocker.cmake create mode 100644 pkg/docker/Dockerfile diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cf891d..4dcbcef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -131,7 +131,7 @@ if(CMAKE_BUILD_TYPE MATCHES Release) set(CPACK_RESOURCE_FILE_README ${CMAKE_CURRENT_SOURCE_DIR}/README.md) # set DEB generator specific packaging options - set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6, libfuse2, libjansson") + set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6, libfuse2, libjansson4") if(DNBD3_KERNEL_MODULE) file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/package/deb/postinst "depmod -a\n") file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/package/deb/postrm "depmod -a\n") @@ -191,6 +191,17 @@ if(CMAKE_BUILD_TYPE MATCHES Release) -P ${PROJECT_MODULES_DIR}/PostVersionPackaging.cmake COMMENT "Cleanup version.h" DEPENDS package_source_main) + + # include target to make docker image + if(NOT DNBD3_KERNEL_MODULE) + find_package(Docker REQUIRED) + include(DockerImage) + set(DOCKER_FILE ${CMAKE_SOURCE_DIR}/pkg/docker/Dockerfile) + set(DOCKER_TAG ${CPACK_PACKAGE_NAME}:${REPOSITORY_VERSION_FULL}) + set(PACKAGE_FILE ${CPACK_PACKAGE_NAME}_${REPOSITORY_VERSION_FULL}_${CMAKE_SYSTEM_PROCESSOR}.deb) + set(DOCKER_IMAGE ${CPACK_PACKAGE_NAME}_${REPOSITORY_VERSION_FULL}_${CMAKE_SYSTEM_PROCESSOR}_ubuntu-20-04_docker.tar) + add_docker_image(docker-ubuntu-20-04 ${DOCKER_IMAGE} ${DOCKER_FILE} ${DOCKER_TAG} ${PACKAGE_FILE} ${CMAKE_BINARY_DIR}) + endif(NOT DNBD3_KERNEL_MODULE) endif(CMAKE_BUILD_TYPE MATCHES Release) # add all dnbd3 related projects from the source code directory diff --git a/README.md b/README.md index aae3426..41a80d6 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,34 @@ make source This target creates compressed archives (\*_source.tar.gz and \*_source.zip) containing the source code of this repository for code distribution purposes. +### Docker image +A docker image of the built dnbd3 components can be created in the `Release` build configuration with the option `DNBD3_KERNEL_MODULE=OFF`. + +Make sure that your docker daemon runs and you are a member of the `docker` group to access the docker deamon without any super user privileges. Then build the docker image by calling the following Make target: + +``` +make docker-ubuntu-20-04 +``` + +The built docker image is saved as archive file (\*_ubuntu-20-04_docker.tar) and can be deployed to other machines and can be loaded with the following docker client call: + +```shell +docker image load -i *_ubuntu-20-04_docker.tar +``` + +After the loading of an image, a docker container named `NAME` and with the IPv4 adrress `IPv4_ADDRESS` can be created with the following docker client call: + +``` +docker container create --name --ip +``` + +Note that the image is already tagged with a `IMAGE_TAG` that is set to the current dnbd3 package version number. The image tag `IMAGE_TAG` can be reused to create a container. Finally, this container can be started to execute the dnbd3-server in this container with the following docker client call: + +``` +docker container start -a +``` + + ## Configuration of _dnbd3-server_ The dnbd3-server is started according to the following command line call. diff --git a/cmake/DockerImage.cmake b/cmake/DockerImage.cmake new file mode 100644 index 0000000..54751e4 --- /dev/null +++ b/cmake/DockerImage.cmake @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2020 Manuel Bentele +# + +# macro to build a docker image based on a provided Dockerfile and an installation package +macro(add_docker_image TARGET_NAME DOCKER_IMAGE DOCKER_FILE DOCKER_TAG PACKAGE_FILE BUILD_DIR) + get_filename_component(PACKAGE_FILE_PATH ${PACKAGE_FILE} PATH) + get_filename_component(PACKAGE_FILE_NAME ${PACKAGE_FILE} NAME) + + # create a pseudo target to do packaging before docker image is built + add_custom_target(package_docker + COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target package + VERBATIM) + + # commands and target to build docker image + add_custom_command(OUTPUT ${DOCKER_IMAGE} + COMMAND docker image build -t ${DOCKER_TAG} --file ${DOCKER_FILE} --build-arg DNBD3_PACKAGE_FILE_NAME=${PACKAGE_FILE_NAME} ${BUILD_DIR} + COMMAND docker image save -o ${DOCKER_IMAGE} ${DOCKER_TAG} + COMMAND docker image rm ${DOCKER_TAG} + DEPENDS ${DOCKER_FILE} + package_docker) + add_custom_target(${TARGET_NAME} + DEPENDS ${DOCKER_IMAGE}) +endmacro(add_docker_image TARGET_NAME DOCKER_IMAGE DOCKER_FILE PACKAGE_FILE) diff --git a/cmake/FindDocker.cmake b/cmake/FindDocker.cmake new file mode 100644 index 0000000..ef3046d --- /dev/null +++ b/cmake/FindDocker.cmake @@ -0,0 +1,20 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2020 Manuel Bentele +# + +find_program(Docker_EXECUTABLE NAMES docker) + +if(Docker_EXECUTABLE) + execute_process(COMMAND docker version --format "{{.Server.Version}}" + OUTPUT_VARIABLE Docker_VERSION + OUTPUT_STRIP_TRAILING_WHITESPACE) +endif(Docker_EXECUTABLE) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Docker + FOUND_VAR Docker_FOUND + REQUIRED_VARS Docker_EXECUTABLE + VERSION_VAR Docker_VERSION + FAIL_MESSAGE "Docker is not available! Please install docker to build and run containers!") + diff --git a/pkg/docker/Dockerfile b/pkg/docker/Dockerfile new file mode 100644 index 0000000..ad2adcb --- /dev/null +++ b/pkg/docker/Dockerfile @@ -0,0 +1,28 @@ +# use Ubuntu 20.04 as base image +FROM ubuntu:focal + +# declare arguments that should be set by 'docker build --build-arg ...' +ARG DNBD3_PACKAGE_FILE_NAME + +# copy built package file from host to docker image +COPY ${DNBD3_PACKAGE_FILE_NAME} /tmp + +# install required dependencies +RUN apt-get update +RUN apt-get install -y libfuse2 libjansson4 + +# install installation package +RUN dpkg -i /tmp/${DNBD3_PACKAGE_FILE_NAME} + +# use default config for dnbd3-server +RUN ln -s /etc/dnbd3-server/sample/server.conf /etc/dnbd3-server +RUN ln -s /etc/dnbd3-server/sample/alt-servers /etc/dnbd3-server + +# make default storage point for dnbd3-server +RUN mkdir -p /mnt/storage + +# expose the port of the dnbd3-server to the host +EXPOSE 5003 + +# run dnbd3-server +CMD [ "dnbd3-server", "-n" ] -- cgit v1.2.3-55-g7522