Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker volume initialization - copying data from image to container

Tags:

docker

https://docs.docker.com/engine/userguide/dockervolumes/ says:

"Volumes are initialized when a container is created. If the container’s base image contains data at the specified mount point, that existing data is copied into the new volume upon volume initialization."

However this is not exactly what I'm observing. Here's my scenario:

  1. I create a container that contains some data in /opt/data
  2. I commit this container and create an image out of it
  3. I create another container using the image I've just prepared and create a volume that points /opt/data to a local catalog.

According to the docs, I expected that files under /opt/data of the image will be copied to the locally created volume. It's not happening..

<local>:~$ docker run --name test -it ubuntu bash
root@76f42fce6ab7:/# mkdir /opt/data
root@76f42fce6ab7:/# echo "foo" > /opt/data/my-data
$ docker commit test test-with-data

<local>:~$ docker run -it -v /tmp/test-volume:/opt/data test-with-data bash
root@731b483527ad:/# ls /opt/data
root@731b483527ad:/#
root@731b483527ad:/# exit

Is there something I don't understand here?

like image 613
mkub Avatar asked Nov 19 '25 00:11

mkub


1 Answers

It's because you've specified a host directory. If you don't specify a host directory and instead let Docker manage the volume, it works as you expect:

$  docker run --name test -it debian bash
root@ac99b805a689:/# mkdir /opt/data
root@ac99b805a689:/# echo "foo" > /opt/data/my-data
root@ac99b805a689:/# exit
exit
$ docker commit test test-with-data
a35463157fbee6180ed91c458288cf528da93a23bf340f44c3d2a7ff355fa2b1
$ docker run -it -v /opt/data/ test-with-data bash
root@73f70c3b5518:/# ls /opt/data
my-data
root@73f70c3b5518:/# cat /opt/data/my-data
foo
like image 140
Adrian Mouat Avatar answered Nov 21 '25 15:11

Adrian Mouat