skip to Main Content

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:

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

 IlluminateDatabaseQueryException  : 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

3

Answers


  1. 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

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

    IlluminateDatabaseQueryException : 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.

    Login or Signup to reply.
  2. In my Docker-compose.yml, I added this:

    phpmyadmin:
            image: 'phpmyadmin/phpmyadmin'
            links:
                - mysql:mysql
            ports:
                - 8080:80
            environment:
                MYSQL_USERNAME: "${DB_USERNAME}"
                MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
                PMA_HOST: mysql
            networks:
                - sail
    

    In my Laravel Env:

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=teram_a_notificationdemo
    DB_USERNAME=sail
    DB_PASSWORD=password
    

    You have to change DB_HOST=mysql to DB_HOST=127.0.0.1

    Login or Signup to reply.
  3. try DB_HOST=host.docker.internal


    Inside docker-compose.yml

     environment:
          - DB_HOST=host.docker.internal
    

    Works also.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search