Answers: <p>Ok, I have found the reason:</p>
  
  <p>I tested starting the services with different environment variables for db. And after that I shut the services down, changed the db name, username and password, and started the services again. </p>
  
  <p>But I forgot that db folder <code>/var/lib/mysql</code> is mounted in a volume, and it remembered my previous data; everything was mixed up. So it's not problem in network, it's mixed data in mysql (different passwords for the same user).</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-08-14T12:12:36.870Z
Answers: 2019-09-13T16:03:37.653Z
Answers: 2019-09-13T16:03:37.653Z
Answers: Log
Body: <p>I have web-app with PHP and database in docker-compose file:</p>
  
  <pre class="lang-yml prettyprint-override"><code>version: '2'
  
  networks:
    default:
      driver: bridge
  
  services:
  
    yii2-app:
      build:
        context: ./web
      ports:
        - "80:80"
      networks:
        - default
      depends_on:
        - yii2-db
        - queue
      environment:
        DB_NAME: ${WEB_DB_NAME}
        DB_USERNAME: ${WEB_DB_USERNAME}
        DB_PASSWORD: ${WEB_DB_PASSWORD}
  
    yii2-db:
      image: mysql:5.7.16
      command: --default-authentication-plugin=mysql_native_password
      networks:
        - default
      expose:
        - "3306"
      environment:
        MYSQL_ROOT_PASSWORD: ${WEB_DB_PASSWORD}
        MYSQL_DATABASE: ${WEB_DB_NAME}
      volumes:
        - "./web/db:/var/lib/mysql"
    ...
  </code></pre>
  
  <p>When I start services via <code>docker-compose up</code> my PHP-app makes db migrations. But this error occured in database connection:</p>
  
  <pre><code>Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [1045] Access denied for user 'root'@'172.21.0.8' (using password: YES)'
  </code></pre>
  
  <p>It's strange, because in my PHP-app I use name of db service <code>yii2-db</code> and docker by itself to define the correct IP.</p>
  
  <p>I connect to my PHP-app container via </p>
  
  <pre><code>docker exec -it passportrecognition_yii2-app_1 bash
  </code></pre>
  
  <p>and check mysql connection via mysql-client</p>
  
  <pre><code>mysql -h yii2-db -u root -p
  ERROR 1045 (28000): Access denied for user 'root'@'172.21.0.8' (using password: YES)
  </code></pre>
  
  <p>But <code>172.21.0.8</code> is not the IP of my db, it's the IP of my PHP-app. That is PHP-app make db-connect to itself.</p>
  
  <p>Database IP is <code>172.21.0.4</code> as shown with this <code>ping</code> command:</p>
  
  <pre><code>ping yii2-db
  PING yii2-db (172.21.0.4): 56 data bytes
  64 bytes from 172.21.0.4: seq=0 ttl=64 time=0.145 ms
  </code></pre>
  
  <p>And another strange moment: when I connect directly on database IP <code>172.21.0.4</code>, the following error occurs:</p>
  
  <pre><code>bash-4.4# mysql -h 172.21.0.4 -u root -p
  Enter password: 
  ERROR 1045 (28000): Access denied for user 'root'@'172.21.0.8' (using password: YES)
  </code></pre>
  
  <p>Why can't I connect to my database? Why does Docker define an invalid IP for the db service?</p>
  
Tags: <docker><docker-compose>
Title: Why does Docker define invalid IP address of service?
UserDisplayName: Log
ThreadText: # Why does Docker define invalid IP address of service?
  
  (Asked by: username_0 on 2019-08-14)
  
  I have web-app with PHP and database in docker-compose file:
  
  ```yml
  version: '2'
  
  networks:
    default:
      driver: bridge
  
  services:
  
    yii2-app:
      build:
        context: ./web
      ports:
        - "80:80"
      networks:
        - default
      depends_on:
        - yii2-db
        - queue
      environment:
        DB_NAME: ${WEB_DB_NAME}
        DB_USERNAME: ${WEB_DB_USERNAME}
        DB_PASSWORD: ${WEB_DB_PASSWORD}
  
    yii2-db:
      image: mysql:5.7.16
      command: --default-authentication-plugin=mysql_native_password
      networks:
        - default
      expose:
        - "3306"
      environment:
        MYSQL_ROOT_PASSWORD: ${WEB_DB_PASSWORD}
        MYSQL_DATABASE: ${WEB_DB_NAME}
      volumes:
        - "./web/db:/var/lib/mysql"
    ...
  
  ```
  
  When I start services via `docker-compose up` my PHP-app makes db migrations. But this error occured in database connection:
  
  ```
  Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [1045] Access denied for user 'root'@'172.21.0.8' (using password: YES)'
  
  ```
  
  It's strange, because in my PHP-app I use name of db service `yii2-db` and docker by itself to define the correct IP.
  
  I connect to my PHP-app container via
  
  ```
  docker exec -it passportrecognition_yii2-app_1 bash
  
  ```
  
  and check mysql connection via mysql-client
  
  ```
  mysql -h yii2-db -u root -p
  ERROR 1045 (28000): Access denied for user 'root'@'172.21.0.8' (using password: YES)
  
  ```
  
  But `172.21.0.8` is not the IP of my db, it's the IP of my PHP-app. That is PHP-app make db-connect to itself.
  
  Database IP is `172.21.0.4` as shown with this `ping` command:
  
  ```
  ping yii2-db
  PING yii2-db (172.21.0.4): 56 data bytes
  64 bytes from 172.21.0.4: seq=0 ttl=64 time=0.145 ms
  
  ```
  
  And another strange moment: when I connect directly on database IP `172.21.0.4`, the following error occurs:
  
  ```
  bash-4.4# mysql -h 172.21.0.4 -u root -p
  Enter password: 
  ERROR 1045 (28000): Access denied for user 'root'@'172.21.0.8' (using password: YES)
  
  ```
  
  Why can't I connect to my database? Why does Docker define an invalid IP for the db service?
  
  **Tags:** docker, docker-compose
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-08-14. Score: 1
  
  Ok, I have found the reason:
  
  I tested starting the services with different environment variables for db. And after that I shut the services down, changed the db name, username and password, and started the services again.
  
  But I forgot that db folder `/var/lib/mysql` is mounted in a volume, and it remembered my previous data; everything was mixed up. So it's not problem in network, it's mixed data in mysql (different passwords for the same user).
  
  
  ---
  
  