mardi 25 décembre 2018

Laravel with docker issue connecting MySQL

Need someone's help who is expert in docker and laravel.

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 3 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 boooom.

enter image description here

I'm able to see laravel home page.

at this point all is good not have any problem. problem starts now when I'm connecting to my DB.

next command to run.(I'm still within container)

php artisan migrate

and the error is:

 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)

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

Any help would be appreciate.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2s2nSHX
via IFTTT

Aucun commentaire:

Enregistrer un commentaire