Home > Software design >  What does volumes do in docker-compose
What does volumes do in docker-compose

Time:05-20

I am currently learning Docker. I am stucked at the idea of volumes. I assume that they made to store the data whenever we restart the container etc., but i do not understand what happens if we don't provide the ":" for the source:target. Example:

- "/usr/src/my-app/frontend/node_modules"
- "/usr/src/my-app/backend/node_modules"

What do we store inside the container if we use volumes like above?

The whole docker-compose

version: '3'

services:

  nginx:
    image: nginx
    container_name: nginx
    ports:
      - 80:80
    restart: always
    volumes:
      - "./nginx/default.conf:/etc/nginx/conf.d/default.conf"


  backend:
    build:
      dockerfile: Dockerfile.dev
      context: ./backend
    container_name: backend
    volumes:
      - "/usr/src/my-app/backend/node_modules"
      - "./backend:/usr/src/my-app/backend"


  frontend:
    build: 
      dockerfile: Dockerfile.dev
      context: ./frontend
    container_name: frontend
    environment:
      CHOKIDAR_USEPOLLING: "true"
    volumes:
      - "/usr/src/my-app/frontend/node_modules"
      - "./frontend:/usr/src/my-app/frontend"

CodePudding user response:

It is an anonymous volume. It is managed by docker like a named volume, but it doesn't have a real name, only a GUID. Likewise, it's similar to the one you get when you use the VOLUME instruction in your Dockerfile without mounting a named volume or bind mount to that path when running the container.

See this for example (emphasis mine):

-v or --volume: Consists of three fields, separated by colon characters (:). The fields must be in the correct order, and the meaning of each field is not immediately obvious.
In the case of named volumes, the first field is the name of the volume, and is unique on a given host machine. For anonymous volumes, the first field is omitted.

https://docs.docker.com/storage/volumes/#choose-the--v-or---mount-flag

As long as you keep the container and only restart it, the same volume will be used. If you delete the container / create a new container, it will use a new volume.

  • Related