Now that we’ve seen that data inside a container is deleted by default, it’s time to explore the first way to persist your data: bind mounts.
A Bind Mount allows you to link a folder on your host machine to a folder inside the container. Anything you write to the container is immediately visible on your machine, and vice versa. It’s like an open window between your container and your system.
How does it work?
When you launch a container with a Bind Mount, you specify a path on your local machine and a path inside the container.
Example with MariaDB:
docker run -d \
--name mariadb \
-e MYSQL_ROOT_PASSWORD=secret \
-v ./data:/var/lib/mysql \
mariadb:latest
Here:
./datais a folder on your computer (or server)/var/lib/mysqlis the folder where MariaDB stores its databases inside the container- The
-v(volume) acts as the link between the two
Simplified diagram:
HÔTE : ./data
│
▼
CONTENEUR : /var/lib/mysql
Everything MariaDB writes to/var/lib/mysql is automatically copied to./data on your machine.
✅ Benefits of Bind Mounts
- Your data is immediately accessible from your host system.
- You can edit or save files directly without going through Docker.
- Ideal for development, where you often want to inspect an application’s files or logs.
⚠️ Limitations to be aware of
- The file structure on the host must be compatible with the application: some containers do not automatically create the required folders.
- Permissions can be an issue depending on the system (LinuxWindows vs. Mac).
- Less secure: your sensitive data is exposed on the host system.
- Less portable deployment: if you move your project to another server, you’ll need to copy the local folder.
Practical example
If you create a file directly in the container:
docker exec -it mariadb bash
touch /var/lib/mysql/fichier-test.txt
You’ll find it instantly in your folder./dataon the host:
ls ./data
fichier-test.txt
/var/lib/mysqlAnd conversely, if you add a file to ./data, MariaDB "sees" it immediately in its folder.
🧠 Key takeaway
Bind Mount is the simple and straightforward solution for persisting your data during development. It offers immediate access to your files from the host, but for production environments, Named Volumes are preferred, which we’ll cover in the next section
📝 Author’s Note
Personally, despite certain drawbacks (permissions, portability, security), I prefer this solution. It allows direct access to files, which is very convenient for development, debugging, and quick backups.