fmdaboville
fmdaboville

Reputation: 1821

Move docker bind-mount to volume

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

Answers (3)

bcag2
bcag2

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

fly2matrix
fly2matrix

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

call-in-co
call-in-co

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:

  1. Stop the running container (e.g. named nexus3)
  2. Create a docker volume called nexus-data, creating it with the following command: docker volume create nexus-data)
  3. By default, Docker will store the volume's content at /var/lib/docker/volumes/nexus-data/_data/
  4. Simply copy the directory where you previously had been using a bind mount to the aforementioned volume directory (you'll need super user privileges to do this, or for the user to be part of the docker group): cp -R /path/to/nexus-data/* /var/lib/docker/volumes/nexus-data/_data/
  5. Restart the nexus3 container with $ docker run -v nexus-data:/nexus-data sonatype/nexus3 --name=nexus3
  6. Your container will be back up and running, with the files persisted in the directory /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/ directory

Q.E.D.

Upvotes: 3

Related Questions