From 0b504738e761a49bd06ef79c580cdda3f64126bb Mon Sep 17 00:00:00 2001
From: Jeffrey Phillips Freeman <jeff@scentech-medical.com>
Date: Sat, 28 Jan 2023 22:07:44 +0200
Subject: [PATCH] Added sudo escalation if user is not root

---
 30-add-host-ip-entry.sh | 10 ++++++++--
 Dockerfile              |  3 +--
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/30-add-host-ip-entry.sh b/30-add-host-ip-entry.sh
index 50dc6f2..b5b462e 100755
--- a/30-add-host-ip-entry.sh
+++ b/30-add-host-ip-entry.sh
@@ -1,3 +1,9 @@
 #!/bin/bash
-HOST_IP=$(/sbin/ip route|awk '/default/ { print $3 }')
-echo "$HOST_IP host.docker.internal" >> /etc/hosts
+if [ "$EUID" -ne 0 ]
+  then
+    HOST_IP=$(sudo /sbin/ip route|awk '/default/ { print $3 }')
+    echo "$HOST_IP host.docker.internal" >> /etc/hosts
+  else
+    HOST_IP=$(/sbin/ip route|awk '/default/ { print $3 }')
+    sudo -s "echo '$HOST_IP host.docker.internal' >> /etc/hosts"
+fi
diff --git a/Dockerfile b/Dockerfile
index 2f344a6..104e8ce 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -13,8 +13,7 @@ COPY 30-add-host-ip-entry.sh /docker-entrypoint.d/30-add-host-ip-entry.sh
 COPY docker-run.sh /docker-run.sh
 RUN mkdir -p /docker-run.d
 RUN chmod a+x /docker-entrypoint.sh && \
-    chmod a+x /docker-run.sh && \
-    chmod a+w /etc/hosts
+    chmod a+x /docker-run.sh
 
 ENTRYPOINT [ "/docker-entrypoint.sh" ]
 CMD [ "/docker-run.sh" ]
-- 
GitLab