Copy the SQL File to the Container

To import a SQL file into a database running in Docker — for example a number_cache.sql into a MySQL container — you first get the file inside the container, then run it with the database client.

1. Copy the SQL File into the Container

Use docker cp to copy number_cache.sql from your local machine into the running MySQL container:

docker cp /tmp/number_cache.sql mysql-container:/tmp/number_cache.sql

This copies the file from your /tmp directory into the /tmp directory of your running mysql-container. The target path is inside the container's filesystem, not the volume — the file stays available even while the container's data volume is untouched.

2. Import the SQL File into the Database

With the file inside the container, pipe it into the MySQL client:

docker exec -i mysql-container mysql -u root -p number_cache_db < /tmp/number_cache.sql

The -i flag keeps stdin open so the shell redirection can feed the file to the client running inside the container. Replace number_cache_db with the target database name.

3. Verify the Import

docker exec -it mysql-container mysql -u root -p -e "USE number_cache_db; SHOW TABLES; SELECT COUNT(*) FROM number_cache;"

Alternative: Run the File Directly in the Container

If the SQL file is already inside the container (e.g., baked into the image), run it directly:

docker exec -i mysql-container mysql -u root -p number_cache_db < /tmp/number_cache.sql

Related Articles

A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles