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.
-
git clone https://github.com/laravel/laravel.git
-
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
-
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.
-
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:
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)
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
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
it’s probably related to a missing dependency, you should run
composer require doctrine/dbal
Also, you said:
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.
In my Docker-compose.yml, I added this:
In my Laravel Env:
You have to change DB_HOST=mysql to DB_HOST=127.0.0.1
try DB_HOST=host.docker.internal
Inside
docker-compose.yml
Works also.