0

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

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:

| improve this answer | |
  • Could you explain why this could help? – 030 Nov 25 '19 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. – Chris Pillen Nov 25 '19 at 14:04
  • (works, by the way) – Chris Pillen Nov 25 '19 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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