In Docker, how can I share files between containers and then save them to an image?

rickard picture rickard · Dec 11, 2014 · Viewed 18k times · Source

I want to commit the data in a container's shared volume to an image. I cannot seem to do it? I kind of get the impression this perhaps is not possible in Docker but that seems totally at odds with the whole philosophy of not leaving data on the host so part of me thinks there must be a way to do this.

1. Terminal 1

Start up a container in Terminal 1 with a volume.

$ docker run -it -v /data ubuntu:14.10 /bin/bash
root@19fead4f6a68:/# echo "Hello Docker Volumes." > /data/foo.txt

2. Terminal 2

Start up a second container in Terminal 2 the file from container 1 is there so docker volumes are all working.

$ docker run -it --volumes-from 19fead4f6a68 ubuntu:14.10 /bin/bash

root@5c7cdbfc67d8:/# cat /data/foo.txt
Hello Docker Volumes.

3. Terminal 3

My understanding is that I can only commit diffs to images so I check what the diffs are on both the containers. For some bizarre reason my changes do not show up!??

$ docker diff 19fead4f6a68
A /data

$ docker diff 5c7cdbfc67d8
A /data

4. Back in Terminal 1

I create a file outside of the volume folder

 root@19fead4f6a68:/# echo "Docker you are a very strange beast...." > /var/beast.txt

5. Back in Terminal 3

We now have some changes we can commit although I am rather frustrated as this is not the data from the volume I needed to share with my other container.

$ docker diff 19fead4f6a68
A /data
C /var
A /var/beast.txt

Clearly this is by design. Anyone have any ideas as to why docker don't allow me to save volume data to a commit? Is there anyway at all to share files between containers and then save them to an image? I feel like there must be something I am missing? Especially to the ends of sharing data whilst avoiding host dependencies.

Answer

Bryan picture Bryan · Dec 11, 2014

Volumes are outside of container images. That's exactly what they are for - bringing data inside a container that isn't in the image.

From the Docker docs:

A data volume is a specially-designated directory within one or more containers that bypasses the Union File System to provide several useful features for persistent or shared data:

  • Data volumes can be shared and reused between containers
  • Changes to a data volume are made directly
  • Changes to a data volume will not be included when you update an image

If you want to save some changes as part of an image, make the changes inside the image and not in a volume. If you want to share changes across multiple containers, put that data in a volume but you have to make your own arrangements for snapshots, rollback, etc., because Docker doesn't have that feature.

Maybe you would be interested in Flocker.