Docker Swarm: Restrict 2 containers always deployed together on the same Node

Nothing changed since then: Use "affinity filter" in "docker service create"

The lagacy standalone Swarm did support affinity using env variables. The build in Swarm-mode does not. If you desperatly need affinity, use the davideshay’s workaround in the linked post or switch to a container orchestrator which actualy supports affinity like Kubernetes.