Working with Docker filters in python

I needed to man-in-the-middle traffic of Docker containers to automatically extract game server status information from plaintext http requests to their master server.
So I wrote a python script for that, but I had some weird behavior on the Docker network “name” filter.
The behavior seems not to be documented on the documentation.


Game servers are running inside an overlay network called “condor2”.
On the Docker host, I run a python script to extract game server information from network traffic flowing through the Docker overlay network.


# docker version
 Client: Docker Engine - Community
  Version:           19.03.2
  OS/Arch:           linux/amd64
 Server: Docker Engine - Community
   Version:          19.03.2
   API version:      1.40 (minimum version 1.12)
   Version:          1.2.6
# pip3 freeze | grep docker
# python3 --version
 Python 3.5.3


Running this piece of script

import docker
d = docker.from_env()
n = d.networks.list(filters={"name":"condor2"})

returns an object like that one:

>>> print (n)
[<Network: 228e98c554>, <Network: b9aafep0a6>]

Both networks have “condor2” in their name:

# docker network inspect 228e98c554 | grep Name    
"Name": "condor2",
# docker network inspect b9aafep0a6 | grep Name
        "Name": "prod_dvaec_condor2-bots_default",


To get exact matches for your filter, you need to use regex inside the filter:

#!/usr/bin/env python3
import docker
d = docker.from_env()
n = d.networks.list(filters={"name":"^condor2$"})

The result looks like that now:

>>> print (n)
[<Network: 228e98c554>]

Bingo. That’s what I actually needed.