2

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?

5
  • Why exactly are you using DB_HOST=db? Isn't the simple solution just to do DB_HOST=127.0.0.1 since you're hosting it locally? Commented Jan 15, 2022 at 12:51
  • Because I am getting the same error when I use "127.0.0.1" or "localhost" Commented Jan 15, 2022 at 12:57
  • Hmm, you're also setting MYSQL_DATABASE: mysql but then in your .env you're doing DB_DATABASE=new_cms_system_db. Commented Jan 15, 2022 at 13:05
  • Thank you so much. I am new and learning both Docker and Laravel. I guess I can safely remove 'MYSQL_DATABASE: MySQL' from docker-compose as I work on 'new_cms_system_db'. Am I right? Commented Jan 15, 2022 at 13:16
  • I would in your case set MYSQL_DATABASE: new_cms_system_db Commented Jan 15, 2022 at 13:22

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

But you will still not be able to connect to your database outside of your docker environment (for example if you use tools like PHPMyAdmin or Tableplus etc..) right? which could be awkward when working on your application.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.