Bookmark this page

Accessing Containerized Network Services

Objectives

  • Expose ports to access containerized services.

Port Forwarding

A container's network namespace is isolated, which means that a networked application is only accessible within the container. Port forwarding maps a port from the host machine where the container runs to a port inside of a container.

The -p option of the podman run command forwards a port. The option accepts the form HOST_PORT:CONTAINER_PORT.

For example, the following command maps port 80 inside the container to port 8075 on the host machine.

[user@host ~]$ podman run -p 8075:80 my-app

Without a host specified, the container is assigned the broadcast address (0.0.0.0). This means that the container is accessible from all networks on the host machine.

To publish a container to a specific host and to limit the networks it is accessible from, use the following form.

[user@host ~]$ podman run -p 127.0.0.1:8075:80 my-app

Port 80 in the my-app container is available from port 8075 only from the host machine, which is accessible via the localhost 127.0.0.1 IP address.

List Port Mappings

To list port mappings for a container, use the podman port command. For example, the following command reveals that port 8010 of the host machine is mapped to port 8008 within the container.

[user@host ~]$ podman port my-app
8008/tcp -> 0.0.0.0:8010

The --all option lists port mappings for all containers.

[user@host ~]$ podman port --all
1aacd9cf1c76	8008/tcp -> 0.0.0.0:8010

Note

In the preceding example output, 1aacd9cf1c76 refers to the ID of the container.

Networking in Containers

Containers attached to Podman networks are assigned private IP addresses for each network. Other containers in the network can make requests to this IP address.

For example, a container called my-app is attached to the apps network. The following command retrieves the private IP address of the container within the apps network.

[user@host ~]$ podman inspect my-app \
 -f '{{.NetworkSettings.Networks.apps.IPAddress}}'
10.89.0.2

Note that this IP address is only valid within the apps network.

Revision: do188-4.14-8c43a16