Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel with Docker issue connecting MySQL

Steps I follow to setup Laravel using Docker: in my local system I don't have installed PHP, Composer, Apache, MySQL, phpMyAdmin, etc. I only have Git and Docker install in my system.

  1. git clone https://github.com/laravel/laravel.git

  2. create docker-composer.yml file on project root.

    version: "3"
    services:
      db:
      image: mysql:5.7
      environment:
        MYSQL_ROOT_PASSWORD: pass
        MYSQL_DATABASE: db
        MYSQL_USER: root
        MYSQL_PASSWORD: pass
      ports:
        - "3306:3306"
    
    web:
      image: php:7.2.2-apache
      container_name: web_laravel
      depends_on:
        - db
      volumes:
        - ./:/var/www/html/
      ports:
        - "4000:80"
      stdin_open: true
      tty: true
    
    phpmyadmin:
      image: phpmyadmin/phpmyadmin
      depends_on:
        - db
      external_links:
        - db:mysql
      ports:
        - "9191:80"
      environment:
        MYSQL_USER: root
        MYSQL_PASSWORD: pass
        MYSQL_ROOT_PASSWORD: pass
        PMA_HOST: db
    
  3. run command from project root.

    docker-compose up
    

    This command will fetch all the images (php:7.2.2-apache, phpmyadmin/phpmyadmin, mysql:5.7) from local cache or Docker Hub and start three containers for these images.

    Now I need to interact with php:7.2.2-apache image's container called web_laravel (see in yml file) so I can add PHP extensions and Composer to run Laravel project.

  4. run this command.

    docker exec -it web_laravel /bin/bash
    

    Now I have access to run any command in running web_laravel container so I've installed Composer and PHP extensions like mbstrings, pdo, pdo_mysql etc.

    Then install Laravel dependency using composer install, set permission for storage and bootstrap/cache folders and run php artisan key:generate.

    open localhost:4000 and I'm able to see Laravel home page:

    enter image description here

At this point all is good. The problem starts now when I'm connecting to my DB.

Next command to run (I'm still within container):

  php artisan migrate

and the errors are:

 Illuminate\Database\QueryException  : SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = blog and table_name = migrations)

 Illuminate\Database\QueryException  : could not find driver (SQL: select * from information_schema.tables where table_schema = blog and table_name = migrations)

enter image description here

enter image description here

I'm able to open phpMyAdmin (http://localhost:9191) and can create DB, table and operations related DB. I've DB called blog.

MySQL env variables:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=pass
like image 471
Dhaval Avatar asked Dec 25 '18 10:12

Dhaval


People also ask

How do I connect to a MySQL Docker container?

Here are the steps you can follow to install the Dockerhub MySQL Container: Step 1: Pull the Docker Image for MySQL. Step 2: Deploy and Start the MySQL Container. Step 3: Connect with the Docker MySQL Container.

Does laravel work with MySQL?

Currently Laravel supports four database systems: MySQL, Postgres, SQLite, and SQL Server.

How do I run phpMyAdmin and Docker in MySQL?

You only need to open your favourite browser and type the following url: http://localhost:8081/ so your instance of phpMyAdmin will show up. To access, type root as username and the password you established in the step one when running the mysql container (if you followed the tutorial the password is mypass123).


1 Answers

I think you have a couple of issues, the first is that laravel .env configuration should point to the MySQL container, not localhost

DB_HOST=127.0.0.1

should be

DB_HOST=db

And the other error you talked about, is not related to Docker

Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = blog and table_name = migrations)

Illuminate\Database\QueryException : could not find driver (SQL: select * from information_schema.tables where table_schema = blog and table_name = migrations)

it's probably related to a missing dependency, you should run

composer require doctrine/dbal

Also, you said:

now I have access to run any command in running web_laravel container so I've installed composer and php extensions like mbstrings, pdo, pdo_mysql etc.

You should build your image on top of PHP 7/Apache image, and add those to the build dockerfile, because your changes (php extensions, configurations...etc) are not persistent. I would suggest you use Laradock or any other existing Laravel/docker environment.

like image 50
Amin Avatar answered Sep 28 '22 10:09

Amin