Helcaraxë
Helcaraxë

Reputation: 31

Laravel env configuration on Docker using MySQL

I don't know the technical terms of the LAMP stack very well so I will try to explain myself as much as I can. I have my project in my local Ubuntu (running on Windows 10 pro). I ran docker from WSL and I edited the default Laravel Sail docker-compose file.

my env file is like this:

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=new_cms_system_db
DB_USERNAME=root
DB_PASSWORD=

and this is my docker-compose MySQL part:

db:
    image: 'mysql:5.7'
    container_name: db
    ports:
        - '3306:3306'
    environment:
        MYSQL_DATABASE: mysql
        MYSQL_ALLOW_EMPTY_PASSWORD: 1
        MYSQL_USER: sail
        MYSQL_PASSWORD: 123
    volumes:
        - 'sailmysql:/var/lib/mysql'
    networks:
        - sail

if I use "DB_HOST" as "db," then I have got no problems reaching the database from the website but then I can't use "PHP artisan migrate" as I get SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known error.

So I change "DB_HOST" to 0.0.0.0, then I can migrate but I can't reach the database from the website.

What should I do to solve this problem so I can both reach the database from the terminal and the website?

Upvotes: 2

Views: 2522

Answers (1)

Helcaraxë
Helcaraxë

Reputation: 31

I solved my problem with the following command:

docker exec -it <name of container> php artisan migrate

In this case, I used my main container's name and it worked. I kept DB_HOST as "db" as I think it should be.

I can reach my database from PHPMyAdmin. I added it to docker-compose like this:

phpmyadmin:
    image: phpmyadmin
    container_name: phpmyadmin
    ports:
        - 8200:80
    environment:
        PMA_HOST: db
        MYSQL_USER: root
    networks:
        - sail

Upvotes: 1

Related Questions