docker container does not open port for php:latest
Try to use php -S 0.0.0.0:8080:

docker run --name php -v $(pwd):/app -w /app -p 8080:8080 -d --rm php:cli php -S 0.0.0.0:8080 -t . index.php


Explanation:

The localhost(or 127.0.0.1) address is attached to the container namespace, so anything outside of this namespace cannot have access to it. Furthermore, the 0.0.0.0 (in this context) is a specific address that means all IPv4 addresses on the local machine. If you check inside the container, you could see:

$ ip a
lo:
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
eth0:
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever


The eth0 interface is the interface used when you forward the port. As you don't specify the IP address of the container, you can use the 0.0.0.0 to listen on the eth0 interface.

I don't know if I make my self clear, but you can (should) read this if you want to know more:


https://pythonspeed.com/articles/docker-connection-refused/
https://superuser.com/questions/949428/whats-the-difference-between-127-0-0-1-and-0-0-0-0