UDP socket host-container doesnt work

Hi

I have an Ubuntu 22.04 host system running a container Ubuntu 24.04. I’m using Docker Desktop 4.36.0.

Container params:
“–hostname=devcontainer”,
“–network=host”,
“–privileged”·

Im testing a basic udp echo server:

cat udp_server_echo.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define PORT 12345 /
#define BUFFER_SIZE 1024

int main() {
int sockfd;
struct sockaddr_in6 servaddr, clientaddr;
char buffer[BUFFER_SIZE];
socklen_t addr_len;
ssize_t n;

sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror(“Error al crear el socket”);
exit(EXIT_FAILURE);
}

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin6_family = AF_INET6;
servaddr.sin6_port = htons(PORT);
servaddr.sin6_addr = in6addr_any;

if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror(“Error en bind”);
close(sockfd);
exit(EXIT_FAILURE);
}

printf(“Server UDP IPv6 listening on port %d…\n”, PORT);

addr_len = sizeof(clientaddr);
while (1) {

n = recvfrom(sockfd, buffer, BUFFER_SIZE - 1, 0,
(struct sockaddr *)&clientaddr, &addr_len);
if (n < 0) {
perror(“Error al recibir datos”);
continue;
}

buffer[n] = ‘\0’;
char client_ip[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &clientaddr.sin6_addr, client_ip, sizeof(client_ip));
printf(“Msg received from %s:%d: %s\n”,
client_ip, ntohs(clientaddr.sin6_port), buffer);

n = sendto(sockfd, buffer, n, 0, (struct sockaddr *)&clientaddr, addr_len);
if (n < 0) {
perror(“Error al enviar datos”);
} else {
printf(“Msg sent back to %s:%d\n”,
client_ip, ntohs(clientaddr.sin6_port));
}
}

close(sockfd);
return 0;
}

On the client side (host):
echo “Hola, servidor!” | nc -u -6 ::1 12345

ON the server side (inside the container):
./udp_server_echo
Server UDP IPv6 listening on port 12345…
Msg received from ::1:58520: Hola, servidor!

Mensaje sent back to ::1:58520

But the client doesnt receive back the message.
tcpdump or wireshark only shows the udp resquest, there is not response in the socket.

Am I missing something?

Best regards.

Is your echo server working without Docker and without Docker Desktop (which adds a VM as another layer)?

Hi.
Docker Desktop 4.36.0
Docker engine
$ docker --version
Docker version 27.4.1, build b9d17ea

Container details:
$ docker inspect objective_margulis
[
{
“Id”: “19934c121f40a74beb3517620c0081dcb5a754093ff9dc65f8429f9d20ecb212”,
“Created”: “2024-12-20T08:02:08.527350812Z”,
“Path”: “/bin/sh”,
“Args”: [
“-c”,
“echo Container started\ntrap "exit 0" 15\n\nexec "$@"\nwhile sleep 1 \u0026 wait $!; do :; done”,
“-”
],
“State”: {
“Status”: “running”,
“Running”: true,
“Paused”: false,
“Restarting”: false,
“OOMKilled”: false,
“Dead”: false,
“Pid”: 43257,
“ExitCode”: 0,
“Error”: “”,
“StartedAt”: “2024-12-20T08:02:08.657910163Z”,
“FinishedAt”: “0001-01-01T00:00:00Z”
},
“Image”: “sha256:319dce69cb02ada51436499cdd76b06bd065b94b0c88e67ab14bcfb800561380”,
“ResolvConfPath”: “/var/lib/docker/containers/19934c121f40a74beb3517620c0081dcb5a754093ff9dc65f8429f9d20ecb212/resolv.conf”,
“HostnamePath”: “/var/lib/docker/containers/19934c121f40a74beb3517620c0081dcb5a754093ff9dc65f8429f9d20ecb212/hostname”,
“HostsPath”: “/var/lib/docker/containers/19934c121f40a74beb3517620c0081dcb5a754093ff9dc65f8429f9d20ecb212/hosts”,
“LogPath”: “/var/lib/docker/containers/19934c121f40a74beb3517620c0081dcb5a754093ff9dc65f8429f9d20ecb212/19934c121f40a74beb3517620c0081dcb5a754093ff9dc65f8429f9d20ecb212-json.log”,
“Name”: “/objective_margulis”,
“RestartCount”: 0,
“Driver”: “overlayfs”,
“Platform”: “linux”,
“MountLabel”: “”,
“ProcessLabel”: “”,
“AppArmorProfile”: “”,
“ExecIDs”: [
“72c3e09daee4c3504889d6969b071cb970c1ef2db28e45ef7bdcc14aa32a709e”,
“d19da184fd22a185c184cab231c0f8bc7c59d6a622b53be3fc9e03ae25794e55”,
“096837f1a272a09cbfef48f8d3009159e33aa9a7dd68a89145c817ab7c2e3390”,
“c4d7beaa9bab7cc4dbe69f650ff7d834acaf7d265bf18f8442d2d7423cb2d0c6”,
“986da13f24cbf787f319c200fe14e3b6aca00195279701c45f94c05ce9dbcdd6”,
“6c5f83d47e63b7f3e4a7afc616512ff2ad66282f6c5b8c4615c5e35ef4f227f4”,
“8c1529e4479ef8bb331fe338142280d80d5e5c4ae2adc4df92296f6860193221”,
“c50738ed708e893f70d87ea219367af2b2620d0144e5e3f2158c58f251748f40”,
“9723e6b5cdcd307bc5e630ff06a70d4cd7091f1c45fc46e1f7a32908ae696159”
],
“HostConfig”: {
“Binds”: null,
“ContainerIDFile”: “”,
“LogConfig”: {
“Type”: “json-file”,
“Config”: {}
},
“NetworkMode”: “host”,
“PortBindings”: {},
“RestartPolicy”: {
“Name”: “no”,
“MaximumRetryCount”: 0
},
“AutoRemove”: false,
“VolumeDriver”: “”,
“VolumesFrom”: null,
“ConsoleSize”: [
24,
80
],
“CapAdd”: null,
“CapDrop”: null,
“CgroupnsMode”: “private”,
“Dns”: ,
“DnsOptions”: ,
“DnsSearch”: ,
“ExtraHosts”: null,
“GroupAdd”: null,
“IpcMode”: “private”,
“Cgroup”: “”,
“Links”: null,
“OomScoreAdj”: 0,
“PidMode”: “”,
“Privileged”: true,
“PublishAllPorts”: false,
“ReadonlyRootfs”: false,
“SecurityOpt”: [
“label=disable”
],
“UTSMode”: “”,
“UsernsMode”: “”,
“ShmSize”: 67108864,
“Runtime”: “runc”,
“Isolation”: “”,
“CpuShares”: 0,
“Memory”: 0,
“NanoCpus”: 0,
“CgroupParent”: “”,
“BlkioWeight”: 0,
“BlkioWeightDevice”: ,
“BlkioDeviceReadBps”: ,
“BlkioDeviceWriteBps”: ,
“BlkioDeviceReadIOps”: ,
“BlkioDeviceWriteIOps”: ,
“CpuPeriod”: 0,
“CpuQuota”: 0,
“CpuRealtimePeriod”: 0,
“CpuRealtimeRuntime”: 0,
“CpusetCpus”: “”,
“CpusetMems”: “”,
“Devices”: ,
“DeviceCgroupRules”: null,
“DeviceRequests”: null,
“MemoryReservation”: 0,
“MemorySwap”: 0,
“MemorySwappiness”: null,
“OomKillDisable”: null,
“PidsLimit”: null,
“Ulimits”: ,
“CpuCount”: 0,
“CpuPercent”: 0,
“IOMaximumIOps”: 0,
“IOMaximumBandwidth”: 0,
“Mounts”: [
{
“Type”: “bind”,
“Source”: “/home/gabilm/zephyrbox-uoscore-uedhoc”,
“Target”: “/workspaces/zephyrbox-uoscore-uedhoc”
},
{
“Type”: “volume”,
“Source”: “vscode”,
“Target”: “/vscode”
}
],
“MaskedPaths”: null,
“ReadonlyPaths”: null
},
“GraphDriver”: {
“Data”: null,
“Name”: “overlayfs”
},
“Mounts”: [
{
“Type”: “bind”,
“Source”: “/home/gabilm/zephyrbox-uoscore-uedhoc”,
“Destination”: “/workspaces/zephyrbox-uoscore-uedhoc”,
“Mode”: “”,
“RW”: true,
“Propagation”: “rprivate”
},
{
“Type”: “volume”,
“Name”: “vscode”,
“Source”: “/var/lib/docker/volumes/vscode/_data”,
“Destination”: “/vscode”,
“Driver”: “local”,
“Mode”: “z”,
“RW”: true,
“Propagation”: “”
}
],
“Config”: {
“Hostname”: “devcontainer”,
“Domainname”: “”,
“User”: “ubuntu”,
“AttachStdin”: false,
“AttachStdout”: true,
“AttachStderr”: true,
“Tty”: false,
“OpenStdin”: false,
“StdinOnce”: false,
“Env”: [
“PATH=/home/ubuntu/zephyrproject/.venv/bin:/home/ubuntu/zephyrproject/.venv/bin:/home/ubuntu/zephyrproject/.venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”,
“VIRTUAL_ENV=/home/ubuntu/zephyrproject/.venv”
],
“Cmd”: [
“-c”,
“echo Container started\ntrap "exit 0" 15\n\nexec "$@"\nwhile sleep 1 \u0026 wait $!; do :; done”,
“-”
],
“Image”: “vsc-zephyrbox-uoscore-uedhoc-b52d6c82dab1c57cb16c89decef305f18b3e3cf832be32cde088c9be8a0b65c1-uid”,
“Volumes”: null,
“WorkingDir”: “/home/ubuntu/zephyrproject/zephyr”,
“Entrypoint”: [
“/bin/sh”
],
“OnBuild”: null,
“Labels”: {
“devcontainer.config_file”: “/home/gabilm/zephyrbox-uoscore-uedhoc/.devcontainer/devcontainer.json”,
“devcontainer.local_folder”: “/home/gabilm/zephyrbox-uoscore-uedhoc”,
“devcontainer.metadata”: “{"postCreateCommand":"uname -a","waitFor":"initializeCommand","customizations":{"vscode":{"settings":{"C_Cpp.default.includePath":["/home/ubuntu/zephyrproject/zephyr/include"],"editor.formatOnSave":true,"editor.codeActionsOnSave":{"source.organizeImports":"always"},"editor.rulers":[80]},"extensions":["mhutchie.git-graph","ms-vscode.cpptools-extension-pack","mcu-debug.debug-tracker-vscode","marus25.cortex-debug","ms-vscode.cmake-tools","twxs.cmake","alphabotsec.vscode-eclipse-keybindings","streetsidesoftware.code-spell-checker"]}},"forwardPorts":[61234]}”,
“org.opencontainers.image.ref.name”: “ubuntu”,
“org.opencontainers.image.version”: “24.04”
}
},
“NetworkSettings”: {
“Bridge”: “”,
“SandboxID”: “dbc57ac377e3b90c798e2c8af66a64fe79281f2c7626dd89b00b9bb09a7c2a70”,
“SandboxKey”: “/var/run/docker/netns/default”,
“Ports”: {},
“HairpinMode”: false,
“LinkLocalIPv6Address”: “”,
“LinkLocalIPv6PrefixLen”: 0,
“SecondaryIPAddresses”: null,
“SecondaryIPv6Addresses”: null,
“EndpointID”: “”,
“Gateway”: “”,
“GlobalIPv6Address”: “”,
“GlobalIPv6PrefixLen”: 0,
“IPAddress”: “”,
“IPPrefixLen”: 0,
“IPv6Gateway”: “”,
“MacAddress”: “”,
“Networks”: {
“host”: {
“IPAMConfig”: null,
“Links”: null,
“Aliases”: null,
“MacAddress”: “”,
“DriverOpts”: null,
“NetworkID”: “69e086fbf8c5bb39f194a82c320f16432e6a9777e5b799e5ac2e66d7897b9024”,
“EndpointID”: “bd9e35744b80880a03cf96eb1fc50c5e31f079cf91bcc4948170dc548426b821”,
“Gateway”: “”,
“IPAddress”: “”,
“IPPrefixLen”: 0,
“IPv6Gateway”: “”,
“GlobalIPv6Address”: “”,
“GlobalIPv6PrefixLen”: 0,
“DNSNames”: null
}
}
}
}
]

Thanks in advance.

Thanks for what? I asked a question and you dump unformatted JSON here.

Thanks for the time to read the post. Even if it is not what you asked.
Yes, the service runs without Docker and docker desktop.
The json includes the details of the container, just in case it can help.