Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to forward custom ports #32

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ 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 `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 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 tcp::1883-:1883" -p 5022:5022 -p 1883:1883 lukechilds/dockerpi
```

Example using the `docker-compose.yml` file to achieve the same result:

```yml
services:
dockerpi:
image: lukechilds/dockerpi
environment:
- QEMU_HOSTFWD=tcp::5022-:22 tcp::1883-:1883
ports:
- "5022:5022"
- "1883:1883"
```

## Wait, what?

Expand Down
8 changes: 5 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,32 @@ 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 $QEMU_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"
dtb="/root/qemu-rpi-kernel/versatile-pb.dtb"
machine=versatilepb
memory=256m
root=/dev/sda2
nic="--net nic --net user,hostfwd=tcp::5022-:22"
nic="--net nic --net user$hostfwd"
elif [ "${target}" = "pi2" ]; then
emulator=qemu-system-arm
machine=raspi2b
memory=1024m
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 -device usb-net,netdev=net0"
elif [ "${target}" = "pi3" ]; then
emulator=qemu-system-aarch64
machine=raspi3b
memory=1024m
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 -device usb-net,netdev=net0"
else
echo "Target ${target} not supported"
echo "Supported targets: pi1 pi2 pi3"
Expand Down