I have a podman container which has a python file to run a script that creates a container of a specific image_name and container_name.
Recreate the issue by folwing the below instaructions:
mkdir trial
cd trial
touch Dockerfile
touch create_container.py
Python File content:
from podman import PodmanClient
import sys
def create_container(image_name, container_name):
with PodmanClient() as client:
try:
# Create and start the container
container = client.containers.create(image=image_name, name=container_name)
container.start()
print(f"Container '{container_name}' created and started successfully.")
print(f"Container ID: {container.id}")
except Exception as e:
print(f"Error creating container: {e}")
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) != 3:
sys.exit(1)
image_name = sys.argv[1]
container_name = sys.argv[2]
create_container(image_name, container_name)
DocekrFile:
FROM python:3.8.5-slim-buster
WORKDIR /app
# Copy the Python script into the container
COPY create_container.py .
# Install the Podman library
RUN pip install podman
# Set the entrypoint to run the Python script
ENTRYPOINT ["python", "create_container.py"]
Run :
podman build -t test
podman run --rm --privileged --network host -v /run/podman/podman.sock:/run/podman/podman.sock test <Name of the image> trial
Getting the Error:
Error creating container: http://%2Ftmp%2Fpodmanpy-runtime-dir-fallback-root%2Fpodman%2Fpodman.sock/v5.2.0/libpod/containers/create (POST operation failed)
My approach to solve the issue: 1)Thought that the Podmanclient is taking a random socket location, hence hardcoded the location when using Podmanclient in the python file.
...
with PodmanClient(uri='unix:///run/podman/podman.sock') as client:
.
.
.
3)Podman service would go inactive after a while hence changed the file at /usr/lib/systemd/system/podman.service to the below mentioned code:
[Unit]
Description=Podman API Service
Requires=podman.socket
After=podman.socket
Documentation=man:podman-system-service(1)
StartLimitIntervalSec=0
[Service]
Type=exec
KillMode=process
Environment=LOGGING="--log-level=info"
ExecStart=/usr/bin/podman $LOGGING system service tcp:0.0.0.0:8080 --time=0
[Install]
WantedBy=default.target
tried changing the tcp url to 127.0.0.1(loclhost) as well yet no success.
Thank you.
Code that runs outside the container. No change in the problem even if i add the extra os.environ in create_container.py file as well. import os import podman
# Set the Podman socket (adjust if necessary)
os.environ['PODMAN_SOCKET'] = '/run/user/1000/podman/podman.sock'
def create_container(image_name, container_name, command):
try:
print(f'Starting Container: {image_name}')
print("Command running: " + command)
client = podman.PodmanClient() # Initialize Podman client
# Use bind mount instead of named volume
volume_src = '/home/vinee/myprojects/trial' # Host directory
volume_dst = '/edge/' # Container mount point
# Ensure the source path exists
if not os.path.exists(volume_src):
raise ValueError(f"Source volume path does not exist: {volume_src}")
# Create the mount configuration
bind_volumes = [
{
'type': 'bind',
'source': volume_src,
'target': volume_dst,
'read_only': False # Set to True if you want read-only access
}
]
# Create and start the container
container = client.containers.run(
image=image_name,
name=container_name,
command=command,
detach=True,
mounts=bind_volumes, # Use the mounts configuration
auto_remove=False,
network_mode="host",
shm_size=2147483648,
privileged=True,
devices=['/dev/nvidia0'], # Specify device paths as needed
environment={'TZ': 'Asia/Kolkata'}
)
print(f"Container ID: {container.id}")
container_data = {
'containername': container_name,
'containerid': container.id,
'imagename': image_name,
'status': "RUNNING"
}
print("Container Information:")
print(container_data)
print("-" * 10 + " Container information updated successfully " + "-" * 10)
print("Container Started Successfully")
return {'message': 'Success'}
except podman.errors.PodmanError as e:
print(f"Podman specific error: {e}")
except Exception as e:
print(f"General error creating container: {e}")
return {'message': 'Error creating container'}
if __name__ == "__main__":
result = create_container(
"pythontest",
"aA993dc42c504f4e853261105a2351bd",
"python3 test.py"
)
print(result)
I didn't try messing with any permissons or unit-files but I had no problems using the container from your Dockerfile.
Are you sure you're not just missing the base_url
argument?
I ran the newly built container test
and mounted in the (rootless) podman socket.
I used PodmanClient
interatively for a quick test:
$ podman run -it --rm --entrypoint='python3' --privileged \
-v /run/user/1000/podman/podman.sock:/run/podman/podman.sock test
Python 3.8.5 (default, Sep 10 2020, 16:58:22)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from podman import PodmanClient
>>> with PodmanClient(base_url="http+unix:///run/podman/podman.sock") as client:
... container = client.containers.run("busybox", ["sleep","infinity"],
... detach=True, name="sleeper")
...
>>>
Exit python container and verify sleeper
container creation:
$ podman ps --filter=name=sleeper
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c98f94c5c76f docker.io/library/busybox:latest sleep infinity 17 seconds ago Up 17 seconds sleeper
$