10

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. The is a proper way to do this ?

fmdaboville
  • 1,394
  • 3
  • 15
  • 28
  • There may well be a first-class way of doing this, but a hacky solution would just be to mount both into a container, and then copying from one to the other from inside the container. – Oliver Charlesworth Apr 17 '18 at 21:52

2 Answers2

6

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.

Krokomot
  • 3,208
  • 2
  • 4
  • 20
fly2matrix
  • 2,351
  • 12
  • 13
  • What about the ports (-p flag), because they are already taken, I just put other and after it will be possible to come back with wanted ports ? – fmdaboville Apr 24 '18 at 15:15
  • For example, my nexus container take the 8081, and I prefer to not change it. – fmdaboville Apr 25 '18 at 20:03
  • 2
    Stop the first NEXUS container( which is actually using 8081) , since this container is already storing data in host's directory (/nexus)- you have all the required information outside of that container. Now start/create the new NEXUS container with mounted volume (nexus-data) and assigned the required port 8081, since at this point of time your 8081 port is free (old nexus-container is stopped) – fly2matrix Apr 26 '18 at 04:46
  • Indeed, storage of the data has little to do with the port your nexus container is listening to. You can start and stop containers without losing data – Peter F Aug 07 '20 at 07:47
  • 3
    For me, `docker copy /nexus-data/* :/nexus-data` doesn't work, `docker cp /nexus-data/. :/nexus-data` works. [Copying files from host to Docker container - Stack Overflow](https://stackoverflow.com/questions/22907231/copying-files-from-host-to-docker-container). – Jason Law Aug 22 '20 at 04:50
  • @JasonLaw -- Edited the post to correct this. (First, alas incomplete, edit by [@besendorf](https://stackoverflow.com/users/7580219/besendorf)) – Krokomot May 21 '23 at 17:54
2

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.

call-in-co
  • 281
  • 2
  • 11