Questo contenuto non è disponibile nella lingua selezionata.
Chapter 9. Communicating among containers
Learn about establishing communication between containers, applications, and host systems leveraging port mapping, DNS resolution, or orchestrating communication within pods.
9.1. The network modes and layers Copia collegamentoCollegamento copiato negli appunti!
There are several different network modes in Podman:
-
bridge
- creates another network on the default bridge network -
container:<id>
- uses the same network as the container with<id>
id -
host
- uses the host network stack -
network-id
- uses a user-defined network created by thepodman
network create command -
private
- creates a new network for the container -
slirp4nets
- creates a user network stack withslirp4netns
, the default option for rootless containers -
pasta
- high performance replacement forslirp4netns
. You can usepasta
beginning with Podman v4.4.1. -
none
- create a network namespace for the container but do not configure network interfaces for it. The container has no network connectivity. -
ns:<path>
- path to a network namespace to join
The host mode gives the container full access to local system services such as D-bus, a system for interprocess communication (IPC), and is therefore considered insecure.
9.2. Differences between slirp4netns and pasta Copia collegamentoCollegamento copiato negli appunti!
Notable differences of pasta
network mode compared to slirp4netns
include:
-
pasta
supports IPv6 port forwarding. -
pasta
is more efficient thanslirp4netns
. -
pasta
copies IP addresses from the host, while slirp4netns uses a predefined IPv4 address. -
pasta
uses an interface name from the host, while slirp4netns uses tap0 as interface name. -
pasta
uses the gateway address from the host, whileslirp4netns
defines its own gateway address and uses NAT.
The default network mode for rootless containers is slirp4netns
.
9.3. Setting the network mode Copia collegamentoCollegamento copiato negli appunti!
You can use the podman run
command with the --network
option to select the network mode.
Prerequisites
-
The
container-tools
module is installed.
Procedure
Optional: If you want to use the
pasta
network mode, install thepasst
package:{PackageManager} install passt
$ {PackageManager} install passt
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the container based on the
registry.access.redhat.com/ubi10/ubi
image:podman run --network=<netwok_mode> -d --name=myubi registry.access.redhat.com/ubi9/ubi
$ podman run --network=<netwok_mode> -d --name=myubi registry.access.redhat.com/ubi9/ubi
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
<netwok_mode>
is the required network mode. Alternatively, you can use thedefault_rootless_network_cmd
option in thecontainers.conf
file to switch the default network mode.
The default network mode for rootless containers is slirp4netns
.
Verification
Verify the setting of the network mode:
podman inspect --format {{.HostConfig.NetworkMode}} myubi
$ podman inspect --format {{.HostConfig.NetworkMode}} myubi <netwok_mode>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
9.4. Inspecting a network settings of a container Copia collegamentoCollegamento copiato negli appunti!
Use the podman inspect
command with the --format
option to display individual items from the podman inspect
output.
Prerequisites
-
The
container-tools
meta-package is installed.
Procedure
Display the IP address of a container:
podman inspect --format='{{.NetworkSettings.IPAddress}}' <containerName>
# podman inspect --format='{{.NetworkSettings.IPAddress}}' <containerName>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Display all networks to which container is connected:
podman inspect --format='{{.NetworkSettings.Networks}}' <containerName>
# podman inspect --format='{{.NetworkSettings.Networks}}' <containerName>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Display port mappings:
podman inspect --format='{{.NetworkSettings.Ports}}' <containerName>
# podman inspect --format='{{.NetworkSettings.Ports}}' <containerName>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
9.5. Communicating between a container and an application Copia collegamentoCollegamento copiato negli appunti!
You can communicate between a container and an application. An application ports are in either listening or open state. These ports are automatically exposed to the container network, therefore, you can reach those containers using these networks. By default, the web server listens on port 80. Using this procedure, the myubi
container communicates with the web-container
application.
Prerequisites
-
The
container-tools
meta-package is installed.
Procedure
Start the container named
web-container
:podman run -dt --name=web-container docker.io/library/httpd
# podman run -dt --name=web-container docker.io/library/httpd
Copy to Clipboard Copied! Toggle word wrap Toggle overflow List all containers:
podman ps -a
# podman ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b8c057333513 docker.io/library/httpd:latest httpd-foreground 4 seconds ago Up 5 seconds ago web-container
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Inspect the container and display the IP address:
podman inspect --format='{{.NetworkSettings.IPAddress}}' web-container
# podman inspect --format='{{.NetworkSettings.IPAddress}}' web-container 10.88.0.2
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the
myubi
container and verify that web server is running:podman run -it --name=myubi ubi10/ubi curl 10.88.0.2:80
# podman run -it --name=myubi ubi10/ubi curl 10.88.0.2:80 <html><body><h1>It works!</h1></body></html>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
9.6. Communicating between a container and a host Copia collegamentoCollegamento copiato negli appunti!
By default, the podman
network is a bridge network. It means that a network device is bridging a container network to your host network.
Prerequisites
-
The
container-tools
meta-package is installed. -
The
web-container
is running. For more information, see section Communicating between a container and an application.
Procedure
Verify that the bridge is configured:
podman network inspect podman | grep bridge
# podman network inspect podman | grep bridge "type": "bridge"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Display the host network configuration:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can see that the
web-container
has an IP of thepodman0
subnet and the network is bridged to the host.Inspect the
web-container
and display its IP address:podman inspect --format='{{.NetworkSettings.IPAddress}}' web-container
# podman inspect --format='{{.NetworkSettings.IPAddress}}' web-container 10.88.0.2
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Access the
web-container
directly from the host:curl 10.88.0.2:80
$ curl 10.88.0.2:80 <html><body><h1>It works!</h1></body></html>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
9.7. Communicating between containers using port mapping Copia collegamentoCollegamento copiato negli appunti!
The most convenient way to communicate between two containers is to use published ports. Ports can be published in two ways: automatically or manually.
Prerequisites
-
The
container-tools
meta-package is installed.
Procedure
Run the unpublished container:
podman run -dt --name=web1 ubi10/httpd-24
# podman run -dt --name=web1 ubi10/httpd-24
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the automatically published container:
podman run -dt --name=web2 -P ubi10/httpd-24
# podman run -dt --name=web2 -P ubi10/httpd-24
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the manually published container and publish container port 8080:
podman run -dt --name=web3 -p 8888:8080 ubi10/httpd-24
# podman run -dt --name=web3 -p 8888:8080 ubi10/httpd-24
Copy to Clipboard Copied! Toggle word wrap Toggle overflow List all containers:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can see that:
-
Container
web1
has no published ports and can be reached only by container network or a bridge. Container
web2
has automatically mapped ports 43595 and 42423 to publish the application ports 8080 and 8443, respectively.NoteThe automatic port mapping is possible because the
registry.access.redhat.com/10/httpd-24
image has theEXPOSE 8080
andEXPOSE 8443
commands in the Containerfile.-
Container
web3
has a manually published port. The host port 8888 is mapped to the container port 8080.
-
Container
Display the IP addresses of
web1
andweb3
containers:podman inspect --format='{{.NetworkSettings.IPAddress}}' web1 podman inspect --format='{{.NetworkSettings.IPAddress}}' web3
# podman inspect --format='{{.NetworkSettings.IPAddress}}' web1 # podman inspect --format='{{.NetworkSettings.IPAddress}}' web3
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Reach
web1
container using <IP>:<port> notation:curl 10.88.0.2:8080
# curl 10.88.0.2:8080 ... <title>Test Page for the HTTP Server on Red Hat Enterprise Linux</title> ...
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Reach
web2
container using localhost:<port> notation:curl localhost:43595
# curl localhost:43595 ... <title>Test Page for the HTTP Server on Red Hat Enterprise Linux</title> ...
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Reach
web3
container using <IP>:<port> notation:curl 10.88.0.4:8080
# curl 10.88.0.4:8080 ... <title>Test Page for the HTTP Server on Red Hat Enterprise Linux</title> ...
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
9.8. Communicating between containers using DNS Copia collegamentoCollegamento copiato negli appunti!
When a DNS plugin is enabled, use a container name to address containers.
Prerequisites
-
The
container-tools
meta-package is installed. -
A network with the enabled DNS plugin has been created by using the
podman network create
command.
Procedure
Run a
receiver
container attached to themynet
network:podman run -d --net mynet --name receiver ubi9 sleep 3000
# podman run -d --net mynet --name receiver ubi9 sleep 3000
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run a
sender
container and reach thereceiver
container by its name:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Exit using the
CTRL+C
.
You can see that the sender
container can ping the receiver
container using its name.
9.9. Communicating between two containers in a pod Copia collegamentoCollegamento copiato negli appunti!
All containers in the same pod share the IP addresses, MAC addresses and port mappings. You can communicate between containers in the same pod using localhost:port notation.
Prerequisites
-
The
container-tools
meta-package is installed.
Procedure
Create a pod named
web-pod
:podman pod create --name=web-pod
$ podman pod create --name=web-pod
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the web container named
web-container
in the pod:podman container run -d --pod web-pod --name=web-container docker.io/library/httpd
$ podman container run -d --pod web-pod --name=web-container docker.io/library/httpd
Copy to Clipboard Copied! Toggle word wrap Toggle overflow List all pods and containers associated with them:
podman ps --pod
$ podman ps --pod CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES POD ID PODNAME 58653cf0cf09 k8s.gcr.io/pause:3.5 4 minutes ago Up 3 minutes ago 4e61a300c194-infra 4e61a300c194 web-pod b3f4255afdb3 docker.io/library/httpd:latest httpd-foreground 3 minutes ago Up 3 minutes ago web-container 4e61a300c194 web-pod
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the container in the
web-pod
based on the docker.io/library/fedora image:podman container run -it --rm --pod web-pod docker.io/library/fedora curl localhost
$ podman container run -it --rm --pod web-pod docker.io/library/fedora curl localhost <html><body><h1>It works!</h1></body></html>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can see that the container can reach the
web-container
.
9.10. Communicating in a pod Copia collegamentoCollegamento copiato negli appunti!
You must publish the ports for the container in a pod when a pod is created.
Prerequisites
-
The
container-tools
meta-package is installed.
Procedure
Create a pod named
web-pod
:podman pod create --name=web-pod-publish -p 80:80
# podman pod create --name=web-pod-publish -p 80:80
Copy to Clipboard Copied! Toggle word wrap Toggle overflow List all pods:
podman pod ls
# podman pod ls POD ID NAME STATUS CREATED INFRA ID # OF CONTAINERS 26fe5de43ab3 publish-pod Created 5 seconds ago 7de09076d2b3 1
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the web container named
web-container
inside theweb-pod
:podman container run -d --pod web-pod-publish --name=web-container docker.io/library/httpd
# podman container run -d --pod web-pod-publish --name=web-container docker.io/library/httpd
Copy to Clipboard Copied! Toggle word wrap Toggle overflow List containers
podman ps
# podman ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7de09076d2b3 k8s.gcr.io/pause:3.5 About a minute ago Up 23 seconds ago 0.0.0.0:80->80/tcp 26fe5de43ab3-infra 088befb90e59 docker.io/library/httpd httpd-foreground 23 seconds ago Up 23 seconds ago 0.0.0.0:80->80/tcp web-container
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Verify that the
web-container
can be reached:curl localhost:80
$ curl localhost:80 <html><body><h1>It works!</h1></body></html>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
9.11. Attaching a pod to the container network Copia collegamentoCollegamento copiato negli appunti!
Attach containers in pod to the network during the pod creation.
Prerequisites
-
The
container-tools
meta-package is installed.
Procedure
Create a network named
pod-net
:podman network create pod-net
# podman network create pod-net /etc/cni/net.d/pod-net.conflist
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a pod
web-pod
:podman pod create --net pod-net --name web-pod
# podman pod create --net pod-net --name web-pod
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run a container named
web-container
inside theweb-pod
:podman run -d --pod webt-pod --name=web-container docker.io/library/httpd
# podman run -d --pod webt-pod --name=web-container docker.io/library/httpd
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Display the pods the containers are associated with:
podman ps -p
# podman ps -p CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES POD ID PODNAME b7d6871d018c registry.access.redhat.com/ubi10/pause:latest 9 minutes ago Up 6 minutes ago a8e7360326ba-infra a8e7360326ba web-pod 645835585e24 docker.io/library/httpd:latest httpd-foreground 6 minutes ago Up 6 minutes ago web-container a8e7360326ba web-pod
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Show all networks connected to the container:
podman ps --format="{{.Networks}}"
# podman ps --format="{{.Networks}}" pod-net
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
9.12. Setting HTTP Proxy variables for Podman Copia collegamentoCollegamento copiato negli appunti!
To pull images behind a proxy server, you must set HTTP Proxy variables for Podman. Podman reads the environment variable HTTP_PROXY
to ascertain the HTTP Proxy information. HTTP proxy information can be configured as an environment variable or under /etc/profile.d
.
Procedure
Set proxy variables for Podman. For example:
Unauthenticated proxy:
cat /etc/profile.d/unauthenticated_http_proxy.sh export HTTP_PROXY=http://192.168.0.1:3128 export HTTPS_PROXY=http://192.168.0.1:3128 export NO_PROXY=example.com,172.5.0.0/16
# cat /etc/profile.d/unauthenticated_http_proxy.sh export HTTP_PROXY=http://192.168.0.1:3128 export HTTPS_PROXY=http://192.168.0.1:3128 export NO_PROXY=example.com,172.5.0.0/16
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Authenticated proxy:
cat /etc/profile.d/authenticated_http_proxy.sh export HTTP_PROXY=http://USERNAME:PASSWORD@192.168.0.1:3128 export HTTPS_PROXY=http://USERNAME:PASSWORD@192.168.0.1:3128 export NO_PROXY=example.com,172.5.0.0/16
# cat /etc/profile.d/authenticated_http_proxy.sh export HTTP_PROXY=http://USERNAME:PASSWORD@192.168.0.1:3128 export HTTPS_PROXY=http://USERNAME:PASSWORD@192.168.0.1:3128 export NO_PROXY=example.com,172.5.0.0/16
Copy to Clipboard Copied! Toggle word wrap Toggle overflow