Reputation: 1821
Actually, I run my containers like this, for example :
docker run -v /nexus-data:/nexus-data sonatype/nexus3
^
After reading the documentation, I discover volumes that are completely managed by docker. For some reasons, I want to change the way to run my containers, to do something like this :
docker run -v nexus-data:/nexus-data sonatype/nexus3
^
I want to transfer my existing bind-mount to volumes.
But I don't want to lose the data into /nexus-data
folder, is there a possibility to transfer this folder, to the new volume, whitout restart everything ? Because I've also Jenkins and Sonar containers for example, I just want to change the way to have persistent data. There is a proper way to do this ?
Upvotes: 16
Views: 10396
Reputation: 2439
Other solution :
I had bind-volume for MySql data in /data/mysql/
,
FIRST, stop and remove any container that used this bind-volume.
docker-compose.yml before (with bind-volume)
db:
image: mysql:8.4
volumes:
- /data/mysql:/var/lib/mysql
I create my volume:
docker create volume db_mysql
check where are this volume db_mysql:
docker volume inspect db_mysql | grep "Mountpoint"
# returns this line:
"Mountpoint": "/var/lib/docker/volumes/db_mysql/_data",
then MOVE folder and set root owner/group :
# move data
sudo mv /data/mysql/ /var/lib/docker/volumes/db_mysql/
# delete default empty _data folder
sudo rm -r /var/lib/docker/volumes/db_mysql/_data
# rename mysql folder to _data as expected
sudo mv /var/lib/docker/volumes/db_mysql/mysql /var/lib/docker/
volumes/db_mysql/_data
# set rights as expected
sudo chown -R root: /var/lib/docker/volumes/db_mysql/_data/
then change docker-compose.yml to:
db:
image: mysql:8.4
volumes:
- db_mysql:/var/lib/mysql
volumes:
db_mysql:
external: true
tested with docker v27.3.1, docker compose v2.29.7 under Ubuntu 22.04
based on: https://www.reddit.com/r/docker/comments/miczl9/switching_from_bind_mounts_to_named_volumes/
Upvotes: 0
Reputation: 2477
You can try out following steps so that you will not loose your current nexus-data
.
#>docker run -v nexus-data:/nexus-data sonatype/nexus3
#>docker cp /nexus-data/. <container-name-or-id>:/nexus-data/
#>docker stop <container-name-or-id>
#>docker start <container-name-or-id>
docker cp
will copy data from your host-machine's /nexus-data
folder to container's FS /nexus-data
folder which is your mounted volume.
Let me know if you face any issue while performing following steps.
Upvotes: 7
Reputation: 291
Here's another way to do this, that I just used successfully with a Heimdall container. It's outlined in the documentation for the sonatype/nexus3 image:
nexus3
)nexus-data
, creating it with the following command: docker volume create nexus-data
)/var/lib/docker/volumes/nexus-data/_data/
docker
group): cp -R /path/to/nexus-data/* /var/lib/docker/volumes/nexus-data/_data/
nexus3
container with $ docker run -v nexus-data:/nexus-data sonatype/nexus3 --name=nexus3
/path/to/nexus-data/
now mirrored in the docker volume. Check if functionality is the same, of course, and if so, you can delete the /path/to/nexus-data/
directoryQ.E.D.
Upvotes: 3