Persistent Data Loss in Docker-Machine: Why Volumes Get Cleared on VM Restart and How to Fix It


2 views

html

When working with Docker Toolbox on OSX, many developers encounter a frustrating scenario: their carefully configured data volumes mysteriously disappear after restarting the docker-machine VM. Let's examine why this happens and how to properly implement persistent storage.

The key misunderstanding stems from how docker-machine handles the boot2docker VM storage. While Docker volumes are designed to persist beyond container lifecycles, the docker-machine restart command actually recreates the VM from scratch in some configurations.

Here's what happens technically:

# Typical workflow where data disappears
docker-machine create default
docker-machine ssh default
docker run -v /data:/var/lib/mysql --name mydata busybox sh -c "echo 'test' > /var/lib/mysql/test"
docker-machine restart default
# Data is now gone

The docker-machine VM uses a temporary filesystem by default. When you restart:

  1. The VM gets destroyed
  2. A new VM is created from the ISO image
  3. Only certain directories (/var/lib/docker) are preserved

For true persistence, you need to either:

Option 1: Use the correct persistent path

docker run -v /var/lib/docker/data:/var/lib/mysql --name mydata busybox \
  sh -c "echo 'persistent' > /var/lib/mysql/test"

Option 2: Create a permanent storage disk

docker-machine create -d virtualbox --virtualbox-disk-size "5000" default
docker-machine stop default
VBoxManage modifyhd ~/.docker/machine/machines/default/disk.vmdk --resize 5000
docker-machine start default

For production environments, consider these additional measures:

# 1. Use named volumes
docker volume create --name mysqldata
docker run -v mysqldata:/var/lib/mysql mysql

# 2. Configure permanent storage in docker-machine
docker-machine create -d virtualbox \
  --virtualbox-memory 2048 \
  --virtualbox-disk-size "10000" \
  --virtualbox-no-share \
  myserver

Remember that Docker Toolbox uses VirtualBox snapshots which can affect persistence. For critical data, consider migrating to Docker Desktop or implementing external storage solutions.


When working with Docker Toolbox on macOS (or Windows), many developers encounter a frustrating behavior where volume data disappears after restarting the docker-machine VM. The root cause lies in how boot2docker (the lightweight Linux VM) handles storage:

# This typical volume mount WILL NOT persist across restarts:
docker run -v /data:/var/lib/mysql --name mydata busybox sh -c "echo 'test' > /var/lib/mysql/test"

docker-machine uses a temporary filesystem by default for the boot2docker VM. When you execute docker-machine restart, the VM gets rebuilt from an ISO image, wiping any non-persistent storage areas. Only specific directories survive this process:

  • Persistent locations: /var/lib/docker and /mnt/sda1 (mounted on boot)
  • Temporary locations: Everything else including custom /data directories

Here's the correct way to create persistent volumes that survive VM restarts:

# 1. First mount to the persistent area
docker run -v /mnt/sda1/data:/var/lib/mysql --name persistent-data busybox

# 2. Verify the actual storage location
docker-machine ssh default
ls /mnt/sda1/data  # Should show your persisted files

# 3. Test persistence through restarts
docker-machine restart default
docker-machine ssh default
ls /mnt/sda1/data  # Files should still exist

For better maintainability, consider using Docker's native volume management:

# Create a named volume
docker volume create mysql_data

# Mount it to your container
docker run -d \
  --name mysql \
  -v mysql_data:/var/lib/mysql \
  mysql:latest

# Verify volume persistence
docker volume inspect mysql_data

For serious development or production use:

  1. Configure docker-machine to use persistent disk storage
  2. Or migrate to Docker Desktop (for macOS/Windows) which handles persistence automatically
  3. For cloud deployments, use cloud provider volume solutions (AWS EBS, Azure Disk, etc.)

Remember that the docker-machine approach is legacy technology. Modern Docker Desktop installations don't suffer from these persistence issues.