Skip to content

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

@343i

Description

@343i

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions