title: How to connect Docker PHP container to MySql container?
body: <p>I have created a docker container with <code>MySQL</code>:</p>
  
  <pre><code>docker run --name mysqlfordocker -p3307:3306 -e MYSQL_ROOT_PASSWORD=password_db-d mysql
  </code></pre>
  
  <p>It works as expected.</p>
  
  <p>It is used by  <code>MySQLWorckbech</code> and <code>PHP</code> application which are not in the container with <code>MySQL</code>:</p>
  
  <pre><code>//CONNECTION BY PHP APPLICATION NOT IN CONTAINER
  $servername = "127.0.0.1:3307";
  $username_db = "username_db";
  $password_db = "password_db";
  $db = "db";
  </code></pre>
  
  <p>The <code>Dockerfile</code> for my <code>PHP</code> application is:</p>
  
  <pre><code>FROM php:7.0-apache
  
  RUN apt-get update \
    &amp;&amp; apt-get install -y --no-install-recommends libpq-dev \
    &amp;&amp; docker-php-ext-install mysqli pdo_pgsql pdo_mysql
  
  COPY / /var/www/html
  EXPOSE 80
  </code></pre>
  
  <p>Then I created a container with my <code>PHP application</code>:</p>
  
  <pre><code>docker run –d –p 5000:80 myphpapplication
  </code></pre>
  
  <p>The <code>PHP application</code> runs, but it has issues with database connection to <code>MySQL</code> container.</p>
  
  <p>I tried to configure a connection <code>servername</code> in a different way but without any luck:</p>
  
  <pre><code>$servername = "127.0.0.1:3307";
  $servername = "locahosto:3307";
  $servername = "10.0.75.1:3307";  //DockerNAT
  $servername = "172.17.0.2:3307"; //Ip by docker inspect mysql
  </code></pre>
  
  <p>Could you please help me on this?</p>
  
  <p>Ps.
  <a href="https://i.stack.imgur.com/pdXEp.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pdXEp.jpg" alt="enter image description here"></a></p>