docker / docker/docker-py

Container.exec_run crashes with BrokenPipeError: [Errno 32] Broken pipe

Open
#3,218 1 comment 0 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

Code:

docker_client = docker.from_env(use_ssh_client=True)

my_container = docker_client.containers.run(
    IMAGE_NAME,
    detach=True,
    remove=True,
    name=CONTAINER_NAME,
)

result = my_container.exec_run(
    ["sleep", "5"]
)

Crash stack trace:

  ...

  File "/path/to/my-script.py", line 193, in wait_started
    result = my_container.exec_run(
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/virtualenv/lib/python3.11/site-packages/docker/models/containers.py", line 210, in exec_run
    exec_output = self.client.api.exec_start(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/virtualenv/lib/python3.11/site-packages/docker/utils/decorators.py", line 19, in wrapped
    return f(self, resource_id, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/virtualenv/lib/python3.11/site-packages/docker/api/exec_api.py", line 173, in exec_start
    output = self._read_from_socket(res, stream, tty=tty, demux=demux)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/my/virtualenv/lib/python3.11/site-packages/docker/api/client.py", line 437, in _read_from_socket
    response.close()
  File "/path/to/my/virtualenv/lib/python3.11/site-packages/requests/models.py", line 1030, in close
    self.raw.close()
  File "/path/to/my/virtualenv/lib/python3.11/site-packages/urllib3/response.py", line 630, in close
    self._connection.close()
  File "/nix/store/ffll6glz3gwx342z0ch8wx30p5cnqz1z-python3-3.11.5/lib/python3.11/http/client.py", line 964, in close
    sock.close()   # close it manually... there may be other refs
    ^^^^^^^^^^^^
  File "/path/to/my/virtualenv/lib/python3.11/site-packages/docker/transport/sshconn.py", line 94, in close
    self.proc.stdin.flush()
BrokenPipeError: [Errno 32] Broken pipe

I'm using use_ssh_client because I need a SSH ProxyJump to reach the Docker host and for some reason it doesn't work when using Paramiko, it just times out. With use_ssh_client everything seems to work, I'm able to list and pull images, run containers etc. but running exec_run crashes every time.

$ pip freeze | grep docker
docker==7.0.0

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

Reproduce the reported use_ssh_client exec_run failure, then trace the close path through docker/models/containers.py, docker/api/exec_api.py, docker/api/client.py, and docker/transport/sshconn.py. Compare the socket cleanup behavior with the successful image and container operations; done means exec_run with the SSH client and ProxyJump no longer raises BrokenPipeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python
Domain
api, networking
Issue type
Bug
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.