Canonical: https://nitride.xyz/blog/shred-receiver-in-docker

# Running a shred receiver in Docker without losing packets

Host networking, a host-wide buffer limit, no fractional CPU quota, and drop counters read in the right namespace: what keeps a containerized shred receiver whole.

Give the receiving container host networking. Raise net.core.rmem_max on the host, because no container can change it, and give the receiver whole cores with --cpuset-cpus rather than a fractional --cpus quota. Read drop counters in the container's own network namespace. Bridge mode with a published port works, but adds NAT and a veth hop.

## Start with host networking

Docker's defaults suit a web service. If you are chasing UDP packet loss in Docker on a stream that cannot be resent, they are the first place to look. A shred receiver is close to the worst case for those defaults ([Solana shreds, explained](https://nitride.xyz/learn/solana-shreds) covers why each datagram matters, and [what a launch frenzy does to a shred feed](https://nitride.xyz/blog/launch-frenzy-packet-rates) shows the peaks): a steady flow of small datagrams, no retransmission, and a program that has to keep up on every slot.

So the first decision is the network mode, and our default is `--network host`. Docker's own documentation describes it plainly: the container's network stack is not isolated from the host, no network address translation is needed, and no userland proxy is created for each port. It also lists performance as a reason to use it. The receiver binds the destination port on the host's own interfaces, and packets reach its socket by the same path they would without Docker.

What you give up is isolation. The container sees every interface on the host, port conflicts become your problem, and `-p` options are ignored with a warning. For a dedicated receiver box we take that trade every time. If the same machine runs untrusted workloads, fix that by moving them, not by putting the receiver behind NAT.

## What a published port does to a UDP stream

If you stay on the default bridge with `-p 9000:9000/udp`, three things sit between the wire and your socket.

**A DNAT rule.** Docker's port publishing docs say it sets up NAT and masquerading rules for each published port. On our test host (Docker 20.10, iptables) the rule for a test port looked like this:

```text file="output"
-A DOCKER ! -i docker0 -p udp -m udp --dport 19000 -j DNAT --to-destination 172.17.0.42:19000
```

Every datagram then crosses the nat table, the docker0 bridge and a veth pair into the container's namespace. That works, and for most traffic it is fast enough, but it is more code on the receive path, and connection tracking keeps an entry for the flow. The kernel's nf_conntrack docs give the UDP timeouts (30 seconds, and 120 for a flow it sees as a stream) and the table size limit, `nf_conntrack_max`. A single feed is one flow, so the table rarely matters, unless the same host also handles a flood of short UDP flows.

**Your host firewall stops applying.** Docker's firewall page says it routes container traffic in the nat table, before the `INPUT` chain that ufw uses, so ufw rules are effectively ignored for published ports. The rules in [firewall rules for the feed](https://nitride.xyz/docs/delivery/firewall) assume a plain host process; behind `-p`, check Docker's own chains instead.

**docker-proxy, for loopback traffic.** The dockerd reference describes `--userland-proxy` as the proxy for loopback traffic, on by default. It is a separate process with its own UDP socket that receives and resends each datagram. We measured it with a container that bound a published port and stopped reading, sending 3,000 datagrams first to `127.0.0.1`, then 3,000 to the host's own address:

```text file="output"
before:                          proxy socket drops: 4303  container socket drops: 5604
after 3000 via 127.0.0.1:        proxy socket drops: 5309  container socket drops: 7598
after 3000 via the host address: proxy socket drops: 5309  container socket drops: 10598
```

Through loopback, the proxy itself dropped 1,006 and passed 1,994 on to the container. Through the host address, the proxy saw nothing and the DNAT rule delivered all 3,000 to the container's socket. Two lessons: a local test against `127.0.0.1` exercises a different path from real delivery, and a proxy drop shows up in the host's counters, not the container's.

## Buffers: one limit for the whole host

The setting that matters most for a receiver is the cap on its receive buffer, and in a container it is the one you cannot touch. `SO_RCVBUF` is per socket, set by your program; socket(7) says the kernel caps the request at `net.core.rmem_max`, then doubles it. That limit lives in one global variable for the whole kernel.

The kernel source is explicit about it. In `net/core/sysctl_net_core.c`, `rmem_max` and `rmem_default` point at the global values, and the code that registers them for any network namespace other than the initial one clears their write bits. On Linux 6.11 and later they are visible inside a container but read only. Before 6.11 they are not registered there at all. Our test host ran 6.8, and the file was simply missing:

```text file="output"
$ docker run --rm python:3.12-alpine cat /proc/sys/net/core/rmem_max
cat: can't open '/proc/sys/net/core/rmem_max': No such file or directory
$ docker run --rm --sysctl net.core.rmem_max=26214400 python:3.12-alpine true
docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: open /proc/sys/net/core/rmem_max: no such file or directory: unknown.
```

`--sysctl` accepts the `net.*` names that really are per namespace; `net.core.somaxconn=1024` worked in the same test. With `--network host`, Docker refuses every `net.*` sysctl:

```text file="output"
docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: sysctl "net.core.somaxconn" not allowed in host network namespace: unknown.
```

So the fix is on the host: raise `net.core.rmem_max` there, persist it in `/etc/sysctl.d`, and restart the container, since the buffer size is fixed when the socket opens. Inside the container, a receiver that asked for 8 MiB got 425,984 bytes: double 212,992, the host's limit, which nobody had raised. [Linux sockets and buffers](https://nitride.xyz/docs/receiver/linux-sockets#raise-the-limit) has the commands and how to size it.

`SO_RCVBUFFORCE` ignores the cap but needs `CAP_NET_ADMIN`. Granting a receiver that capability to dodge one sysctl hands it far more than a buffer; set the host limit instead.

## CPU quotas throttle in bursts

`--cpus 0.5` does not give the receiver half a core all the time. Docker's resource docs say `--cpus` and `--cpu-quota` are implemented with the CFS quota, and cgroup v2 shows it as `cpu.max`: a budget per period. Once the budget is spent, the container stops running until the next period begins. A busy loop in a container with `--cpus 0.5` for three seconds:

```text file="output"
50000 100000
nr_periods 31
nr_throttled 30
throttled_usec 1489668
```

It was throttled in 30 of 31 periods. For a web server, a pause like that is a slow request. For a receiver, it is a stretch where nobody reads the socket while shreds keep arriving, and whatever does not fit in the buffer is lost. Averages hide it: the container can look half idle and still drop packets every period.

Keep CPU quotas off the receiver. If you need to fence it off, use `--cpuset-cpus` to give it whole cores, which limits where it runs without stopping it on a timer. Then watch `nr_throttled` in `/sys/fs/cgroup/cpu.stat` inside the container; on a receiver it should stay at zero.

## Reading drops from inside and outside

UDP counters belong to a network namespace, so the host's `nstat` does not see what a bridged container drops. Read them where the socket lives:

- **Inside**: `cat /proc/net/udp` (the last column, `drops`, per socket) and `/proc/net/snmp`, which is all a slim image has. Add `ss` and `nstat` to the image if you can.
- **Outside, no tools in the image**: `nsenter -t <pid> -n nstat -az UdpRcvbufErrors UdpInErrors`, with the pid from `docker inspect -f '{{.State.Pid}}'`, runs the host's tools in the container's namespace. `cat /proc/<pid>/net/snmp` works too.

From the host, against a bridged test container that did not read:

```text file="output"
$ nsenter -t $PID -n ss -ulmn
UNCONN 214272 0  0.0.0.0:19601  0.0.0.0:*
	 skmem:(r214272,rb212992,t0,tb212992,f2816,w0,o0,bl0,d1907)
$ nsenter -t $PID -n nstat -az UdpRcvbufErrors
UdpRcvbufErrors                 1907               0.0
```

With host networking there is one namespace, and the usual host commands see the receiver directly. If those counters stay flat and shreds are still missing, the loss is before the socket: on the network, the NIC or the bridge. [Packet loss and jitter](https://nitride.xyz/learn/packet-loss-and-jitter) walks through each place, and `ip -s link` on the container's veth shows drops at that interface.

## A run line to start from

Putting it together for a dedicated receiver:

```sh file="terminal"
sudo sysctl -w net.core.rmem_max=67108864    # host-wide; persist it in /etc/sysctl.d
docker run -d --name shred-rx --network host --cpuset-cpus 2,3 \
  --restart unless-stopped my-receiver:latest
```

We ran this shape with a test image: the receiver's socket appeared in the host's `ss` output, and `cpuset.cpus.effective` read `2-3` inside the container. Pick cores that nothing else on the host is pinned to, and size the buffer from your own drop counters, not from the example number.

If several programs need the stream, keep one receiver here and copy datagrams to the others, as in [one destination, many bots](https://nitride.xyz/blog/one-destination-many-bots). If you reach the point where the socket path itself is the bottleneck, [kernel bypass for a shred receiver](https://nitride.xyz/blog/kernel-bypass-worth-it) is the next question, and the container gets harder from there. None of this changes with the plan: [Ultra and Standard](https://nitride.xyz/compare/ultra-vs-standard-shreds) differ only in the path the packets take before they reach your host. Whatever runs the receiver, the box under it matters more; [choosing a server for a Frankfurt shred feed](https://nitride.xyz/guides/frankfurt-server-for-shreds) covers that.

Each plan has a free 2-hour trial, once per person. Email support@nitride.xyz with the IPv4 address and UDP port you want to test. We verify you receive on that address, then your 2 hours start.
