Reputation: 161
I would like to change an existing docker mysql volume that contains my databases in a container from:
/var/lib/docker/volumes/docker_data
to a shared volume, eg
/mysql_data
but when I change the docker-compose.yml
volume parameter from:
- data:/var/lib/mysql
to
- /mysql_data:/var/lib/mysql
I lose all my database files and mysql starts as vanilla install.
How can I move or otherwise change the existing container mysql storage to the shared storage?
Upvotes: 0
Views: 1597
Reputation: 158714
If you create a container that has both mounts, you can copy data from one place to the other.
docker run \
--rm \ # don't keep this container after exit
-v docker_data:/old \ # mount the old directory
-v /mysql_data:/new \ # mount the new directory
-w /old \ # cd /old
busybox \ # image to run
cp -a . /new # command to run
For the most part you need to actually be inside a Docker container to access Docker volumes. The Docker documentation's discussion on backing up and restoring volumes also uses this technique.
Upvotes: 1
Reputation: 179
First, you need to create a data volume container which is pretty straightforward.
$ docker create -v /var/lib/mysql --name mysqldata mysql
Next, you need to mount the data volume container "mysqldata" when you run the MySQL container which will have MySQL running.
$ docker run --name mysqldb --volumes-from mysqldata -e MYSQL_ROOT_PASSWORD=password -p 3307:3306 mysql
To quick test add few data and delete your existing container and rerun new one. you will see your data are persistent .
Same equivalent command you can use in your yaml file.
Upvotes: 1