Laravel fails to connect to database while in Docker container

Solution 1:

After some research and thinking about it, the problem was that I was not running the migrate command from within the container but from the outside.

Here is what I did:

  1. (Within VS Code) docker ps
  2. docker exec -it finance_app_backend_1 bash, result: root@1bb6f2d30d8a:/var/www/html#
  3. cd finance_app/
  4. root@1bb6f2d30d8a:/var/www/html/finance_app# php artisan migrate

And it worked

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.07 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.06 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.03 seconds)
Migrating: 2019_11_25_012607_create_cateagories_table
Migrated:  2019_11_25_012607_create_cateagories_table (0.04 seconds)
root@1bb6f2d30d8a:/var/www/html/finance_app#