Skip to main content
Mohammad Hassani

Import database to docker with terminal

if you are developing with Linux and wanna import database to docker with terminal you can use this tutorial

Hey, this is Mohammad Hassani :smiley:

if you are developing with Linux and wanna import database to docker with terminal you can use this tutorial...

###It's just 4 steps

#1) copy database to _data folder: first of all you should copy your .sql file to var/lib/docker/volumes/blog_blogmysql/_data

in terminal:

cp DATABASE.sql var/lib/docker/volumes/blog_blogmysql/_data

// Replace DATABASE to your database file name

#2) execute docker now your database is ready to import, in this step execute docker in terminal, (docker desktop is not supported in Linux but you can install dockstation for Linux, they are similar. and then you can execute there)

also in terminal:

docker exec -it CONTAINERNAME bash

// Replace CONTAINERNAME to your container name like site_mysql_1

#3) Bring up mysql now open mysql in terminal:

mysql -uroot -p DATABASENAME
  • if you dont have database in container you can use
mysql -uroot -p

and then:

CREATE DATABASE DATABASENAME;

// Replace DATABASENAME to your database name

#4) source .sql file ok! everything is set now and ready to go... in terminal:

source var/lib/mysql/DATABASE.sql

it maybe take some time depends on your .sql file size and your computer.

better way

  • you can use this code instead of step 3 and 4:
mysql --init-command="set autocommit=0" DATABASENAME < var/lib/mysql/DATABASE.sql

that's it!

if you have another way, leave a message :)

Be happy! ;)

buymeacoffee!