Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing SQL scripts on docker container

Tags:

docker

mysql

I have a docker container running mysql and I want to push a .sql file to the container and then have mysql execute the commands in it. The first step is fairly straightforward:

docker cp ./dummy.sql <container_id>:/

From there I am trying to run mysql from the command line and point it to the file that I just pushed to the container.

docker exec <container_id> mysql -u root -ppassword < /dummy.sql

This command appears to be trying to use /sample.sql as stdin locally rather than on the container. I also tried wrapping quotes around everything after the container ID which also seems to not work.

I also tried pushing a .sh file with the command in it to the docker container and then just executing that, but that is less ideal and also not working. Any advice?

like image 360
Akron Avatar asked Jan 14 '16 00:01

Akron


People also ask

Can I run SQL Server in a Docker container?

You can now install SQL Server on Linux distributions like the RHEL, SUSE, Ubuntu, etc. However, in order to install and use SQL Server on a Mac, you need to run the Linux distribution inside a docker container.


2 Answers

If you are going to do pipe redirections in your command, pass it as a string to /bin/sh:

docker exec <container_id> /bin/sh -c 'mysql -u root -ppassword </dummy.sql'
like image 192
Thomasleveil Avatar answered Oct 01 '22 00:10

Thomasleveil


One does not need to copy the script first. If the script is on the host then:

docker exec -i <container_name> mysql -u root -ppassword <mydb> < /path/to/script.sql

Or if the Use db directive is contained inside the script:

docker exec -i <container_name> mysql -u root -ppassword < /path/to/script.sql
like image 27
PuzzledVacuum Avatar answered Sep 28 '22 00:09

PuzzledVacuum