skip to Main Content

I am trying to run mongodb using docker-compose.
Every time I restart the containers, I see that mongo creates default volumes with random names and the number of volume grows.

enter image description here

Why these volumes are created and how can I avoid them.

My docker-compose.yml for mongo is as follows:

mongo:
    image: mongo
    restart: always
    networks:
      - ts-net
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: root
    volumes:
      - db_data:/data/db/

2

Answers


  1. You’re asking:

    Why these volumes are created…?

    The volumes you speak about are called anonymous volumes. They can typically be created by the Dockerfile directive VOLUME, e.g.:

    github.com/docker-library/mongo/blob/master/5.0/Dockerfile

    VOLUME /data/db /data/configdb
    

    These volumes have indeed the drawbacks that (i) their automatically-generated name does not refer to the image they were created from, and that (ii) they are not removed once the corresponding container is removed (unless we use the CLI option docker run --rm).

    how can I avoid them…?

    1. If you’re developing your own base image, just avoid using the VOLUME directive.
    2. Otherwise, the best way to cope with existing images relying on the VOLUME directive is to (i) figure out which paths are associated to a given volume, and (ii) associate these paths to a named volume within the docker-compose.yml specification, namely:
    services:
    
      db:
        image: mongo:5.0
        restart: always
        environment:
          MONGO_INITDB_ROOT_USERNAME: root
          MONGO_INITDB_ROOT_PASSWORD: root
        volumes:
          - 'db_data:/data/db'
          - 'db_config:/data/configdb'
        networks:
          - db-net
    
    networks:
      db-net:
        driver: bridge
    
    volumes:
      db_data:
        driver: local
      db_config:
        driver: local
    

    Additional references

    For more details/remarks about VOLUMEs, see also:

    Login or Signup to reply.
  2. I was having the same issue (newbie) with unnamed / tangling Docker Images and Volumes. After much research and experimentation (and the first answer posted here) I now have a DOCKER-COMPOSE.YAML the can be used to build a generic 3-Tier MERN Stack App that is Dockerized into three Containers. And all the Docker objects are named explicitly by the YAML file.

    Containers:
    <appname>-frontend
    <appname>-backend
    <appname>-database
    
    Images:
    <appname>-frontend
    <appname>-backend
    <appname>-database
    
    Volumes:
    <appname>-frontend
    <appname>-backend
    <appname>-database (dg config)
    <appname>-mongodb (external persistent db)
    

    My 3-TIER MERN App DOCKER-COMPOSE.YAML:

    #
    version: '3'
    #
    #  V O L U M E S -- NOTE: can't use ${APP_NAME} here, and internal are auto-prefixed with <appname>_<name-specified>
    #  -------------
    #
    volumes:
      frontend-volume:  # our Frontend/Client execution volume
        name: "badbank-frontend"
        external: false  # temporary, build specific
    #
      backend-volume:  # our Backend/Server execution volume
        name: "badbank-backend"
        external: false  # temporary, build specific
    #
      database-volume:  # our Database configuration volume
        name: "badbank-database"
        external: false  # temporary, build specific
    #
      mongodb-volume:  # our Database/MongoDB storage volume
        name: "badbank-mongodb"
        external: true  # keep after all Containers are destroyed
    #
    services:
      #
      #  F R O N T E N D
      #  ---------------
      frontend:
        # frontend, client, app ui
        container_name: ${APP_NAME}-frontend
        # use the shared .env file for all 3 Tiers
        env_file:
          - ..env  # shared environment vars for all 3-Tiers
          - ..env.${NODE_ENV:-development}  # "development" to override the "production" vars in the 'Dockerfile'
        build:
          context: frontend  # build from frontend
          args:
            APP_NAME: ${APP_NAME}
            NODE_ENV: ${NODE_ENV:-development}
        ports:
          - '${APP_FRONTEND_PORT:-3000}:${APP_FRONTEND_PORT:-3000}'
        volumes:
          - frontend-volume:/exe/frontend  # temporary volume
        restart: always
        networks:
          - react-express  # frontend to backend connection
        depends_on:
          - backend
      #
      #  B A C K E N D
      #  -------------
      backend:
        # backend, server, app internals
        container_name: ${APP_NAME}-backend
        # use the shared .env file for all 3 Tiers
        env_file:
          - ..env  # shared environment vars for all 3-Tiers
          - ..env.${NODE_ENV:-development}  # "development" to override the "production" vars in the 'Dockerfile'
        build:
          context: backend  # build from backend
          args:
            APP_NAME: ${APP_NAME}
            NODE_ENV: ${NODE_ENV:-development}
        ports:
          - '${APP_BACKEND_PORT:-8080}:${APP_BACKEND_PORT:-8080}'
        volumes:
          - backend-volume:/exe/frontend  # temporary volume
        restart: always
        networks:
          - react-express  # frontend to backend connection
          - express-mongo  # backend to database connection
        depends_on:
          - database
      #
      #  D A T A B A S E
      #  ---------------
      database:
        # database, db, data store, app persistent data
        container_name: ${APP_NAME}-database
        # use the shared .env file for all 3 Tiers
        env_file:
          - ..env  # shared environment vars for all 3-Tiers
          - ..env.${NODE_ENV:-development}  # "development" to override the "production" vars in the 'Dockerfile'
        build:
          context: database  # build from database
          args:
            APP_NAME: ${APP_NAME}
            NODE_ENV: ${NODE_ENV:-development}
        ports:
          - '${APP_DATABASE_PORT:-27017}:${APP_DATABASE_PORT:-27017}'
        volumes:
          - database-volume:/data/configdb  # temporary volume -- NOTE: /data/configdb/ is the path required by MongoDB
          - mongodb-volume:/data/db  # persist our database in this volume -- NOTE: /data/db/ is the path required by MongoDB
        restart: always
        networks:
          - express-mongo  # backend to database connection
    #
    networks:
      react-express:
      express-mongo:
    #
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search