├── Dockerfile-Debian-bookworm-12 ├── Dockerfile-Debian-bullseye-11 ├── Dockerfile-Debian-buster-10 ├── Dockerfile-Debian-sid-unstable ├── Dockerfile-Debian-stretch-9 ├── Dockerfile-ubuntu-17.04 ├── Dockerfile-ubuntu-18.04 ├── Dockerfile-ubuntu-19.10 ├── Dockerfile-ubuntu-20.04 ├── Dockerfile-ubuntu-22.04 ├── LICENSE ├── README.md ├── build └── build-helper.sh /Dockerfile-Debian-bookworm-12: -------------------------------------------------------------------------------- 1 | FROM debian:bookworm 2 | 3 | RUN set -ex \ 4 | && sed -i -- 's/Types: deb/Types: deb deb-src/g' /etc/apt/sources.list.d/debian.sources \ 5 | && apt-get update \ 6 | && apt-get install -y --no-install-recommends \ 7 | build-essential \ 8 | cdbs \ 9 | devscripts \ 10 | equivs \ 11 | fakeroot \ 12 | && apt-get clean \ 13 | && rm -rf /tmp/* /var/tmp/* 14 | -------------------------------------------------------------------------------- /Dockerfile-Debian-bullseye-11: -------------------------------------------------------------------------------- 1 | FROM debian:bullseye 2 | 3 | RUN set -ex \ 4 | && sed -i -- 's/# deb-src/deb-src/g' /etc/apt/sources.list \ 5 | && apt-get update \ 6 | && apt-get install -y --no-install-recommends \ 7 | build-essential \ 8 | cdbs \ 9 | devscripts \ 10 | equivs \ 11 | fakeroot \ 12 | && apt-get clean \ 13 | && rm -rf /tmp/* /var/tmp/* 14 | -------------------------------------------------------------------------------- /Dockerfile-Debian-buster-10: -------------------------------------------------------------------------------- 1 | FROM debian:buster 2 | 3 | RUN set -ex \ 4 | && sed -i -- 's/# deb-src/deb-src/g' /etc/apt/sources.list \ 5 | && apt-get update \ 6 | && apt-get install -y --no-install-recommends \ 7 | build-essential \ 8 | cdbs \ 9 | devscripts \ 10 | equivs \ 11 | fakeroot \ 12 | && apt-get clean \ 13 | && rm -rf /tmp/* /var/tmp/* 14 | -------------------------------------------------------------------------------- /Dockerfile-Debian-sid-unstable: -------------------------------------------------------------------------------- 1 | FROM debian:sid 2 | 3 | RUN set -ex \ 4 | && sed -i -- 's/Types: deb/Types: deb deb-src/g' /etc/apt/sources.list.d/debian.sources \ 5 | && apt-get update \ 6 | && apt-get install -y --no-install-recommends \ 7 | build-essential \ 8 | cdbs \ 9 | devscripts \ 10 | equivs \ 11 | fakeroot \ 12 | && apt-get clean \ 13 | && rm -rf /tmp/* /var/tmp/* 14 | -------------------------------------------------------------------------------- /Dockerfile-Debian-stretch-9: -------------------------------------------------------------------------------- 1 | FROM debian:stretch 2 | 3 | RUN set -ex \ 4 | && sed -i -- 's/# deb-src/deb-src/g' /etc/apt/sources.list \ 5 | && apt-get update \ 6 | && apt-get install -y --no-install-recommends \ 7 | build-essential \ 8 | cdbs \ 9 | devscripts \ 10 | equivs \ 11 | fakeroot \ 12 | && apt-get clean \ 13 | && rm -rf /tmp/* /var/tmp/* 14 | -------------------------------------------------------------------------------- /Dockerfile-ubuntu-17.04: -------------------------------------------------------------------------------- 1 | FROM ubuntu:17.04 2 | 3 | RUN set -ex \ 4 | && sed -i -- 's/# deb-src/deb-src/g' /etc/apt/sources.list \ 5 | && apt-get update \ 6 | && apt-get install -y --no-install-recommends \ 7 | build-essential \ 8 | cdbs \ 9 | devscripts \ 10 | equivs \ 11 | fakeroot \ 12 | && apt-get clean \ 13 | && rm -rf /tmp/* /var/tmp/* 14 | -------------------------------------------------------------------------------- /Dockerfile-ubuntu-18.04: -------------------------------------------------------------------------------- 1 | FROM ubuntu:18.04 2 | 3 | RUN set -ex \ 4 | && sed -i -- 's/# deb-src/deb-src/g' /etc/apt/sources.list \ 5 | && apt-get update \ 6 | && apt-get install -y --no-install-recommends \ 7 | build-essential \ 8 | cdbs \ 9 | devscripts \ 10 | equivs \ 11 | fakeroot \ 12 | && apt-get clean \ 13 | && rm -rf /tmp/* /var/tmp/* 14 | -------------------------------------------------------------------------------- /Dockerfile-ubuntu-19.10: -------------------------------------------------------------------------------- 1 | FROM ubuntu:19.10 2 | 3 | RUN set -ex \ 4 | && sed -i -- 's/# deb-src/deb-src/g' /etc/apt/sources.list \ 5 | && apt-get update \ 6 | && apt-get install -y --no-install-recommends \ 7 | build-essential \ 8 | cdbs \ 9 | devscripts \ 10 | equivs \ 11 | fakeroot \ 12 | && apt-get clean \ 13 | && rm -rf /tmp/* /var/tmp/* 14 | -------------------------------------------------------------------------------- /Dockerfile-ubuntu-20.04: -------------------------------------------------------------------------------- 1 | FROM ubuntu:20.04 2 | 3 | ARG DEBIAN_FRONTEND=noninteractive 4 | 5 | RUN set -ex \ 6 | && sed -i -- 's/# deb-src/deb-src/g' /etc/apt/sources.list \ 7 | && apt-get update \ 8 | && apt-get install -y --no-install-recommends \ 9 | build-essential \ 10 | cdbs \ 11 | devscripts \ 12 | equivs \ 13 | fakeroot \ 14 | && apt-get clean \ 15 | && rm -rf /tmp/* /var/tmp/* 16 | -------------------------------------------------------------------------------- /Dockerfile-ubuntu-22.04: -------------------------------------------------------------------------------- 1 | FROM ubuntu:22.04 2 | 3 | ARG DEBIAN_FRONTEND=noninteractive 4 | 5 | RUN set -ex \ 6 | && sed -i -- 's/# deb-src/deb-src/g' /etc/apt/sources.list \ 7 | && apt-get update \ 8 | && apt-get install -y --no-install-recommends \ 9 | build-essential \ 10 | cdbs \ 11 | devscripts \ 12 | equivs \ 13 | fakeroot \ 14 | && apt-get clean \ 15 | && rm -rf /tmp/* /var/tmp/* 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Tero Saarni 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Creating Debian packages in Docker container 2 | 3 | ## Overview 4 | 5 | Docker can be used to set up a clean build environment for Debian 6 | packaging. This tutorial shows how to create a container with 7 | required build tools and how to use it to build packages. 8 | 9 | For more complete solution with many improvements see 10 | [container-deb-builder](https://github.com/cgzones/container-deb-builder/). 11 | 12 | You might also be interested in [debcraft](https://salsa.debian.org/otto/debcraft), which offers an easy, fast, and secure way to build Debian packages. 13 | 14 | ## Create build environment 15 | 16 | Start by building a container that will act as package build environment: 17 | 18 | docker build -t docker-deb-builder:17.04 -f Dockerfile-ubuntu-17.04 . 19 | 20 | In this example the target is Ubuntu 17.04 but you can create and 21 | modify `Dockerfile-nnn` to match your target environment. 22 | 23 | ## Building packages 24 | 25 | First download or git clone the source code of the package you are 26 | building: 27 | 28 | git clone ... ~/my-package-source 29 | 30 | The source code should contain subdirectory called `debian` with at 31 | least a minimum set of packaging files: `control`, `copyright`, 32 | `changelog` and `rules`. 33 | 34 | Clone the 35 | [docker-deb-builder](https://github.com/tsaarni/docker-deb-builder) 36 | (the repository you are reading now) and run the build script to see 37 | usage: 38 | 39 | $ ./build 40 | usage: build [options...] SOURCEDIR 41 | Options: 42 | -i IMAGE Name of the docker image (including tag) to use as package build environment. 43 | -o DIR Destination directory to store packages to. 44 | -d DIR Directory that contains other deb packages that need to be installed before build. 45 | 46 | To build Debian packages run following commands: 47 | 48 | # create destination directory to store the build results 49 | mkdir output 50 | 51 | # build package from source directory 52 | ./build -i docker-deb-builder:17.04 -o output ~/my-package-source 53 | 54 | After successful build you will find the `.deb` files in `output` 55 | directory. 56 | 57 | Sometimes build might require dependencies that cannot be installed with 58 | `apt-get build-dep`. You can install them into the build environment 59 | by passing option `-d DIR` where DIR is a directory with `*.deb` files 60 | in it. 61 | 62 | ./build -i docker-deb-builder:17.04 -o output -d dependencies ~/my-package-source 63 | 64 | ## Integrating with CI 65 | 66 | In this tutorial all package-specific build dependencies are installed 67 | from scratch each time build is executed in the container. The 68 | benefit is that the container is generic and reusable for building any 69 | package but the installation of build-time dependencies can add up to 70 | considerable overhead, both in time and bandwidth. This overhead may 71 | not be acceptable when building packages as part of continuous 72 | integration pipeline. One possible solution to reduce overhead is to 73 | install package-specific build dependencies into build environment 74 | container. 75 | -------------------------------------------------------------------------------- /build: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | PROG=$(basename $0) 4 | PROG_DIR=$(dirname $0) 5 | 6 | function usage { 7 | cat <&2 8 | usage: $PROG [options...] SOURCEDIR 9 | Options: 10 | -i IMAGE Name of the docker image (including tag) to use as package build environment. 11 | -o DIR Destination directory to store packages to. 12 | -d DIR Directory that contains other deb packages that need to be installed before build. 13 | EOF 14 | exit 1 15 | } 16 | 17 | function fatal { 18 | echo "$PROG: ${1:-"Unknown Error"}" 1>&2 19 | exit 1 20 | } 21 | 22 | function abspath { 23 | echo $(cd "$1" && pwd) 24 | } 25 | 26 | 27 | ########################################################################### 28 | 29 | [[ $# -eq 0 ]] && usage 30 | 31 | while getopts "i:o:d:h" opt; do 32 | case $opt in 33 | i) 34 | image="$OPTARG" 35 | ;; 36 | o) 37 | outdir="$OPTARG" 38 | ;; 39 | d) 40 | depdir="$OPTARG" 41 | ;; 42 | h) 43 | usage 44 | ;; 45 | *) 46 | usage 47 | ;; 48 | esac 49 | done 50 | 51 | shift $(($OPTIND - 1)) 52 | srcdir=$1 53 | docker_args="-it " 54 | 55 | # Check that mandatory parameters are valid 56 | [[ ! "$outdir" ]] && fatal "output directory was not given (-o DIR)" 57 | [[ ! -d "$outdir" ]] && fatal "output directory does not exist: $outdir" 58 | [[ ! "$srcdir" ]] && fatal "source directory not given" 59 | [[ ! -r "$srcdir/debian" ]] && fatal "source direcotry does not contain debian sub directory" 60 | [[ ! "$image" ]] && fatal "docker image name not given (-i IMAGE)" 61 | 62 | # Check that optional parameters are valid 63 | if [[ "$depdir" ]]; then 64 | [[ ! -d "$depdir" ]] && fatal "dependency directory given but does not exist: $depdir" 65 | docker_args+="-v $(abspath "$depdir"):/dependencies:ro " 66 | fi 67 | 68 | docker_args+="-v $(abspath "$srcdir"):/source-ro:ro -v $(abspath "$outdir"):/output -v $(cd $PROG_DIR; pwd)/build-helper.sh:/build-helper.sh:ro " 69 | 70 | # Pass current UID and GID to container, so that it can change the 71 | # ownership of output files which are otherwise writen to outdir as 72 | # root 73 | docker_args+="-e USER=$(id -u) -e GROUP=$(id -g) " 74 | 75 | # Comment following out if you want to keep container after execution 76 | # for debugging 77 | docker_args+="--rm " 78 | 79 | # disable any selinux stuff while using rh and derivates with podman 80 | [[ "$( docker --version )" =~ "podman" ]] && docker_args+="--security-opt label=disable " 81 | 82 | cmd="docker run -it $docker_args $image /build-helper.sh" 83 | 84 | echo "Running docker:" 85 | echo "$cmd" 86 | 87 | exec $cmd 88 | -------------------------------------------------------------------------------- /build-helper.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | # This script is executed within the container as root. It assumes 4 | # that source code with debian packaging files can be found at 5 | # /source-ro and that resulting packages are written to /output after 6 | # succesful build. These directories are mounted as docker volumes to 7 | # allow files to be exchanged between the host and the container. 8 | 9 | # Install extra dependencies that were provided for the build (if any) 10 | # Note: dpkg can fail due to dependencies, ignore errors, and use 11 | # apt-get to install those afterwards 12 | [[ -d /dependencies ]] && dpkg -i /dependencies/*.deb || apt-get -f install -y --no-install-recommends 13 | 14 | # Make read-write copy of source code 15 | mkdir -p /build 16 | cp -a /source-ro /build/source 17 | cd /build/source 18 | 19 | # Install build dependencies 20 | mk-build-deps -ir -t "apt-get -o Debug::pkgProblemResolver=yes -y --no-install-recommends" 21 | 22 | # Build packages 23 | debuild -b -uc -us 24 | 25 | # Copy packages to output dir with user's permissions 26 | chown -R $USER:$GROUP /build 27 | cp -a /build/*.deb /build/*.buildinfo /build/*.changes /output/ 28 | ls -l /output 29 | --------------------------------------------------------------------------------