Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Hubro opened this issue Jan 16, 2024 · 1 comment
Open

Comments

@Hubro
Copy link

Hubro commented Jan 16, 2024

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
@Khushiyant
Copy link
Contributor

The problem stems from the way the use_ssh_client capability is implemented in the Docker library. When use_ssh_client is set to True, the library connects to the Docker host using the SSH client installed on your machine. This is not a standard TCP socket, but rather a pipe connected to the SSH client process.

When you use exec_run on a container, the library attempts to read from the SSH connection via a socket-like interface. However, when the SSH client process terminates (for example, owing to a timeout or a BrokenPipeError), the pipe is closed, and all subsequent efforts to read from it result in a BrokenPipeError.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants