title: How to solve Access denied for user 'root'@ when connect Laravel with mysql through docker
body: <p>Im trying to deploy my laravel app using docker. Then I created docker-compose.yml file and Dockerfile like below.</p>
  <p>docker-compose.yml</p>
  <pre><code>version: &quot;3.7&quot;
  services:
      app:
          build:
              args:
                  user: sammy
                  uid: 1000
              context: ./
              dockerfile: Dockerfile
          image: travellist
          container_name: travellist-app
          restart: unless-stopped
          working_dir: /var/www/
          volumes:
              - ./:/var/www
          networks:
              - travellist
  
      db:
          image: 'mysql:latest'
          restart: 'unless-stopped'
          ports:
              - '3306:3306'
          environment:
              - 'MYSQL_RANDOM_ROOT_PASSWORD=true'
              - 'MYSQL_DATABASE=${DB_DATABASE}'
              - 'MYSQL_USER=${DB_USER}'
              - 'MYSQL_PASSWORD=${DB_PASSWORD}'
              - 'MYSQL_ALLOW_EMPTY_PASSWORD=true'
          volumes:
              - 'laravel-database:/var/lib/mysql/'
          networks:
              - 'travellist'
  
      nginx:
          image: nginx:alpine
          container_name: travellist-nginx
          restart: unless-stopped
          ports:
              - 8000:80
          volumes:
              - ./:/var/www
              - ./docker-compose/nginx:/etc/nginx/conf.d/
          networks:
              - travellist
  
  networks:
      travellist:
          driver: bridge
  
  volumes:
      laravel-database:
  </code></pre>
  <p>Dockerfile</p>
  <pre><code>FROM php:7.4-fpm
  
  # Arguments defined in docker-compose.yml
  ARG user
  ARG uid
  
  # Install system dependencies
  RUN apt-get update &amp;&amp; apt-get install -y \
      git \
      curl \
      libpng-dev \
      libonig-dev \
      libxml2-dev \
      zip \
      unzip
  
  # Clear cache
  RUN apt-get clean &amp;&amp; rm -rf /var/lib/apt/lists/*
  
  # Install PHP extensions
  RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
  
  # Get latest Composer
  COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
  
  # Create system user to run Composer and Artisan Commands
  RUN useradd -G www-data,root -u $uid -d /home/$user $user
  RUN mkdir -p /home/$user/.composer &amp;&amp; \
      chown -R $user:$user /home/$user
  
  # Set working directory
  WORKDIR /var/www
  
  USER $user
  </code></pre>
  <p>And when I try docker-compose up, all the containers run with a any error. And also when I run docker-compose ps, it shows like this,</p>
  <pre><code>      Name                    Command               State                          Ports
  ---------------------------------------------------------------------------------------------------------------
  proj_db_1          docker-entrypoint.sh mysqld      Up      0.0.0.0:3306-&gt;3306/tcp,:::3306-&gt;3306/tcp, 33060/tcp
  travellist-app     docker-php-entrypoint php-fpm    Up      9000/tcp
  travellist-nginx   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:8000-&gt;80/tcp,:::8000-&gt;80/tcp
  </code></pre>
  <p>But, when laravel application, tries to connect with mysql, it doesnt happens. And even I cant connect using TablePlus, it shows this.</p>
  <pre><code>Lost connection to MySQL server at 'waiting for initial communication packet', system error: 10060
  </code></pre>
  <p>And laravel application shows this below error..</p>
  <pre><code>local.ERROR: SQLSTATE[HY000] [1045] Access denied for user 'root'@'172.19.0.3' (using password: YES) 
  </code></pre>
  <p>Here is my .env</p>
  <pre><code>DB_CONNECTION=mysql
  DB_HOST=db
  DB_PORT=3306
  DB_DATABASE=here_goes_db_name
  DB_USERNAME=root
  DB_PASSWORD=tyutyu
  </code></pre>
  <p>How can I fix and connect my laravel application and mysql ?? And also how can I log into table plus ??</p>