Skip to main content

Exploring Podman RESTful API using Python and Bash

Check out Podman's new RESTful API using Python and Bash.
Exploring Podman's new RESTful API with Python and Bash

You may have heard that Podman V2 has a new RESTful API. This document demonstrates the API using code examples in Python and shell commands. Additional notes are included in the code comments. The provided code was written to be clear vs. production quality.


  • You have Python >3.4 installed.
  • You have installed the Python requests library.
  • An IDE for editing Python is recommended.
  • Two terminal windows: one for running the Podman service and reviewing debugging information, the second window to run scripts.
  • Usage of curl and jq commands are demonstrated.
  • You can review connection URLs here.

Getting started

The service

For these examples, we are running the Podman service as a normal user and on an unsecured TCP/IP port number.

For production, the Podman service should use systemd's socket activation protocol. This allows Podman to support clients without additional daemons and secure the access endpoint.

The following command runs the Podman service on port 8080 without timing out. You will need to type ^C into this terminal window when you are finished with the tutorial.

# podman system service tcp:localhost:8080 --log-level=debug --time=0

In addition to the TCP socket demonstrated above, the Podman service supports running under systemd's socket activation protocol and Unix domain sockets (UDS).

[ You might also like: Sneak peek: Podman's new REST API ]

Python code

Info resource

The following shows us information about the Podman service and host:

import json
import requests

response = requests.get("http://localhost:8080/v1.40.0/libpod/info")

Deep dive

  • requests.get() calls the requests library to pass the URL to the Podman service using the GET HTTP method.
    • The requests library provides helper methods for all the popular HTTP methods.
  • http://localhost:8080 matches the Podman service invocation above.
  • /v1.40.0 denotes the API version we are using.
  • /libpod denotes that we expect the service to provide a libpod-specific return payload.
    • Not using this element causes the server to return a compatible payload.
  • /info specifies the resource we are querying.

Interesting to read, but without output, how do we know it worked?

Getting output

Append the lines below, and you can now see the version of Podman running on the host:


info = json.loads(response.text)
  • raise_for_status() raises an exception if status code is not between 200 and 399.
  • json.loads() decodes the body of the HTTP response into an object/dictionary.

When executed, the output is:


The following works from the shell:

$ curl -s 'http://localhost:8080/v1.40.0/libpod/info' | jq .version.Version


Listing containers

import json
import requests

response = requests.get("http://localhost:8080/v1.40.0/libpod/containers/json?all=true")

ctnrs = json.loads(response.text)
for c in ctnrs:

json.loads() decodes the HTTP body into an array of objects/dictionaries, the program then prints each container Id. For example:

$ curl -s 'http://localhost:8080/v1.40.0/libpod/containers/json?all=true' | jq .[].Id


If the query parameter all=true had not been provided, then only the running containers would have been listed. The resource queries and parameters for the API are documented here.

Something useful

We've looked at a couple of examples, but how about something a little more useful? You have finished developing the next great container, and the script below will remove everything from your local storage. (If you want to save on typing

#!/usr/bin/env python
import json

import requests

# Clean up local storage by removing all containers, pods, and images.  Any error will
#   abort the process

confirm = input("Really delete all items from storage? [y/N] ")
if str(confirm).lower().strip() != 'y':

# Query for all pods in storage
response = requests.get("http://localhost:8080/v1.40.0/libpod/pods/json")

pods = json.loads(response.text)
# Workaround for
if pods is not None:
    for p in pods:
        # For each container: delete container and associated volumes
        response = requests.delete(f"http://localhost:8080/v1.40.0/libpod/pods/{p['Id']}?force=true")
    print(f"Removed {len(pods)} pods and associated objects")
    print(f"Removed 0 pods and associated objects")

# Query for all containers in storage
response = requests.get("http://localhost:8080/v1.40.0/libpod/containers/json?all=true")

ctnrs = json.loads(response.text)
for c in ctnrs:
    # For each container: delete container and associated volumes
    response = requests.delete(f"http://localhost:8080/v1.40.0/libpod/containers/{c['Id']}?force=true&v=true")
print(f"Removed {len(ctnrs)} containers and associated objects")

# Query for all images in storage
response = requests.get("http://localhost:8080/v1.40.0/libpod/images/json")

imgs = json.loads(response.text)
for i in imgs:
    # For each image: delete image and any associated containers
    response = requests.delete(f"http://localhost:8080/v1.40.0/libpod/images/{i['Id']}?force=true")
print(f"Removed {len(imgs)} images and associated objects")

[ Getting started with containers? Check out this free course. Deploying containerized applications: A technical overview. ]


I hope you find this helpful. The API documentation provides you with all the resources and required methods. The input and output bodies are included as well as the status codes.

The Podman code is under heavy development and we welcome your input with issues and pull requests on the project's GitHub page. 

Topics:   Linux   Containers   Podman  
Author’s photo

Jhon Honce

Experienced senior software developer/system administrator with 37 years of technical experience. Deep understanding of UNIX/Linux, TCP/IP networking, Internet and software development. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.