[1/2] docker: Add DOCKER_FROM arg
diff mbox series

Message ID b89cd1d85f4a3f9e5661403b8a5580b1b97895ad.1526932581.git.geoff@infradead.org
State Accepted
Headers show
Series
  • [1/2] docker: Add DOCKER_FROM arg
Related show

Commit Message

Geoff Levand May 21, 2018, 7:59 p.m. UTC
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>
---
 ...Dockerfile.builder.amd64 => Dockerfile.builder} |  4 +++-
 docker/Dockerfile.builder.arm64                    | 24 --------------------
 docker/build-builder                               | 26 ++++++++++++++++++++--
 docker/builder-include                             | 13 +++++++++++
 4 files changed, 40 insertions(+), 27 deletions(-)
 rename docker/{Dockerfile.builder.amd64 => Dockerfile.builder} (89%)
 delete mode 100644 docker/Dockerfile.builder.arm64

Patch
diff mbox series

diff --git a/docker/Dockerfile.builder.amd64 b/docker/Dockerfile.builder
similarity index 89%
rename from docker/Dockerfile.builder.amd64
rename to docker/Dockerfile.builder
index dc0cce4..b4e3827 100644
--- a/docker/Dockerfile.builder.amd64
+++ b/docker/Dockerfile.builder
@@ -1,6 +1,8 @@ 
 # Image for compiling petitboot.
 
-FROM ubuntu:17.10
+ARG DOCKER_FROM
+
+FROM ${DOCKER_FROM}
 
 ENV LANG C.UTF-8
 ENV LC_ALL C.UTF-8
diff --git a/docker/Dockerfile.builder.arm64 b/docker/Dockerfile.builder.arm64
deleted file mode 100644
index d46581f..0000000
--- a/docker/Dockerfile.builder.arm64
+++ /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
diff --git a/docker/build-builder b/docker/build-builder
index 8a5015c..04603ad 100755
--- a/docker/build-builder
+++ b/docker/build-builder
@@ -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
diff --git a/docker/builder-include b/docker/builder-include
index 40e0120..87405b8 100644
--- a/docker/builder-include
+++ b/docker/builder-include
@@ -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="${*}"