Making docker.host.internal resolvable in Docker for Linux

Change-Id: Icc74d1aeccf44f68b785b786cae8eafe65700e66
diff --git a/Dockerfile b/Dockerfile
index 92fc209..6a93503 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -15,7 +15,9 @@
     bzip2 \
     build-essential \
     curl \
-    unzip
+    unzip \
+    iproute2
+
 RUN apt-get update && apt-get install -y ${BUILD_DEPS}
 
 # Install Bazel
@@ -65,6 +67,13 @@
 COPY --from=builder /output/ /root/onos/
 WORKDIR /root/onos
 
+# Add ip command
+COPY --from=builder /bin/ip /bin/
+COPY --from=builder /lib/x86_64-linux-gnu/libmnl.so* /lib/x86_64-linux-gnu/
+COPY --from=builder /usr/lib/x86_64-linux-gnu/libelf.so* /lib/x86_64-linux-gnu/
+COPY --from=builder /sbin/ip /sbin/
+RUN ldconfig
+
 # Set JAVA_HOME (by default not exported by zulu images)
 ARG JDK_VER
 ENV JAVA_HOME /usr/lib/jvm/zulu-${JDK_VER}-amd64
diff --git a/tools/package/bin/onos-service b/tools/package/bin/onos-service
index d2879ef..fe4e248 100755
--- a/tools/package/bin/onos-service
+++ b/tools/package/bin/onos-service
@@ -15,6 +15,18 @@
 set -e  # exit on error
 set -u  # exit on undefined variable
 
+#
+# Hack: Making docker.host.internal resolvable in Docker for Linux
+# TODO(bocon): this can be removed when this issue is resolved: https://github.com/docker/for-linux/issues/264
+#              iproute2 can also be removed from the Dockerfile as a build dep
+#
+# Only run this if we are running in a Docker container
+if awk -F/ '$2 == "docker"' /proc/self/cgroup &>/dev/null; then
+  # Add host.docker.internal to /etc/hosts if it does not resolve
+  getent hosts host.docker.internal > /dev/null || \
+  ip -4 route list match 0/0 | awk '{print $3"\thost.docker.internal"}' >> /etc/hosts
+fi
+
 # If ONOS_HOME is set, respect its value.
 # If ONOS_HOME is not set (e.g. in the init or service environment),
 # set it based on this script's path.