Answers: <p>Try to use <code>php -S 0.0.0.0:8080</code>:</p>
  
  <pre><code>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
  </code></pre>
  
  <p><em>Explanation</em>:</p>
  
  <p>The <code>localhost</code>(or <code>127.0.0.1</code>) address is attached to the container namespace, so anything outside of this namespace cannot have access to it. Furthermore, the <code>0.0.0.0</code> (in this context) is a specific address that means <em>all IPv4 addresses on the local machine</em>. If you check inside the container, you could see:</p>
  
  <pre><code>$ 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
  </code></pre>
  
  <p>The <code>eth0</code> 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 <code>0.0.0.0</code> to listen on the <code>eth0</code> interface.</p>
  
  <p>I don't know if I make my self clear, but you can (should) read this if you want to know more:</p>
  
  <ul>
  <li><a href="https://pythonspeed.com/articles/docker-connection-refused/" rel="nofollow noreferrer">https://pythonspeed.com/articles/docker-connection-refused/</a></li>
  <li><a href="https://superuser.com/questions/949428/whats-the-difference-between-127-0-0-1-and-0-0-0-0">https://superuser.com/questions/949428/whats-the-difference-between-127-0-0-1-and-0-0-0-0</a></li>
  </ul>
  
Answers: CC BY-SA 4.0
Answers: 2019-11-25T13:49:19.150Z
Answers: Could you explain why this could help?
Answers: 030
Answers: CC BY-SA 4.0
Answers: 2019-11-25T14:04:34.753Z
Answers: 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`.
Answers: Chris Pillen
Answers: CC BY-SA 4.0
Answers: 2019-11-25T14:05:59.117Z
Answers: (works, by the way)
Answers: Chris Pillen
Answers: CC BY-SA 4.0
Answers: 2019-11-25T10:35:21.513Z
Answers: 2019-11-25T14:47:02.223Z
Answers: 2019-11-25T14:47:02.223Z
Answers: Hedi Nasr
Body: <p>I'm working with Docker containers for a while know, but I do not notice, what I am doing wrong here:</p>
  
  <p>In my current folder on the host system, I have a simple <code>index.php</code> file which contains:</p>
  
  <pre><code>&lt;?php echo 'hi'; ?&gt;
  </code></pre>
  
  <p>Why is this not working?</p>
  
  <pre><code>docker run --name php -v $(pwd):/app -w /app -p 8080:8080 -d --rm php:cli php -S localhost:8080 -t . index.php
  </code></pre>
  
  <p>This results in an empty reply:</p>
  
  <pre><code>$ curl localhost:8080                                                                                                                                             
  curl: (52) Empty reply from server
  </code></pre>
  
  <p>If I <code>exec</code> in the container and apply the <code>curl</code> there it succeeds in replying <code>hi</code>.</p>
  
  <p>So, what am I missing?</p>
  
Tags: <docker><php>
Title: docker container does not open port for php:latest
UserDisplayName: Chris Pillen
ThreadText: # docker container does not open port for php:latest
  
  (Asked by: username_0 on 2019-11-25)
  
  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?
  
  **Tags:** docker, php
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-11-25. Score: 2 | Accepted answer
  
  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>
  
  > **Comments:**
  > **username_2**: Could you explain why this could help?  
  > **username_0**: 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`.  
  > **username_0**: (works, by the way)  
  
  ---
  
  