Cannot send UDP packets over a local network using Docker-Compose

I have an application that is meant to send UDP messages to other devices on a local network. When I run the application as a standalone Docker container using the docker run command, the behavior is as expected and the messages are sent to the correct address and port that corresponds to a computer on the local network. Please note that it works whether or not I run it with the bridge or host network. However, when attempting to run the application through docker compose the UDP messages are not sent. To verify that there was no conflict with other containers running in compose, I ran the container on its own in docker-compse and the messages were still not being sent. I tried running the container in docker-compose while specifying network_mode: host as well. I checked Wireshark and it reported that UDP messages were being sent when the application was started with docker run but none appearend when running with docker-compose. Additionally, I enabled Ipv4 forwarding from docker containers to the outside world on the host machine as described here with no luck.

Here are the two ways I am running the container:

Docker:
docker run --network host -e OUTPUT=192.168.1.3:14551 container_name

Docker-Compose:

version: "3"
services:
  name:
    image: name
    network_mode: host # have tried with and without this
    environment:
      - OUTPUT=192.168.1.3:14551

I have also tried exposing the 14551 port in a ports section of the docker-compose, however that did not change anything.

What could explain the difference in behavior with docker vs docker-compose? Is it due to an extra layer of networking with docker compose specifically? Is there a workaround to get docker-compose working?

Docker Compose is just a client tool to manage Docker containers. The only difference I can see between the too ways is Docker Compose creating a custom network bridge for each compose project, but you could do the same with the docker command as well.

However, since you mentioned that UDP packages don’t work even using the host network, I don’t know what else could cause this.

Here are some suggestions you can try to get us closer to the truth.

  • Run the service again both ways and use docker container inspect container_name on both containers. Try to find the differences.
  • Run a container using the docker command and use the --network option to attach the network that the other container started with Docker Compose using.
    docker container inspect wordpress-docker-db-1 --format '{{ range $i,$j := .NetworkSettings.Networks }}{{ printf "%s\n" $i }}{{ end }}'
    
    My test output:
    wordpress-docker_default
    wordpress-docker_one
    wordpress-docker_twoo
    
    Now chose one network from the list. I chose the first (you will have only one):
    docker run --network wodpress-docker_one -e OUTPUT=192.168.1.3:14551 container_name
    
  • An other, very strange case could be when you have multiple Docker contexts and somehow Docker Compose uses a different context. For example you installed Docker from multiple sources to have multiple clients, (“A” and “B”) so Docker Compose uses “B” while “docker” still uses “A”. By this time, if you tried my previous suggestion, you know the answer, because you could see the container started by Docker Compose when you inspected the containers.
1 Like

Thank you for the response. After running the docker container inspect command I noticed no difference (apart from container/process IDs). However when I attached my standalone container to the Docker Compose network, the messages were able to be sent outside of the container. After this I tried to run the container normally Docker-Compose with the bridge network the messages were able to to be sent correctly. I’m not entirely sure yet why it didn’t work when I tried the bridge network previously (it’s possible that something else was different in my compose configuration) but I will try to investigate what was causing the original failure.