question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make `DockerClient` a Context Manager

See original GitHub issue

It’s not obvious that DockerClient needs to have its close() method called to clean up resources, which can lead to unclosed sockets. We noticed this using asyncio and pytest together, which complains about unclosed sockets when tearing down test sessions.

Since DockerClient is wrapping ApiClient, and ApiClient inherits its close() method from requests.Session, it’s not even obvious from a brief glance source what close() is for.

I expect that ApiClient is already a Context Manager, since it would inherit __enter__ and __exit__ from requests.Session, so the low-level API already has this behaviour.

To make it easier to avoid mistakes, DockerClient could have __enter__ and __exit__ methods added, identical to requests.Sessions, which would allow safer usage patterns such as

with docker.from_env() as docker_client:
 # Do stuff with docker_client

when that’s feasible.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
johnthagencommented, Apr 11, 2021

When this is implemented, it would be great if the docs could also be updated to show context manager usage as the first example new users find: https://docker-py.readthedocs.io/en/stable/client.html#docker.client.from_env

Also the top level README: https://github.com/docker/docker-py#usage

I too was surprised to find a close() method and no context manager support to call this automatically.

1reaction
feliperuhlandcommented, Apr 7, 2021

Hi, @TBBle That makes total sense and I´m going to take a look at your request.

@ulyssessouza could you assign this issue to me?

Thanks and have a good day 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Docker Context - Docker Documentation
The docker context command makes it easy to export and import contexts on different machines with the Docker client installed. You can use...
Read more >
contextlib — Utilities for with-statement contexts ... - Python Docs
A context manager that is designed to make it easy to programmatically combine other context managers and cleanup functions, especially those that are...
Read more >
The with statement - Back 2 Code
The context manager is responsible of the file lifecycle: initialization ... command # the docker client used to interact with the container ...
Read more >
Any existing built in for creating context managers like python
I am using that library, when creating volumes or containers I want to ensure they are removed when finished with, I have wraped...
Read more >
Docker client - Python on whales - GitHub Pages
DockerClient ( config=None, context=None, debug=None, host=None, ... For example, if you create two containers with the same name from different threads, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found