cant connect to mysql database while using laravel on docker(not found,pdo,connection refused)

i wanted to dockerize my laravel app.

i launched two containers of mysql and php like this

sudo docker pull mysql/mysql-server:latest
sudo docker run --name=mysql  -e MYSQL_ROOT_PASSWORD=mypass -d mysql/mysql-server:latest

sudo docker pull phpmyadmin/phpmyadmin:latest
sudo docker run --name phpmyadmin -d --link mysql:db -p 8081:80 phpmyadmin/phpmyadmin

so i configured my user and imported my db manually in phpmyadmin

i launch my laravel container using my docker file etc

#some other code …

EXPOSE 8000
CMD php artisan serve --host=0.0.0.0 --port=8000

my env file

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:aS838/4/6V7n9VQXDUdiQBDxDiM7sWhSc1ym0KahcZY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=mypass

if i keep db_host as 127.0.0.1 i get connection refused error if i give db_host as mysql(which is my running mysql container name) i get pdo errors if i give db_host as localhost i get not found errors i saw on few stackoverflow fixes that u have to give your local network ips instead i dont wanna do it cause i have my own reasons any way around this? i’m stuck on this for 2 days now and im depressed af .any help will be appreciated

Source: Docker Questions

LEAVE A COMMENT