title: MySQL Access denied for Laravel on Docker
body: <p>I want to dockerize my laravel project and have done the configurations but whenever i run <code>php artisan migrate</code> command, i get this error</p>
  
  <p><code>SQLSTATE[HY000] [1045] Access denied for user 'admin'@'172.16.238.10' (using password: YES) (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')</code></p>
  
  <p>Clearly it says access is denied for the admin user but I can't really traced where the error is.</p>
  
  <p>Below are the configuration file:</p>
  
  <p>app.dockerfile:</p>
  
  <pre><code>FROM php:7.2-apache
  
  COPY composer.lock /var/www
  
  COPY composer.json /var/www
  
  WORKDIR /var/www
  
  COPY database /var/www/database
  
  RUN apt-get update &amp;&amp; apt-get install -y git gnupg &amp;&amp; \
      curl -sL https://deb.nodesource.com/setup_10.x | bash - &amp;&amp; \
      apt-get install -y nodejs
  
  RUN apt-get update &amp;&amp; apt-get install -y \
      build-essential \
      libpng-dev \
      libjpeg62-turbo-dev \
      libfreetype6-dev \
      locales \
      zip \
      nano \
      vim \
      openssl \
      git \
      curl\
      unzip 
  
  RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
  
  RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
  
  RUN docker-php-ext-install gd
  
  RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
      &amp;&amp; php composer-setup.php \
      &amp;&amp; php -r "unlink('composer-setup.php');" \
      &amp;&amp; php composer.phar install --no-dev --no-scripts \
      &amp;&amp; rm composer.phar
  
  COPY . /var/www
  
  RUN chown -R www-data:www-data \
          /var/www/storage \
          /var/www/bootstrap/cache \
          /var/www/public
  
  RUN  apt-get install -y libmcrypt-dev \
          libmagickwand-dev --no-install-recommends \
          &amp;&amp; pecl install mcrypt-1.0.2 \
          &amp;&amp; docker-php-ext-install pdo_mysql \
          &amp;&amp; docker-php-ext-enable mcrypt
  
  COPY laravel.conf /etc/apache2/sites-available
  
  RUN ln -s /etc/apache2/sites-available/laravel.conf /etc/apache2/sites-enabled/
  
  RUN a2dissite 000-default.conf
  
  RUN a2ensite laravel.conf
  
  RUN a2enmod rewrite
  
  RUN service apache2 restart
  
  CMD php artisan serve --port=8000
  
  EXPOSE 8000
  </code></pre>
  
  <p>docker-compose.yml:</p>
  
  <pre><code>version: '3'
  
  services: 
  
       # The Database
      database:
          container_name: mysql_database
          image: mysql:5.7
          restart: always
          volumes:
              - dbdata:/var/lib/mysql
          environment:
              - "MYSQL_DATABASE=laravel"
              - "MYSQL_USER=laravel"
              - "MYSQL_PASSWORD=admin"
              - "MYSQL_ROOT_PASSWORD=admin"
          ports:
              - 3306:3306
          networks:
              myapp_net: 
                  ipv4_address: 172.16.238.11
  
      # The Application
      app:
          container_name: laraveldocker
          build:
              context: ./
              dockerfile: app.dockerfile
          restart: always
          volumes:
              - ./storage:/var/www/storage
          env_file: '.env'
          environment:
              - "DB_HOST=database"
              - "REDIS_HOST=cache"
          depends_on:
              - database
          ports:
              - "8000:8000"
          networks:
              myapp_net: 
                  ipv4_address: 172.16.238.10
  
  
      # redis
      cache:
          image: redis:3.0-alpine
  
  volumes:
      dbdata:
  
  networks:
      myapp_net:
          driver: bridge
          ipam:
              driver: default
              config:
                  - subnet: 172.16.238.0/24
  </code></pre>
  
  <p>laravel.conf: </p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
      ServerName 172.16.238.10
  
      ServerAdmin webmaster@localhost
      DocumentRoot /var/www/public
  
      &lt;Directory /var/www/&gt;
          AllowOverride All
      &lt;/Directory&gt;
  
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>.env:</p>
  
  <pre><code>APP_NAME=Laravel
  APP_ENV=local
  APP_KEY=base64:yS7v+khzJ1GxLVAtpyWd0gG/5SOwoZr/wsXH2h/VH2o=
  APP_DEBUG=true
  APP_URL=http://localhost
  
  LOG_CHANNEL=stack
  
  DB_CONNECTION=mysql
  DB_HOST=172.16.238.11
  DB_PORT=3306
  DB_DATABASE=laravel
  DB_USERNAME=admin
  DB_PASSWORD=admin
  
  BROADCAST_DRIVER=log
  CACHE_DRIVER=file
  QUEUE_CONNECTION=sync
  SESSION_DRIVER=file
  SESSION_LIFETIME=120
  
  REDIS_HOST=127.0.0.1
  REDIS_PASSWORD=null
  REDIS_PORT=6379
  
  AWS_ACCESS_KEY_ID=
  AWS_SECRET_ACCESS_KEY=
  AWS_DEFAULT_REGION=us-east-1
  AWS_BUCKET=
  
  PUSHER_APP_ID=
  PUSHER_APP_KEY=
  PUSHER_APP_SECRET=
  PUSHER_APP_CLUSTER=mt1
  
  MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
  MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
  
  </code></pre>
  
  <p>Is there anything I'm doing wrong?</p>