From 915494557162c07104211ee204dfe8122d3eb3b9 Mon Sep 17 00:00:00 2001
From: eyedeekay <idki2p@i2pmail.org>
Date: Sun, 29 Sep 2024 16:58:44 -0400
Subject: [PATCH] Docker: automatically resolve conflicting port issues when
 the external port used by docker does not match the port in the routerInfo

---
 Docker.md                 | 17 +++++++++--------
 docker/rootfs/startapp.sh | 10 +++++++++-
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/Docker.md b/Docker.md
index f3e3769cde..4f8762982a 100644
--- a/Docker.md
+++ b/Docker.md
@@ -4,7 +4,8 @@
 If you just want to give I2P a quick try or are using it on a home network, follow these steps:
 
 1. Create two directories `i2pconfig` and `i2ptorrents`
-2. Copy the following text and save it in a file `docker-compose.yml`
+2. Create an `.env` file containing the `EXT_PORT` environment variable.
+3. Copy the following text and save it in a file `docker-compose.yml`
 ```
 version: "3.5"
 services:
@@ -14,14 +15,14 @@ services:
             - 127.0.0.1:4444:4444
             - 127.0.0.1:6668:6668
             - 127.0.0.1:7657:7657
-            - 54321:12345
-            - 54321:12345/udp
+            - "$EXT_PORT":"$EXT_PORT"
+            - "$EXT_PORT":"$EXT_PORT"/udp
         volumes:
             - ./i2pconfig:/i2p/.i2p
             - ./i2ptorrents:/i2psnark
 ```
-3. Execute `docker-compose up`
-4. Start a browser and go to `http://127.0.0.1:7657` to complete the setup wizard.
+4. Execute `docker-compose up`
+5. Start a browser and go to `http://127.0.0.1:7657` to complete the setup wizard.
 
 Note that this quick-start approach is not recommended for production deployments on remote servers.  Please read the rest of this document for more information.
 
@@ -81,13 +82,13 @@ docker build -t geti2p/i2p .
 # I2NP port needs TCP and UDP.  Change the 54321 to something random, greater than 1024.
 docker run \
     -e JVM_XMX=256m \
+    -e EXT_PORT=54321 \
     -v i2phome:/i2p/.i2p \
     -v i2ptorrents:/i2psnark \
     -p 127.0.0.1:4444:4444 \
     -p 127.0.0.1:6668:6668 \
     -p 127.0.0.1:7657:7657 \
-    -p 54321:12345 \
-    -p 54321:12345/udp \
+    -p "$EXT_PORT":"$EXT_PORT" \
+    -p "$EXT_PORT":"$EXT_PORT"/udp \
     geti2p/i2p:latest
 ```
-
diff --git a/docker/rootfs/startapp.sh b/docker/rootfs/startapp.sh
index 8030d514ed..0af289d0c1 100644
--- a/docker/rootfs/startapp.sh
+++ b/docker/rootfs/startapp.sh
@@ -8,6 +8,14 @@ if [ -z $JVM_XMX ]; then
     JVM_XMX=512m
 fi
 
+if [ -z $EXT_PORT ]; then
+    echo "*** EXT_PORT is unset."
+    echo "*** I2P router will resolve to a \"Firewalled\" state"
+    echo "*** please configure EXT_PORT in your docker-compose.yaml or docker run command"
+else
+    find . -name 'router.config' -exec sed -i "s|12345|$EXT_PORT|g" {} \;
+fi
+
 # Explicitly define HOME otherwise it might not have been set
 export HOME=/i2p
 
@@ -31,7 +39,7 @@ if [ -f /.dockerenv ] || [ -f /run/.containerenv ]; then
     echo "[startapp] setting reachable IP to container IP $IP_ADDR"
     find . -name '*.config' -exec sed -i "s/127.0.0.1/$IP_ADDR/g" {} \;
     find . -name '*.config' -exec sed -i "s/localhost/$IP_ADDR/g" {} \;
-    
+
 fi
 
 # Options required for reflective access in dynamic JVM languages like Groovy and Jython
-- 
GitLab