Use a Docker image to build an RPM package

Source: Internet
Author: User

Use a Docker image to build an RPM package

Editor's note: RPM (Red Hat Package Manager) is the most common software Package Manager for Linux distribution. Because it allows distribution of compiled software, you can install the software with only one command. The RPM package is cumbersome to build and has high requirements on the environment. This article describes how to use Docker to build an RPM package that can be applied to multiple platforms.

In an internal project, I have been thinking about how to generate RPM packages through non-CI tools/processes. I want to manually generate RPM packages so that I can test whether they can be installed properly, and used for normal Smoke Testing (Note: Smoke Testing is a simple test of the basic functions of the system after the daily build is completed. This test emphasizes the coverage rate of the function, rather than verifying the correctness of the function ).

Docker is an all-powerful player in our CI process, so I am also wondering if I can combine Docker images with RPM. Ideally, integrate RPM with Docker. In this way, the process of creating an RPM package is actually building a Docker image. Basically, the construction of the % prep part of the RPM package can be quickly completed in a special Docker image, and then the generated RPM package is returned to the host.

The advantage of this method is that your RPM package is built in a relatively closed and renewable environment, therefore, you can quickly build RPM packages for other systems such as CentOS, Fedora, and RHEL.

I believe there are other workways to do this, such as chroot. But I think it would be better if this packaging mechanism is built in RPM (through the system abstracted by chroot/Docker or other container technology.

Since my project is not completed yet, I just verified my idea: simply build an image containing dependencies.

This is a Python project that uses PBR to generate a version. First, I generate a tarball In the build directory, then get the generated version number, then modify the version number in the spec file, and then start to build the image with the new tar package and spec file. Finally, run the image and mount the Volume (Volume) to the local directory. After running the start. sh script in the container, the image runs.

Start. sh is quite simple. After the RPM package is built, it copies it to the volume directory as root, and can also copy it to the output directory from the host. I didn't copy or update it to an object storage system like swift, because I still need to use it in CI, so I used local file copy.

In SPECS/project. spec and SOURCES/* Are spec files, source files, and patch files required by the standard RPM package. The only thing that needs to be done is to define the % define_version macro and use it in the spec file. Below are some of my scripts.

Main script build. sh. It can be run from CI.

#!/bin/bashset -exfPROJECT=myprojectCURDIR=$(dirname $(readlink -f $0))TOPDIR=$(git rev-parse --show-topklevel 2>/dev/null)rm -rf ${CURDIR}/.build/rpmmkdir -p ${CURDIR}/.build/rpm/{BUILD,SRPMS,SPECS,RPMS/noarch}cp -r ${CURDIR}/SOURCES ${CURDIR}/.build/rpmpushd ${TOPDIR} >/dev/nullpython setup.py sdist --dist-dir ${CURDIR}/.build/rpm/SOURCES/SALADIER_VERSION=$(sed -n '/^Version/ { s/.* //; p}' ${PROJECT}.egg-info/PKG-INFO)popd >/dev/nullsed -e "s/%define _version.*/%define _version ${SALADIER_VERSION}/" ${CURDIR}/SPECS/${MYROJECT}.spec > \        ${CURDIR}/.build/rpm/SPECS/${MYPROJECT}.specdocker build -t chmouel/buildrpm ${CURDIR}docker run -v $CURDIR/.build:/data -it chmouel/buildrpmif [[ -n ${ARTIFACT_DIR} ]];then    rm -rf ${ARTIFACT_DIR}/rpm    cp -a ${CURDIR}/.build/output ${ARTIFACT_DIR}/rpmfi


DockerFile optimizes Docker cache:

FROM fedora:21MAINTAINER Chmouel Boudjnah <chmouel@enovance.com>RUN yum -y groupinstall 'Development Tools'RUN yum -y install fedora-packagerRUN yum -y install yum-utilsRUN yum -y install sudoRUN sed -i.bak -n -e '/^Defaults.*requiretty/ { s/^/# /;};/^%wheel.*ALL$/ { s/^/# / ;} ;/^#.*wheel.*NOPASSWD/ { s/^#[ ]*//;};p' /etc/sudoersRUN yum install -y https://rdo.fedorapeople.org/rdo-release.rpm# This is an optimisation for caching, since using the auto generated one will# make docker always run the builddep steps since new fileADD SPECS/project.spec /tmp/RUN yum-builddep -y /tmp/project.specADD bin/start.sh /start.shRUN useradd -s /bin/bash -G adm,wheel,systemd-journal -m rpmWORKDIR /home/rpmCMD /start.shADD .build/rpm/ /home/rpm/rpmbuild/RUN chown -R rpm: /home/rpmUSER rpm


And the start. sh script running from the container:

#!/bin/bash# script run inside the containerrpmbuild -ba rpmbuild/SPECS/project.spec || exit 1[[ -d /data ]] || exit 0sudo rm -rf /data/outputsudo cp -a rpmbuild/RPMS/noarch /data/output


Scripts may not be directly used in your environment, but at least you can understand this idea.

Create your own rpm package

Directory structure and configuration after rpm installation in Linux

Brief Introduction and demo of rpm and yum

RedHat Linux --- rpm command details

Use FPM to easily create an RPM package

Docker details: click here
Docker: click here

Http://blog.chmouel.com/2014/12/31/building-rpm-with-docker-images/ Author: chmouel
He linchong, http://dockerone.com/article/138## Translator

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.