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?

1 Answer 1


Try to use php -S

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


The localhost(or address is attached to the container namespace, so anything outside of this namespace cannot have access to it. Furthermore, the (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
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet scope host lo
       valid_lft forever preferred_lft forever
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet brd 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 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:

  • Could you explain why this could help?
    – 030
    Nov 25, 2019 at 13:49
  • Thank you. Did not try it yet. But can you explain, why localhost will not work? I mean, I thought 8080:8080 will map all incomming traffic on port 8080 to localhost's port 8080. Nov 25, 2019 at 14:04
  • (works, by the way) Nov 25, 2019 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.