F
F
furyon2016-11-05 03:39:01
Docker
furyon, 2016-11-05 03:39:01

How do volumes work in Docker?

I came to the realization that I don’t fully understand how volumes work in docker, I started to figure it out, I brought out 4 entities about volumes:

  1. Mounting external folders with the -v option. Everything seems to be clear here.
  2. Data-only containers. As I understand it, a container is stupidly created in which the data will be in the right folder (in fact, somewhere in /var/lib/docker/vfs/dir/..), and this can be used using --volumes-from. Plus, in this case, you don’t have to fool around about rights, etc. on files if it is required, for example, to work with database files, unlike the first method.
  3. named volumes. Unlike data-only, a container is not created (as a result, a base image is not needed), it is located somewhere in /var/lib/docker/volumes/.., available by name. It is more convenient to do inspect.
  4. VOLUME in Dockerfile. This is where I can't understand. I specify, for example, VOLUME /var/www , save something there, delete the container, recreate it from the same image, the data is gone. What's the point? I couldn't find the answer in the documentation.

Please tell me if I understood the first 3 points correctly, and tell me about the 4th.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Askhat Bikmetov, 2016-12-03
@furyon

Apart from host directories, there are 2 ways to do persistence in docker: (1) data containers and (2) volumes. On the example of a composition, we will consider both options.
The oldest and, in my opinion, crutch method. It consists in the fact that the data will be stored in a container in which the process is not running, and in fact the container is stopped. As such a container, you can use the "main" image or use a special one, like tianon/true.

version: '2'
services:
  web:
    image: 'nginx'
    volumes_from:
      - 'data'
  data:
    build: './public_html'

FROM nginx
ADD index.html /var/www/public_html
CMD /bin/true

Pay attention to the directive CMDin the Dockerfile of the data container. Despite the fact that the initial image is nginx, as in the service web, the command /bin/truewill not start the process, but only signals the docker that the container has been successfully built. docker-compose pswill show that the data container has a status of type Exit..., and this is exactly the state we are looking for for this container.
Or, as they are officially called, named volumes. A more obvious way to work with persistence.
version: '2'
services:
  web:
    image: 'nginx'
    volumes:
      - 'web_data:/var/www/public_html'
volumes:
  web_data:

This configuration will automatically create a volume with the name web_dataand map it to a folder /var/www/public_htmlinside the container webwhen the compose is started. Please note that in this example, the volume is designated as a key with no values, this is a valid yaml syntax, and, in this case, means that the compose will fallback the volume options to the default ones, of which driver: 'default'. In case you are pulling compos locally or on a remote docker machine, the default driver is fine unless you know it's not.
Note: Of the drivers that are of particular interest, at least to me, are the driver for NFS and DigitalOcean Block Storage .
The default behavior of docker is to not delete anything unless the user explicitly asks for it, this applies to both data containers and volumes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question