Skip to content

faster check for docker running #1569

@asottile

Description

@asottile

currently this is how pre-commit checks if docker is available:

def docker_is_running() -> bool: # pragma: win32 no cover
try:
cmd_output_b('docker', 'ps')
except CalledProcessError:
return False
else:
return True
def assert_docker_available() -> None: # pragma: win32 no cover
assert docker_is_running(), (
'Docker is either not running or not configured in this environment'
)

there's two low-hanging-fruit problems with this:

  1. it is checked repeatedly, but can probably be checked just once per process (can use functools.lru_cache as a "run once" decorator)
  2. docker ps is pretty slow, there's probably a faster way to see if docker is available -- on my machine it takes more than a second! even without containers
    $ time docker ps
    CONTAINER ID  IMAGE   COMMAND  CREATED  STATUS  PORTS   NAMES
    
    real	0m1.169s
    user	0m0.088s
    sys	0m0.085s

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions