james
james

Reputation: 41

Podman network cannot resolve container name

I follow a podman tutorial,which shows multi containers interacting in same network.

$ podman network create foo
/home/user/.config/cni/net.d/foo.conflist
$ podman run -d --name web --hostname web --network foo nginx:alpine
$ podman run --rm --network foo alpine wget -O - http://web.dns.podman

The expected result is

Connecting to web.dns.podman (10.88.4.6:80)
...
<h1>Welcome to nginx!</h1>
...

But I got

wget: bad address 'web.dns.podman'

I guess container network dns fail,But container can resolve other network domain like www.baidu.com normally,it just cannot resolve container name.I have no idea how to fix it.

Upvotes: 4

Views: 8626

Answers (4)

Hielke Walinga
Hielke Walinga

Reputation: 2845

Install dependencies:

sudo apt-get install netavark aardvark-dns

Change configurations:

file /etc/containers/containers.conf

[network]
network_backend = "netavark"

Now after creating the network you see with podman network inspect foo:

[                                            
     {                                                                                         
          "name": "foo",
          "id": "79a41794b5cb811d8d5c6a11f8285d9c21abed9e5fe7014d70c18b0a2345dd97",
          "driver": "bridge",                
          "network_interface": "podman1", 
          "created": "2025-01-13T16:08:42.807457441+01:00",
          "subnets": [
               {                
                    "subnet": "10.89.0.0/24",
                    "gateway": "10.89.0.1"
               }           
          ],                         
          "ipv6_enabled": false,
          "internal": false,
          "dns_enabled": true,
          "ipam_options": {
               "driver": "host-local"                                                                                                                                                          
          }                 
     }
] 

And it should work.

Upvotes: 0

Alijvhr
Alijvhr

Reputation: 2263

PreRequirements:

First you have to install podman-plugins & containernetworking-plugins using this command:

$ sudo dnf -y install podman-plugins containernetworking-plugins

It's required to run this command before network creation. If you already created your network, consider creating a fresh network after installing the packages. rt the system after installation.

Unix Domain Sockets:

***This is the best option I ever tried.***

As an stable and reliable option you can use Unix Domain Sockets and share them through named volumes.

Don't forget to use volumes with this flags to be writeable by container: "rw,z".

The TCP Solution:

Then you should be able to communicate inter-container using container names.

Same Pod

If they are in the same pod, it's enough to call the container alias, Like:

$ podman network create foo
$ podman pod create --name=ptestpod
$ podman run -d --name web1 --pod=testpod --network foo nginx:alpine
$ podman run -d --name web2 --pod=testpod --network foo nginx:alpine

In the web1 container you can simply ping web2 and vise versa, It's working.

Different Pod

If they are not in the same pod but same network, the full name will work. For Example:

$ podman network create foo
$ podman pod create  --name=testpod1
$ podman run -d --name web1 --pod=testpod1 --network foo nginx:alpine
$ podman pod create  --name=testpod2
$ podman run -d --name web2 --pod=testpod2 --network foo nginx:alpine

In this case you should just use fullname. In the web1 container you can ping testpod2_web2_1 and it works!

Notice:

  • If you are not using pods the second case will work everywhere.
  • It's not a problem for containers to register on multiple networks. But the containers must have at least one common network.
  • It's important to keep just one network DNS enabled. If more than one network with enabled dns is connected to container it will fail to resolve anything.

Upvotes: 2

physicus
physicus

Reputation: 401

In the meantime, I found out, what my problem was. I don't know, if it helps in your case.

On my machine, the package podman-dnsname (install it from here or from the package respository of your distro) was missing.

Upvotes: 1

physicus
physicus

Reputation: 401

Did you find a solution? This problem is preventing me form using podman-compose.

My setting is:

  • Podman v 4.3.0
  • Arch Linux, kernel 6.0.7
  • slirp4netns (v 2.5.4) installed

The communication within a pod works as expected, but across containers from different pods, the hostname do net get resolved.

Upvotes: 1

Related Questions