podman-py icon indicating copy to clipboard operation
podman-py copied to clipboard

[question] how to use container's `exec_run` with stream enabled?

Open 343i opened this issue 1 year ago • 2 comments

Hi,

I'm trying to convert following snippet of my code that is using docker-py to for exec_run to stream the result:

docker:

  • code:
 exit_code, response = ubuntu_container.exec_run(cmd='apt-get install -y git docker.io neofetch', stream=True)
 
 for line in response:
     print(line.decode('utf-8').strip())
  • output:
Reading package lists...

Building dependency tree...
Reading state information...

docker.io is already the newest version (26.1.3-0ubuntu1).
neofetch is already the newest version (7.1.0-4).
Suggested packages:
  gettext-base git-daemon-run | git-daemon-sysvinit git-doc git-email git-gui
  gitk gitweb git-cvs git-mediawiki git-svn
The following NEW packages will be installed:
git
0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
Need to get 4045 kB of archives.
After this operation, 24.0 MB of additional disk space will be used.
Get:1 http://ports.ubuntu.com/ubuntu-ports oracular/main arm64 git arm64 1:2.45.2-1ubuntu1 [4045 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 4045 kB in 4s (914 kB/s)
Selecting previously unselected package git.

podman:

  • code:
 exit_code, response = ubuntu_container.exec_run(cmd='apt-get install -y git docker.io neofetch', stream=True)
 
 for line in response:
     print(line) # changed as line.decode('utf-8').strip() throws AttributeError: 'int' object has no attribute 'decode'
  • output:
1
0
0
0
0
0
0
59
72
105
116
58
49
32
104

moreover the numbers that are returned in output are returned all at once in end, not in stream fashion,

I assume I'm doing this wrong by trying to force the docker way, can someone share sample code that is to be followed for stream responses

343i avatar Nov 30 '24 20:11 343i

Hey, thanks this is worth investigating. I can't reproduce it with this code here.

from podman import PodmanClient

client = PodmanClient()
c = client.containers.create('ubuntu:latest', command=['sleep', '1000'], detach=True)
c.start()

exit_code response = c.exec_run(cmd='apt-get install -y git', stream=True)
for line in response:
     print(line.decode('utf-8').strip())

Output

Reading package lists...

Building dependency tree...
Reading state information...

E: Unable to locate package git

can you share more details of your reproducer? also, some versions will help me investigating the issue

  • podman version
  • podman-py version
  • python version
  • os

inknos avatar Dec 04 '24 15:12 inknos

hey @343i , did you have the chance to investigate on it?

inknos avatar Jan 21 '25 14:01 inknos

@inknos Hi, I have a relevant question about handling return codes when using exec_run with stream=True.

Since the response is streamed, the return code is always None. Is this the intended behavior by design of the Podman API, or is there a workaround to retrieve the exit code?

ChMcg avatar Sep 05 '25 06:09 ChMcg

Since the response is streamed, the return code is always None. Is this the intended behavior by design of the Podman API, or is there a workaround to retrieve the exit code?

@ChMcg , yes, you are not getting the exit code with stream. It's documented here.

inknos avatar Sep 05 '25 09:09 inknos

closing, can't reproduce and no updates. feel free to reopen if you hit the issue and I'll help you investigating :)

inknos avatar Sep 22 '25 13:09 inknos