From d95d0ef769527acc4d9aaffa446d943444d9a980 Mon Sep 17 00:00:00 2001 From: Matteo Carnelos Date: Tue, 1 Jun 2021 16:46:02 +0200 Subject: [PATCH 1/6] Add possibility to forward custom ports --- entrypoint.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 5822e8c..1c7b658 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -33,6 +33,7 @@ if [ "${target}" = "pi1" ]; then memory=256m root=/dev/sda2 nic="--net nic --net user,hostfwd=tcp::5022-:22" + for fwd in $HOSTFWD; do nic="$nic,hostfwd=$fwd"; done elif [ "${target}" = "pi2" ]; then emulator=qemu-system-arm machine=raspi2b @@ -40,7 +41,9 @@ elif [ "${target}" = "pi2" ]; then kernel_pattern=kernel7.img dtb_pattern=bcm2709-rpi-2-b.dtb append="dwc_otg.fiq_fsm_enable=0" - nic="-netdev user,id=net0,hostfwd=tcp::5022-:22 -device usb-net,netdev=net0" + nic="-netdev user,id=net0,hostfwd=tcp::5022-:22" + for fwd in $HOSTFWD; do nic="$nic,hostfwd=$fwd"; done + nic="$nic -device usb-net,netdev=net0" elif [ "${target}" = "pi3" ]; then emulator=qemu-system-aarch64 machine=raspi3b @@ -48,7 +51,9 @@ elif [ "${target}" = "pi3" ]; then kernel_pattern=kernel8.img dtb_pattern=bcm2710-rpi-3-b-plus.dtb append="dwc_otg.fiq_fsm_enable=0" - nic="-netdev user,id=net0,hostfwd=tcp::5022-:22 -device usb-net,netdev=net0" + nic="-netdev user,id=net0,hostfwd=tcp::5022-:22" + for fwd in $HOSTFWD; do nic="$nic,hostfwd=$fwd"; done + nic="$nic -device usb-net,netdev=net0" else echo "Target ${target} not supported" echo "Supported targets: pi1 pi2 pi3" From db4ca9287b28590d91a98e09d48459812f8acac5 Mon Sep 17 00:00:00 2001 From: Matteo Carnelos Date: Mon, 7 Jun 2021 00:41:11 +0200 Subject: [PATCH 2/6] Optimize code --- entrypoint.sh | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 1c7b658..ba26813 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -25,6 +25,8 @@ if [[ "$(($image_size_in_bytes % ($GIB_IN_BYTES * 2)))" != "0" ]]; then qemu-img resize $image_path "${new_size_in_gib}G" fi +for fwd in $HOSTFWD; do hostfwd="$hostfwd,hostfwd=$fwd"; done + if [ "${target}" = "pi1" ]; then emulator=qemu-system-arm kernel="/root/qemu-rpi-kernel/kernel-qemu-4.19.50-buster" @@ -32,8 +34,7 @@ if [ "${target}" = "pi1" ]; then machine=versatilepb memory=256m root=/dev/sda2 - nic="--net nic --net user,hostfwd=tcp::5022-:22" - for fwd in $HOSTFWD; do nic="$nic,hostfwd=$fwd"; done + nic="--net nic --net user$hostfwd" elif [ "${target}" = "pi2" ]; then emulator=qemu-system-arm machine=raspi2b @@ -41,9 +42,7 @@ elif [ "${target}" = "pi2" ]; then kernel_pattern=kernel7.img dtb_pattern=bcm2709-rpi-2-b.dtb append="dwc_otg.fiq_fsm_enable=0" - nic="-netdev user,id=net0,hostfwd=tcp::5022-:22" - for fwd in $HOSTFWD; do nic="$nic,hostfwd=$fwd"; done - nic="$nic -device usb-net,netdev=net0" + nic="-netdev user,id=net0$hostfwd -device usb-net,netdev=net0" elif [ "${target}" = "pi3" ]; then emulator=qemu-system-aarch64 machine=raspi3b @@ -51,9 +50,7 @@ elif [ "${target}" = "pi3" ]; then kernel_pattern=kernel8.img dtb_pattern=bcm2710-rpi-3-b-plus.dtb append="dwc_otg.fiq_fsm_enable=0" - nic="-netdev user,id=net0,hostfwd=tcp::5022-:22" - for fwd in $HOSTFWD; do nic="$nic,hostfwd=$fwd"; done - nic="$nic -device usb-net,netdev=net0" + nic="-netdev user,id=net0$hostfwd -device usb-net,netdev=net0" else echo "Target ${target} not supported" echo "Supported targets: pi1 pi2 pi3" From cc72636e54cd82c2546b3b9d33927258e4220f0e Mon Sep 17 00:00:00 2001 From: Matteo Carnelos Date: Mon, 7 Jun 2021 17:02:41 +0200 Subject: [PATCH 3/6] Add README section --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index 206ca44..a044c4e 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,27 @@ docker run -it lukechilds/dockerpi pi3 > **Note:** In the Pi 2 and Pi 3 machines, QEMU hangs once the machines are powered down requiring you to `docker kill` the container. See [#4](https://github.com/lukechilds/dockerpi/pull/4) for details. +## Port forwarding + +In some applications you may want to have access to some ports of the Raspberry Pi (e.g. for SSH connection). To do so, you can set the `HOSTFWD` enviroment variable of the container by adding one or more entries, separated by spaces, in the standard QEMU format (`protocol::hostip:hostport-guestip:guestport`). + +Example using the `docker run` command to expose the SSH port from the Raspberry Pi to the Container (`-e` part) and from the Container to the Host (`-p` part): + +``` +docker run -it -e HOSTFWD=tcp::5022-:22 -p 5022:5022 lukechilds/dockerpi +``` + +Example using the `docker-compose.yml` file to achieve the same result: + +```yml +services: + dockerpi: + image: lukechilds/dockerpi + environment: + - HOSTFWD=tcp::5022-:22 + ports: + - "5022:5022" +``` ## Wait, what? From b6f7707e56a55c3f86d1ffe55f76ed6665d78bd6 Mon Sep 17 00:00:00 2001 From: Matteo Carnelos Date: Fri, 3 Sep 2021 17:26:45 +0200 Subject: [PATCH 4/6] Rename HOSTFWD in QEMU_HOSTFWD --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index ba26813..d163720 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -25,7 +25,7 @@ if [[ "$(($image_size_in_bytes % ($GIB_IN_BYTES * 2)))" != "0" ]]; then qemu-img resize $image_path "${new_size_in_gib}G" fi -for fwd in $HOSTFWD; do hostfwd="$hostfwd,hostfwd=$fwd"; done +for fwd in $QEMU_HOSTFWD; do hostfwd="$hostfwd,hostfwd=$fwd"; done if [ "${target}" = "pi1" ]; then emulator=qemu-system-arm From ca71ed7a8be9f8b84c46e48442e7321b1438a3f1 Mon Sep 17 00:00:00 2001 From: Matteo Carnelos Date: Fri, 3 Sep 2021 17:29:31 +0200 Subject: [PATCH 5/6] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a044c4e..7593ddd 100644 --- a/README.md +++ b/README.md @@ -64,12 +64,12 @@ docker run -it lukechilds/dockerpi pi3 ## Port forwarding -In some applications you may want to have access to some ports of the Raspberry Pi (e.g. for SSH connection). To do so, you can set the `HOSTFWD` enviroment variable of the container by adding one or more entries, separated by spaces, in the standard QEMU format (`protocol::hostip:hostport-guestip:guestport`). +In some applications you may want to have access to some ports of the Raspberry Pi (e.g. for SSH connection). To do so, you can set the `QEMU_HOSTFWD` enviroment variable of the container by adding one or more entries, separated by spaces, in the standard QEMU format (`protocol::hostip:hostport-guestip:guestport`). Example using the `docker run` command to expose the SSH port from the Raspberry Pi to the Container (`-e` part) and from the Container to the Host (`-p` part): ``` -docker run -it -e HOSTFWD=tcp::5022-:22 -p 5022:5022 lukechilds/dockerpi +docker run -it -e QEMU_HOSTFWD=tcp::5022-:22 -p 5022:5022 lukechilds/dockerpi ``` Example using the `docker-compose.yml` file to achieve the same result: @@ -79,7 +79,7 @@ services: dockerpi: image: lukechilds/dockerpi environment: - - HOSTFWD=tcp::5022-:22 + - QEMU_HOSTFWD=tcp::5022-:22 ports: - "5022:5022" ``` From a67bc86987615556c045f06c30d6bfa62cb68cba Mon Sep 17 00:00:00 2001 From: Matteo Carnelos Date: Sat, 4 Sep 2021 19:22:58 +0200 Subject: [PATCH 6/6] Update README.md --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7593ddd..7cbf6ae 100644 --- a/README.md +++ b/README.md @@ -66,10 +66,10 @@ docker run -it lukechilds/dockerpi pi3 In some applications you may want to have access to some ports of the Raspberry Pi (e.g. for SSH connection). To do so, you can set the `QEMU_HOSTFWD` enviroment variable of the container by adding one or more entries, separated by spaces, in the standard QEMU format (`protocol::hostip:hostport-guestip:guestport`). -Example using the `docker run` command to expose the SSH port from the Raspberry Pi to the Container (`-e` part) and from the Container to the Host (`-p` part): +Example using the `docker run` command to expose the SSH and MQTT ports from the Raspberry Pi to the Container (`-e` part) and from the Container to the Host (`-p` part): ``` -docker run -it -e QEMU_HOSTFWD=tcp::5022-:22 -p 5022:5022 lukechilds/dockerpi +docker run -it -e QEMU_HOSTFWD="tcp::5022-:22 tcp::1883-:1883" -p 5022:5022 -p 1883:1883 lukechilds/dockerpi ``` Example using the `docker-compose.yml` file to achieve the same result: @@ -79,9 +79,10 @@ services: dockerpi: image: lukechilds/dockerpi environment: - - QEMU_HOSTFWD=tcp::5022-:22 + - QEMU_HOSTFWD=tcp::5022-:22 tcp::1883-:1883 ports: - "5022:5022" + - "1883:1883" ``` ## Wait, what?