title: Docker connect to localhost
body: <p>I have some simple code that doesn't work when running on a docker container.</p>
  
  <pre class="lang-php prettyprint-override"><code>    $h = curl_init();
      $header = [
          'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0',
          'Referer: https://shoots.video',
      ];
      $url = str_replace("localhost:8080","localhost",$url);
      curl_setopt($h, CURLOPT_HTTPHEADER, $header);
      curl_setopt($h, CURLOPT_URL, $url);
      curl_setopt($h, CURLOPT_SSL_VERIFYHOST,false);     
      curl_setopt($h, CURLOPT_SSL_VERIFYPEER,false);
      curl_setopt($h, CURLOPT_RETURNTRANSFER,true);
      $content = curl_exec($h);
      if ( curl_errno($h) ) {
          echo "Curl Error: " . curl_error($h);
      }
      curl_close($h);
      echo $content;
      var_dump($content);
      echo file_get_contents($url);
  </code></pre>
  
  <p>Above I am trying both curl and file_get_contents. I simply want to connect to the apache instance running on the docker container to load a webpage. </p>
  
  <p>What I get is:</p>
  
  <pre><code>string(0) ""
  
  Warning: file_get_contents(http://localhost/flat-listings/?search_location=&amp;search_radius=50&amp;per_page=25&amp;page=1&amp;order=desc&amp;orderby=rating-desc): failed to open stream: Cannot assign requested address
  </code></pre>
  
  <p>The var_dump for the curl request is blank.</p>
  
  <p>And file_get_contents throws a warning. </p>
  
  <p>I have tried variations host.docker.internal, 127.0.0.1, $_SERVER['SERVER_ADDR'] all with varying failures.</p>
  
  <p>The containers are run from a docker-compose file, so if there's something I can put in the docker-compose.yml to fix it, that's cool.</p>
  
  <p>Please understand, I want 100% localhost behavior. Nothing else. I want to connect to the container, from inside the container. I can't seem to figure out why this doesn't work? I don't want to connect to mysql, or something else. It's just a php script that connects back to the HTTP server with a request for another page on the same "website".</p>
  
  <p>I am using Docker on linux.</p>
  
  <p>Here is my docker-compose.yml</p>
  
  <pre><code>version: '3.3'
  services:
    mysql: 
      image: mariadb
      container_name: shootsmysql
      volumes:
        - vshootsdb:/var/lib/mysql
      environment:
        MYSQL_ROOT_PASSWORD: xxxx
        MYSQL_DATABASE: wordpress1
        MYSQL_USER: wordy
        MYSQL_PASSWORD: xxxx
      restart: always
      ports:
        - '3306:3306'
      expose:
        - '3306'
    wordpress:
      image: andreccosta/wordpress-xdebug
      container_name: shootswordpress
      volumes:
        - vshootswp:/var/www/html/
      depends_on:
        - mysql
      environment:
        WORDPRESS_DB_HOST: mysql:3306
        WORDPRESS_DB_NAME: wordpress1
        WORDPRESS_DB_USER: wordy
        WORDPRESS_DB_PASSWORD: xxxx
        WORDPRESS_TABLE_PREFIX: wp1_
      links:
        - mysql
      restart: always
      ports:
        - "80:80"
      expose:
        - '80'
  volumes:
    vshootsdb: 
    vshootswp: 
      driver: local
      driver_opts:
        o: bind
        type: none
        device: /var/www/shoots/wordpress/
  </code></pre>
  
  <p>I have already looked at several possible solutions (including from SO), and either a) I couldn't get them to work or b) I didn't understand the answer well enough to implement it.</p>
  
  <p>So I am looking for a bit more info than just: "Pass -network=host like the docs say". I have tried adding network_mode: "host" to the wordpress definition, to no joy.</p>