2

I'm using a docker volume, specified in my dockerfile so that my data can persist on the host. The dockerfile looks something like this:

FROM base-image
VOLUME /path/to/something
RUN do_stuff
....

When I run the container it creates a volume (call it VolumeA) which I can see when I do a docker volume ls.

If I stop and remove the container, the VolumeA sticks around as expected.

My question is, if I run a new version of the container, is there a way to use VolumeA rather than have it create a new one?

Chris
  • 329
  • 3
  • 13
  • 1
    That's why you should have named volumes – Xiongbing Jin May 12 '16 at 20:20
  • So the purpose of VOLUME in a dockerfile is purely to persist data after the container that created it is removed? There's no way to re-use that data in another container? – Chris May 12 '16 at 20:23
  • You can but it is just not straightforward as named volume. See http://stackoverflow.com/questions/26009619/how-to-retrieve-volume-from-a-removed-docker-container – Xiongbing Jin May 12 '16 at 20:30

2 Answers2

2

I prefer using named volumes, as you can mount them easily to a new container.

But for unnamed volume, I:

  • run my container (the VOLUME directive causes it to create a new volume to a new path that you can get by inspecting it)
  • move the path of the old volume to that new path.

Before docker volume commands, I used to do that with a script: updateDataContainerPath.sh.

But again, these days, none of my images have a VOLUME in them: I create separately named volumes (docker volume create), and mount them to containers at runtime (docker run -v my-named-volume:/my/path)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks very much. Have removed the VOLUME from my dockerfile and manage it now with a -v at runtime. Much nicer solution. – Chris May 13 '16 at 12:00
0

You might use the -v flag in the docker run command to bind the existing volume to your new docker container

docker run -v VolumeA:/path/to/something [image]

Also look into the --volumes-from flag to mount volumes used or created by other containers.

https://docs.docker.com/engine/reference/commandline/run/ https://docs.docker.com/engine/reference/commandline/volume_create/

erezny
  • 46
  • 1
  • 3