How do named volumes work in docker?

I am trying to understand exactly how a named volume works in the following example from docker docs :

version: "3"

services:
  db:
    image: db
    volumes:
      #1
      - data-volume:/var/lib/db
  backup:
    image: backup-service
    volumes:
      #2
      - data-volume:/var/lib/backup/data

volumes:
  data-volume:

I assume that the first occurrence of the named volume (# 1) determines what is contained within the volume, and subsequent occurrences (# 2) simply share the contents of the volume with any containers to which they refer.

Is this assumption correct?

+4
source share
2 answers

Listing data-volume:under a volumes:top-level key creates a named volume on the host, if it does not already exist. This behaves as follows according to this source.

  • , docker -v my-valu-data:/data imageName, / / .

  • , , / /, .

  • docker / . " " [volume-name] ".

, , , ( ). , (althoug, , ), depend_on,

-------------------

depend_on 3.

+4

, .

- , , .

, . ( , ):

version: "3"

services:
  db:
    image: db
    volumes:
      #1 uses the named and shared volume 'data-volume' created with #3
      - data-volume:/var/lib/db
  backup:
    image: backup-service
    volumes:
      #2 uses the named and shared volume 'data-volume' created with #3
      - data-volume:/var/lib/backup/data

volumes:
  #3 creates the named volume 'data-volume' 
  data-volume:

(, , , ). .

, .

0

Source: https://habr.com/ru/post/1674179/


All Articles