Expose containers by using port mapping

You can securely expose specific containerized applications by mapping network ports to your RHEL containers. This enables communication between isolated services and external clients.

Before you begin

  • The container-tools meta-package is installed.

Procedure

  1. Run the unpublished container:
    # podman run -dt --name=web1  ubi10/httpd-24
  2. Run the automatically published container:
    # podman run -dt --name=web2 -P  ubi10/httpd-24
  3. Run the manually published container and publish container port 8080:
    # podman run -dt --name=web3 -p 8888:8080  ubi10/httpd-24
  4. List 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                  web3

    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 33127 and 37679 to publish the application ports 8080 and 8443.

      The automatic port mapping is possible because the registry.access.redhat.com/ubi10/httpd-24:latest image has the EXPOSE 8080 and EXPOSE 8443 commands in the Containerfile.

    • Container web3 has a manually published port. The host port 8888 is mapped to the container port 8080.
  5. Display the IP addresses of web1 and web3 containers:
    # podman inspect --format='{{.NetworkSettings.IPAddress}}' web1
    # podman inspect --format='{{.NetworkSettings.IPAddress}}' web3
  6. Reach web1 container using <IP>:<port> notation:
    # curl 10.88.0.2:8080
    ...
    <title>Test Page for the HTTP Server on Red Hat Enterprise Linux</title>
    ...
  7. Reach web2 container using localhost:<port> notation:
    # curl localhost:43595
    ...
    <title>Test Page for the HTTP Server on Red Hat Enterprise Linux</title>
    ...
  8. Reach web3 container using <IP>:<port> notation:
    # curl 10.88.0.4:8080
    ...
    <title>Test Page for the HTTP Server on Red Hat Enterprise Linux</title>
    ...