Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Volumes when updating the image

I'm newbie with Docker and I'm trying to better understand the Volumes topic. Here I see that:

  • 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. (Note that this does not apply when mounting a host directory.)
  • Data volumes can be shared and reused among containers.
  • Changes to a data volume are made directly.
  • Changes to a data volume will not be included when you update an image.
  • Data volumes persist even if the container itself is deleted.
  • Data volumes are designed to persist data, independent of the container’s life cycle. Docker therefore never automatically deletes volumes when you remove a container, nor will it “garbage collect” volumes that are no longer referenced by a container.

Which is very clear, except for this:

  • Changes to a data volume will not be included when you update an image.

and I'm really struggling having a better understanding of this. Can anyone provide me some more clear overview, possibly with some basic example?

Thanks!

like image 778
net.cat Avatar asked Jun 29 '16 09:06

net.cat


People also ask

Does Docker automatically create volumes?

For the mongo image, this is the /data/db directory, as specified in the Docker hub documentation. Docker automatically creates a new volume with a random name the first time you run it. If the named volume doesn't exist locally, it will be created automatically. That's it!

How do you automatically update your Docker containers if base images are updated?

By pushing a new Docker image to your repository, Watchtower will automatically trigger a chain of events to update your running containercontainerIn computer science, a container is a class or a data structure whose instances are collections of other objects. In other words, they store objects in an organized way that follows specific access rules. The size of the container depends on the number of objects (elements) it contains.https://en.wikipedia.org › wiki › Container_(abstract_data_type)Container (abstract data type) - Wikipedia's base Docker image. When Watchtower detects a new push, it will pull the new base image, gracefully shutdown your running container, and start it back up.

Do Docker volumes persist?

Volumes are the preferred way to persist data in Docker containers and services. Some use cases for volumes include: Sharing data among multiple running containers. If you don't explicitly create it, a volume is created the first time it is mounted into a container.


2 Answers

Volumes live outside of your container's filesystem. They are mounted at runtime but data in them does not live in the container. Let's take an example:

j@host $ docker volume create --name my-data

This creates a volume (no containers yet) called my-data. Now I create a new container and mount this volume inside my container at /inside/data.

j@host $ docker run -it -v my-data:/inside/data --name container1 alpine:3.3 sh

Now I'm in my shell inside my container, so I create a new file inside the folder /inside/data.

root@container1 $ cd /inside/data && touch my-file

Now /inside/data isn't really in my container filesystem, it's a volume that lives outside of the container, that is just mounted in. If I stop my container, and _don't mount that volume back in, my file isn't there.

root@container1 $ exit
j@host $ docker run -it --name container2 alpine:3.3 sh
root@container2 $ ls /inside/data
ls: /inside/data: No such file or directory
root@container2 $ exit

In fact, even the folder isn't there! Because I didn't mount anything at that location. I'm using volumes, so my data is not persisted in the container. So how do I find it again? Well it is persisted in the volume (my-data). Let's take a look at it by mounting it to another container (I don't need to mount it at the same point, I'm going to use something different).

j@host $ docker run -it -v my-data:/different/folder --name container3 alpine:3.3 sh
root@container3 $ ls /different/folder
my-file
root@container3 $ exit

OK so I can get that data and mount it around to different containers. But where does the data in a volume actually live? Well on my host filesystem. We can check by doing:

j@host $ docker volume inspect my-data
[
    {
        "Name": "my-data",
        "Driver": "local",
        "Mountpoint": "/var/lib/docker/volumes/my-data/_data",
        "Labels": {},
        "Scope": "local"
    }
]

OK, so it tells me where on my host machine that volume is located, let's take a look at what's in there (we need sudo because it's owned by root).

j@host $ sudo ls /var/lib/docker/volumes/my-data/_data
my-file

And there's the file!

like image 94
johnharris85 Avatar answered Sep 24 '22 20:09

johnharris85


I think it's trying to say if the data volume is changed by other process (it could be changed from other container or other system), the changes will not be included / recorded when you building / updating your docker image, which is very obvious.

like image 41
Aldo Suwandi Avatar answered Sep 22 '22 20:09

Aldo Suwandi