Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 704
rickard Avatar asked Dec 11 '14 11:12

rickard


People also ask

How do I transfer files between two containers?

You can use the docker cp command to copy the file. The first path (Source) is the path in the Docker Container and the second one is the path inside your Local System (Destination).


2 Answers

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.

like image 116
Bryan Avatar answered Oct 06 '22 01:10

Bryan


It looks as though there is an open issue around adding volume layers to docker:

https://github.com/docker/docker/issues/9382

like image 39
rickard Avatar answered Oct 06 '22 01:10

rickard