Here’s how to run sql file inside a docker container in a laravel project. But before we start, I assume you already have a dockerize laravel project in your local. If not you may follow my previous tutorial http://safventure.live/dockerize-existing-laravel-project.

Procedure:

  1. Place your sql file in your laravel root folder

Alt Text

  1. Copy the SQL file into the mysql docker container by executing the command below.

Alt Text

docker cp laravel-task_db laravel_mysql:/laravel-task_db

laravel-task_db.sql – sql file

laravel_mysql – name of my mysql container

  1. Launch a mysql bash terminal inside your docker container by executing the commands below.

Alt Text

docker ps – copy the the container_id of laravel_mysql

Alt Text

docker exec -it [mysql_container_id] bash
  1. Login to MySql

Alt Text

mysql -u root -p (default password is password)
show databases;

Alt Text

use laravel;

– laravel is the name of our database

source laravel-task_db.sql

– execute this command to run the sql file

Note: To run sql file in postgre, execute command below:

 psql laravel_mysql < laravel-task_db.sql