
Docker | WSL
Importing a SQL Dump Into Docker Container Using WSL
Written by:
Raphael Batagini
Published on:
Sun Dec 03 2023
Introduction
When working with Docker containers and WSL (Windows Subsystem for Linux), it's common to encounter scenarios where you need to import a SQL dump into a database running inside a Docker container. This process is essential for tasks such as setting up development environments or migrating databases.
The Command
To achieve this, you can use the docker exec command along with the MySQL client inside the Docker container. The command has a generic structure, making it adaptable to various scenarios:
docker exec -i your_container_name_or_id mysql -u your_username -pyour_password your_database_name < /path/to/your/sql_dump.sql
Replace the placeholders with your specific details:
your_container_name_or_id: The name or ID of your Docker container. your_username: Your MySQL database username. your_password: Your MySQL database password. your_database_name: The name of the database you want to import into. /path/to/your/sql_dump.sql: The path to the SQL dump file on your WSL host machine.
Understanding the Filepath in WSL
In the provided example command below:
docker exec -i my-app-db-1 mysql -u user -ppassword my_app_schema < /mnt/e/Downloads/db-dump.sql
The file path /mnt/e/Downloads/db-dump.sql
refers to a location on the Windows filesystem accessible from within WSL. In WSL, the filesystem is mounted under the /mnt directory, with each drive having its own subdirectory. In this case, the E: drive is accessible under /mnt/e/.
Therefore, the full Windows path is E:\Downloads\db-dump.sql
, and within WSL, it is represented as /mnt/e/Downloads/db-dump.sql
. This allows seamless interaction between WSL and Windows, enabling you to access and manipulate files across both environments.
Conclusion
In conclusion, importing a SQL dump into a Docker container using WSL proves to be a straightforward process with the right command structure. Leveraging the flexibility of the docker exec command along with the MySQL client within the Docker container allows for seamless database setup and migration. Understanding the nuances of file paths in WSL ensures accurate referencing of files located on the Windows filesystem. By adapting the provided command to your specific configuration, you can efficiently manage database operations in a Dockerized environment integrated with WSL.