This should probably read

    if config and ('ExposedPorts' in config):

because in Python, looking up a non-present key leads to an exception.