1

I'm working with Docker containers for a while know, but I do not notice, what I am doing wrong here:

In my current folder on the host system, I have a simple index.php file which contains:

<?php echo 'hi'; ?>

Why is this not working?

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

This results in an empty reply:

$ curl localhost:8080                                                                                                                                             
curl: (52) Empty reply from server

If I exec in the container and apply the curl there it succeeds in replying hi.

So, what am I missing?

Chris Pillen
  • 143
  • 3

1 Answers1

2

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:

Hedi Nasr
  • 746
  • 3
  • 7