10.7. Communicating between containers using port mapping
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-toolsmeta-package is installed.
Procedure
Run the unpublished container:
# podman run -dt --name=web1 ubi10/httpd-24Run the automatically published container:
# podman run -dt --name=web2 -P ubi10/httpd-24Run the manually published container and publish container port 8080:
# podman run -dt --name=web3 -p 8888:8080 ubi10/httpd-24List all containers:
# podman ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES db23e8dabc74 registry.access.redhat.com/ubi10/httpd-24:latest /usr/bin/run-http... 23 seconds ago Up 23 seconds 8080/tcp, 8443/tcp web1 1824b8f0a64b registry.access.redhat.com/ubi10/httpd-24:latest /usr/bin/run-http... 18 seconds ago Up 18 seconds 0.0.0.0:33127->8080/tcp, 0.0.0.0:37679->8443/tcp web2 39de784d917a registry.access.redhat.com/ubi10/httpd-24:latest /usr/bin/run-http... 5 seconds ago Up 5 seconds 0.0.0.0:8888->8080/tcp, 8443/tcp web3You can see that:
-
Container
web1has no published ports and can be reached only by container network or a bridge. Container
web2has automatically mapped ports 43595 and 42423 to publish the application ports 8080 and 8443, respectively.注意The automatic port mapping is possible because the
registry.access.redhat.com/10/httpd-24image has theEXPOSE 8080andEXPOSE 8443commands in the Containerfile.-
Container
web3has a manually published port. The host port 8888 is mapped to the container port 8080.
-
Container
Display the IP addresses of
web1andweb3containers:# podman inspect --format='{{.NetworkSettings.IPAddress}}' web1 # podman inspect --format='{{.NetworkSettings.IPAddress}}' web3Reach
web1container using <IP>:<port> notation:# curl 10.88.0.2:8080 ... <title>Test Page for the HTTP Server on Red Hat Enterprise Linux</title> ...Reach
web2container using localhost:<port> notation:# curl localhost:43595 ... <title>Test Page for the HTTP Server on Red Hat Enterprise Linux</title> ...Reach
web3container using <IP>:<port> notation:# curl 10.88.0.4:8080 ... <title>Test Page for the HTTP Server on Red Hat Enterprise Linux</title> ...