docker: Add DOCKER_FROM arg
authorGeoff Levand <geoff@infradead.org>
Mon, 21 May 2018 19:59:39 +0000 (19:59 +0000)
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>
Wed, 23 May 2018 01:11:36 +0000 (11:11 +1000)
The dockerfile for each architecture is the same except for the 'FROM' image.
Add a new Dockerfile argument DOCKER_FROM that allows for a commom dockerfile.
If the docker version is older than 17.05 generate a docker file from
the common one.

Signed-off-by: Geoff Levand <geoff@infradead.org>
Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
docker/Dockerfile.builder [new file with mode: 0644]
docker/Dockerfile.builder.amd64 [deleted file]
docker/Dockerfile.builder.arm64 [deleted file]
docker/build-builder
docker/builder-include

diff --git a/docker/Dockerfile.builder b/docker/Dockerfile.builder
new file mode 100644 (file)
index 0000000..b4e3827
--- /dev/null
@@ -0,0 +1,26 @@
+# Image for compiling petitboot.
+
+ARG DOCKER_FROM
+
+FROM ${DOCKER_FROM}
+
+ENV LANG C.UTF-8
+ENV LC_ALL C.UTF-8
+
+RUN apt-get update && apt-get install -y \
+       apt-utils \
+       autoconf \
+       autopoint \
+       bison \
+       flex \
+       gettext \
+       gcc \
+       git \
+       libtool \
+       libuv-dev \
+       libdevmapper-dev \
+       libncurses-dev \
+       pkg-config \
+       && rm -rf /var/lib/apt/lists/*
+
+CMD /bin/bash
diff --git a/docker/Dockerfile.builder.amd64 b/docker/Dockerfile.builder.amd64
deleted file mode 100644 (file)
index dc0cce4..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-# Image for compiling petitboot.
-
-FROM ubuntu:17.10
-
-ENV LANG C.UTF-8
-ENV LC_ALL C.UTF-8
-
-RUN apt-get update && apt-get install -y \
-       apt-utils \
-       autoconf \
-       autopoint \
-       bison \
-       flex \
-       gettext \
-       gcc \
-       git \
-       libtool \
-       libuv-dev \
-       libdevmapper-dev \
-       libncurses-dev \
-       pkg-config \
-       && rm -rf /var/lib/apt/lists/*
-
-CMD /bin/bash
diff --git a/docker/Dockerfile.builder.arm64 b/docker/Dockerfile.builder.arm64
deleted file mode 100644 (file)
index d46581f..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-# Image for compiling petitboot.
-
-FROM arm64v8/ubuntu:17.10
-
-ENV LANG C.UTF-8
-ENV LC_ALL C.UTF-8
-
-RUN apt-get update && apt-get install -y \
-       apt-utils \
-       autoconf \
-       autopoint \
-       bison \
-       flex \
-       gettext \
-       gcc \
-       git \
-       libtool \
-       libuv-dev \
-       libdevmapper-dev \
-       libncurses-dev \
-       pkg-config \
-       && rm -rf /var/lib/apt/lists/*
-
-CMD /bin/bash
index 8a5015c80e3f406871f0e8fa6eb24f0dc1d12dc8..04603ad9be0459feef4923b821261608d2f66271 100755 (executable)
@@ -8,7 +8,8 @@ name="$(basename $0)"
 
 source ${TOP_DIR}/docker/builder-include
 
-: ${DOCKER_FILE:="${TOP_DIR}/docker/Dockerfile.builder.$(arch)"}
+: ${DOCKER_FILE:="${TOP_DIR}/docker/Dockerfile.builder"}
+: ${DOCKER_FROM:="$(docker_from)"}
 
 usage () {
        echo "${name} - Builds a docker image that contains tools for building petitboot." >&2
@@ -22,6 +23,7 @@ usage () {
        echo "  -v --verbose  - Verbose execution." >&2
        echo "Environment:" >&2
        echo "  DOCKER_FILE   - Default: '${DOCKER_FILE}'" >&2
+       echo "  DOCKER_FROM   - Default: '${DOCKER_FROM}'" >&2
        echo "  DOCKER_TAG    - Default: '${DOCKER_TAG}'" >&2
        echo "Examples:" >&2
        echo "  ${name} -v"
@@ -87,6 +89,22 @@ if [[ -n "${tag}" ]]; then
        exit 0
 fi
 
+version () {
+       echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'
+}
+
+# Support for docker versions older than 17.05.
+# See https://github.com/moby/moby/issues/32457
+if [[ $(version "$(docker version --format '{{.Server.Version}}')") < $(version "17.05") ]]; then
+       tmp_file="${DOCKER_FILE}.tmp"
+       trap "rm -f ${tmp_file}" EXIT
+
+       cp -f ${DOCKER_FILE} ${tmp_file}
+       DOCKER_FILE=${tmp_file}
+       sed --in-place "s|ARG DOCKER_FROM||" ${tmp_file}
+       sed --in-place "s|\${DOCKER_FROM}|${DOCKER_FROM}|" ${tmp_file}
+fi
+
 if [[ -n "${purge}" ]] && docker inspect --type image ${DOCKER_TAG} >/dev/null 2>/dev/null; then
        echo "Removing docker image: ${DOCKER_TAG}" >&2
        run_cmd "docker rmi --force ${DOCKER_TAG}"
@@ -99,6 +117,10 @@ fi
 echo "Building docker image: ${DOCKER_TAG}" >&2
 
 cd ${TOP_DIR}
-run_cmd "docker build --file ${DOCKER_FILE} --tag ${DOCKER_TAG} ."
+run_cmd "docker build \
+       --file "${DOCKER_FILE}" \
+       --build-arg DOCKER_FROM=${DOCKER_FROM} \
+       --tag ${DOCKER_TAG} \
+       ."
 
 show_tag
index 40e0120ca9b1017ade31354fa4f7485fdf63b44f..87405b8ed5772dcbdf328afb1c675a90a83843f2 100644 (file)
@@ -19,6 +19,19 @@ arch_tag() {
        esac
 }
 
+docker_from() {
+       local a="$(arch)"
+
+       case "${a}" in
+               amd64) echo "ubuntu:17.10" ;;
+               arm64) echo "arm64v8/ubuntu:17.10" ;;
+               *)
+                       echo "${name}: ERROR: Unknown arch ${a}" >&2
+                       exit 1
+               ;;
+       esac
+}
+
 run_cmd () {
        local cmd="${*}"