docker / docker/docker-py

Add timeout to `Client.container.exec_run()`

Open
#2,651 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.2k
Forks
1.7k
Avg merge
13d 8h
Merged PRs (30d)
2

Description

It's unusually difficult to add a timeout to this method. This is important if we want to exec a command that may have an unreasonable wait time.

I have tried accessing the underlying socket to add a timeout, but this doesn't seem to have any effect. My initial (not-working) attempt is this:

from docker.utils.socket import consume_socket_output, demux_adaptor, frames_iter

# This exec will not finish for a very long time
_, socket = self.container.exec_run(
    ["sleep", "999999"], stdout=True, stderr=True, demux=True, socket=True
)

# Access the underlying socket to add a timeout
socket._sock.settimeout(5)

# Use internal methods to read from the socket normal
gen = frames_iter(socket, tty)
gen = (demux_adaptor(*frame) for frame in gen)
stdout, stderr = consume_socket_output(gen, demux=True)
return (stdout or stderr).decode()

System details:

docker==4.3.0
Python 3.7.6
Client: Docker Engine - Community
 Version:           19.03.11
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        42e35e61f3
 Built:             Mon Jun  1 09:12:23 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.11
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       42e35e61f3
  Built:            Mon Jun  1 09:10:55 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at Client.container.exec_run() and trace how its returned socket is consumed through docker.utils.socket, including frames_iter, demux_adaptor, and consume_socket_output. Check how a timeout could be exposed without relying on the underlying socket setting. Done means callers can limit a long-running exec and the behavior is covered by the relevant tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.