containers / python-podman

Python bindings and code examples for using Varlink access to Podman Service
Apache License 2.0
49 stars 19 forks source link

Libary calls do not work #85

Open Pilzington opened 4 years ago

Pilzington commented 4 years ago

I installed podman and the python podman library. But I does not show any images or containers:

$ podman version
Version:      2.0.4
API Version:  1
Go Version:   go1.14.4
Built:        Thu Jan  1 01:00:00 1970
OS/Arch:      linux/amd64

$ podman images
REPOSITORY                TAG     IMAGE ID      CREATED       SIZE
docker.io/library/nginx   latest  4bb46517cac3  7 days ago    137 MB
docker.io/library/alpine  latest  a24bb4013296  2 months ago  5.85 MB

$ podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED            STATUS                PORTS   NAMES
96f979df67f8  docker.io/library/nginx:latest  nginx -g daemon o...  About an hour ago  Up About an hour ago          eager_galileo

My Python Script:

#!/usr/bin/env python3

import podman

with podman.Client() as client:
    print(client.images.list())
    for image in client.images.list():
        print(image)
    print(client.containers.list())
    for container in client.containers.list():
        print(container)

Output:

<generator object Images.list at 0x7f41cbd344c0>
<generator object Containers.list at 0x7f41cbd344c0>

Am I doing anything wrong? I installed it first following this blog post. Does this break anything due to the new API. Also is there a documentation anywhere?

rhatdan commented 4 years ago

We are concentrating more on podman-py, which should use the new API.

bmarlow commented 4 years ago

Are you running the script and the command as the same user (eg are you running podman images as root)?

The podman client module requires root access (due to needing a unix socket binding), but each user has a unique list of images, containers, etc.

So if you run the command as your regular user, but the script as root, you'll get different output.

The reason that you are getting the output you have is because client.images.list() is a function and returns a generator, which has to be called to yield an actual value (that's why you're getting generator object), and because there are no images the for loops are printing nothing.

In order to print the images you'll need to iterate over the generator object (as you are doing with your for loop).

Hopefully this helps.

Pilzington commented 4 years ago

Are you running the script and the command as the same user (eg are you running podman images as root)?

The podman client module requires root access (due to needing a unix socket binding), but each user has a unique list of images, containers, etc.

So if you run the command as your regular user, but the script as root, you'll get different output.

Ah thanks this was my problem.

The reason that you are getting the output you have is because client.images.list() is a function and returns a generator, which has to be called to yield an actual value (that's why you're getting generator object), and because there are no images the for loops are printing nothing.

In order to print the images you'll need to iterate over the generator object (as you are doing with your for loop).

Hopefully this helps.

I only printed these to show that the library seems to be working, if this makes sense.

Another question, if you don't mind: Is it possible to process all podman images/containers belonging to all users for an admin user?

Thank you very much for the help and support.