mysqldockerdocker-exec

How to import SQL file from docker container file system?


I have a docker exec command that tries to import a SQL dump file but it's complaining about the file not being found.

docker exec $DB_CONTAINER mysql -u root -ptest < /path/to/database.sql

The path is correct (inside the container) but when I investigated why this was not working, I figured that it was trying to look for the file from my host system instead of the container where the file is actually located. How do I import this file and make sure that it looks at the container file system instead of my host file system.


Solution

  • docker exec $DB_CONTAINER mysql -u root -ptest < /path/to/database.sql
    

    The < is being interpreted by the shell that's about to execute docker. /path/to/database.sql.

    The path is correct (inside the container)

    Then the filename has to make it inside the container. You have a few options:

    docker exec $DB_CONTAINER sh -c 'mysql -u root -ptest < /path/to/database.sql'
    

    Invoke a shell on the container and have the shell handle the redirection to mysql.

    docker exec $DB_CONTAINER mysql -u root -ptest -e "source /path/to/database.sql"