Docker udp communication

Hi,

I’m trying to develop a Lora gateway based on my Mallow board AM62.

A semtech LR1302 pcie board is connected to my spi , i2c and gpio SoM.

I’m able to run the first PHY piece of software names Semtech Lora Packet Forwarder.

It just send via UDP on port 1700 the result of radio scan. Because the advised way of working and the gpioset command are not in the basic images of toradex for my board, i’m running this paquet forwarder into a container. It works fine and i’m able to see udp crypted data.

This crypted data are send to the chirpstack-gateway-bridge that run into another container. This chirpstack-gateway-bridge listen on port 1700 to be able to receive data from the packet forwarder.

Both container are started by a docker-compose.yml file.

My problem is that as the two software are in two separate container, i can’t use ports: -1700:1700/udp for both because docker complain that the port is already in use.

Bind for 0.0.0.0:1700 failed: port is already allocated

How can I established the udp communication between this two container ?

Thank you.

Here is my docker-compose.yml file:

version: “3”

services:

chirpstack:
image: chirpstack/chirpstack:4
command: -c /etc/chirpstack
restart: unless-stopped
volumes:
- ./configuration/chirpstack:/etc/chirpstack
- ./lorawan-devices:/opt/lorawan-devices
depends_on:
- postgres
- mosquitto
- redis
environment:
- MQTT_BROKER_HOST=mosquitto
- REDIS_HOST=redis
- POSTGRESQL_HOST=postgres
ports:
- 8080:8080

chirpstack-gateway-bridge:
image: chirpstack/chirpstack-gateway-bridge:4
restart: unless-stopped
volumes:
- ./configuration/chirpstack-gateway-bridge:/etc/chirpstack-gateway-bridge
environment:
- INTEGRATION__MQTT__EVENT_TOPIC_TEMPLATE=eu868/gateway/{{ .GatewayID }}/event/{{ .EventType }}
- INTEGRATION__MQTT__STATE_TOPIC_TEMPLATE=eu868/gateway/{{ .GatewayID }}/state/{{ .StateType }}
- INTEGRATION__MQTT__COMMAND_TOPIC_TEMPLATE=eu868/gateway/{{ .GatewayID }}/command/#
depends_on:
- mosquitto
ports:
- 1700:1700/udp

chirpstack-gateway-bridge-basicstation:
image: chirpstack/chirpstack-gateway-bridge:4
restart: unless-stopped
command: -c /etc/chirpstack-gateway-bridge/chirpstack-gateway-bridge-basicstation-eu868.toml
ports:
- 3001:3001
volumes:
- ./configuration/chirpstack-gateway-bridge:/etc/chirpstack-gateway-bridge
depends_on:
- mosquitto

chirpstack-rest-api:
image: chirpstack/chirpstack-rest-api:4
restart: unless-stopped
command: --server chirpstack:8080 --bind 0.0.0.0:8090 --insecure
ports:
- 8090:8090
depends_on:
- chirpstack

postgres:
image: postgres:14-alpine
restart: unless-stopped
volumes:
- ./configuration/postgresql/initdb:/docker-entrypoint-initdb.d
- postgresqldata:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=root

redis:
image: redis:7-alpine
restart: unless-stopped
command: redis-server --save 300 1 --save 60 100 --appendonly no
volumes:
- redisdata:/data

mosquitto:
image: eclipse-mosquitto:2
restart: unless-stopped
ports:
- 1883:1883
volumes:
- ./configuration/mosquitto/config/:/mosquitto/config/

semtech_pkt_fwd:
build: .
volumes:
- /dev:/dev
device_cgroup_rules:
- ‘c 254:* rmw’
- ‘c 153:* rmw’
- ‘c 89:* rmw’
ports:
- 1700:1700/udp

volumes:
postgresqldata:
redisdata:

Greetings @FloSolio,

Let me try to summarize your network setup to make sure I understand.

So you have 1 container that sends data on UDP port 1700 in a container. This information needs to then be received by another container listening on the same port. Is that more or less accurate?

If that is the case, then this should be relatively simple. What you’re trying to do right now is send the information via port binding to the host network then back to the container that needs to receive the data. There is a better alternative. Since you’re launching all your containers via docker-compose, this means every container launched in this compose file is in the same network. More details here: Networking | Docker Docs

As seen in the documentation each container in this shared network is discoverable by other containers via a hostname that is equal to the service name that you defined in the compose file. With that you should be able to use the hostname of the container that is outputting the data to access it from the listening container, without the need to bind ports to the host network.

Does this work for your needs?

Best Regards,
Jeremias

1 Like

Hi Jeremias,

It is exactly that, you understood my problematics.

I find after three days of trying the solution.

I added this two container in a network called my_net via networks: option.

At the end of the docker compose file , i declared the my_net network not as bridge, but as host.

And it worked. I guess it is not the best pratice because it open to the world my board …

And it worked. I guess it is not the best pratice because it open to the world my board …

Well to be precise it doesn’t “open” your board to the world. What setting the host networking does is make it so that the container runs on the same network stack as the host OS instead of a separate bridge network. This gives your container more access to the host, but it doesn’t do anything to “open” your board up to the world more than it is.

In some cases this can be an acceptable approach but it depends on your system and use-case.

Does the alternate approach I suggested work for you?

Best Regards,
Jeremias