(Edited Solution Dec 13, 2024)
(Second edit of Solution Dec 28 2024)
Background
I was trying to get the IP address of one of my podman containers.
I was using this command over and over with much pain (and no gain):
podman inspect -f '{{ .NetworkSettings.IPAddress }}' <container-name>
Journey to the Solution
Finally, after much frustration, I resorted to AI shameful face
ChatGPT told me this:
“The command podman inspect -f '{{ .NetworkSettings.IPAddress }}' <container-name>
doesn’t return the IP because, in Podman’s newer versions, container IP addresses are organized under specific networks (not directly under .NetworkSettings.IPAddress as in Docker). This means you need to query the IP address specifically for the network name in use.”
Then, it told me to run this kind of thing which gave me errors on the network name syntax and then finally gave me ‘no value’ output.
podman inspect -f '{{ .NetworkSettings.Networks.<network_name>.IPAddress }}' <container-name>
I also tried to remove the container completely with podman container rm -a
.
No go.
I tried pruning the containers with podman network prune
No go
I was pulling my hair out and then at some unknown point, I was reading something in the terminal and it seemed as though it was complaining about something related to a hyphen / dash.
The Solution
YOU CANNOT USE A PROJECT FOLDER WITH A HYPHEN IN IT IF YOU WANT TO BE ABLE TO RUN THIS COMMAND:
podman inspect project_01 -f '{{ .NetworkSettings.Networks.project_01.projectnetwork }}'
So the solution is this: never create a podman project directory with a hyphen / dash in the name.
Once I changed the hyphen to an underscore, everything worked as expected!
So, here is an example of pulling an IP address out of a Podman container with the following assumptions:
- podman is being run in a directory called ‘project_01’
- your podman container has a container name of ‘project_01’ as well
- you created a network name called ‘project1net` and added your container to it through some means
First, it’s helpful to remember podman network ls
command to show the networks and their names.
Here is the template command to pull the IP address:podman inspect <container-name> -f "{{.NetworkSettings.Networks.<container-name>.<container-network-name>.IPAddress}}"
So let’s fill in the blanks with our template info to see how that looks in real life:
podman inspect project_01 -f "{{.NetworkSettings.Networks.project_01.project1net.IPAddress}}"
You should have a neat little IP spit back to you.
EDIT DEC 28 2024 –
It seems that if you modify the network like I did (ie added a custom IP address and subnet to the container) that things don’t work exactly as per above, however, with one small tweak I easily got my IP address spat back at me. I had to run the command like this:
podman inspect <container-name> -f "{{.NetworkSettings.Networks.<container-network-name>.IPAddress}}"
(Simply omitted the container name)
Bonus!
If you want to see the gateway, IP address and all the IPV6 stuff, too, all you have to do is remove the IPAddress tag at the end and you’ll see more than you need 🙂
Hope this helps.