Here we gonna see the different ways that the docker storage files.
Unification filesystem - AUFS
AUFS is one of the most common among Docker installations, it is based in layers, and when a layer creates a new file it will exists only in this layer or derivate (upper) containers.
Whenever you wanna modify a file that besides in bottom layers the Docker uses a technique called CoW (Copy-on-write), where the file of the lowers layers still unmodified (the files in bottom layers are read only to preserve its state), then Docker handler with this simple creating a new copy of this file in the new layer with the modified part attached, do you saw the problem here? Even if a small part of the file is modified, all the file must be copied.
This is a technique named whiteout, and the new files are marked as whiteout file.
This is the simplest known way and maybe most used by developers to put the application code inside a container, you just need to give Docker a link between a folder in your host and your container, to do so you need to run your container with a -v param, like in this example:
$ docker container run -v /var/lib/container1:/var ubuntu
With the -v param we are telling the docker to make a link between the /var/lib/container1 folder located in the host with the folder /var located in the container.
This method had a problem that is not being portable, so the host that will execute must have the necessary files.
Mapping through data container
This model creates a container and inside of it is given a volume to be used by other containers. To create this container just do:
$ docker create -v /dbdata --name dbdata postgres /bin/true
To consume the data in other containers just run with:
$ docker container run -d --volumes-from dbdata --name db2 postgres
Now the container db2 gonna have a folder named /dbdata that is the same as in the container dbdata, making the process fully portable. The counterpart of this method is because you gonna need to keep a container just for it.
Since Docker 1.9 we can create volumes in an isolate mode, just use this commando:
$ docker volume create --name dbdata
The above command the docker creates a volume that can be used by any container. To use this volume, you just need to use this command when creating a new container:
$ docker container run -d -v dbdata:/var/lib/data postgres
At this point the folder /var/lib/data within the container will point to dbdata volume.
This last model is recommended since the release because it's not easily lost when the container is deleted and still easy to manage it.
This post is part of a series about docker, to see more please follow the tag series: docker-by-tiagomac, keep learning and practicing.
Top comments (0)