How do I create an external volume using docker compose?

Viewed 1404

Basing on this Node-RED tutorial, I'm trying to mount an external volume with the Node-RED files outside the docker machine. I'm using the following docker-compose file:

version: "3.7"

services:
  node-red:
    image: nodered/node-red:latest
    environment:
      - TZ=Europe/Amsterdam
    ports:
      - "2000:1880"
    networks:
      - node-red-net
    volumes:
      - node-red-data:/home/user/node-red1
    
volumes:
  node-red-data:
    
networks:
  node-red-net:

However, even though this file works fine when I run docker-compose up, the volume exists only inside the docker machine. I've tried adding the line external: true in volumes but I get the following error:

ERROR: In file './docker-compose.yml', volume 'external' must be a mapping not a boolean. 

What am I missing? How do I mount an external volume using docker-compose files?

1 Answers

I ended up finding a related question with this answer. There are multiple answers that didn't work for me there (also there's no accepted answer). The syntax that worked was:

  node-red-data:
    driver: local
    driver_opts:
      o: bind
      type: none
      device: /path/external/folder

So the final dockerfile that works after running docker-compose up is:

version: "3.7"

services:
  node-red:
    image: nodered/node-red:latest
    environment:
      - TZ=Europe/Amsterdam
    ports:
      - "2000:1880"
    networks:
      - node-red-net
    volumes:
      - node-red-data:/data
    container_name: node-red

volumes:
  node-red-data:
    driver: local
    driver_opts:
      o: bind
      type: none
      device: "/home/user/node-red1"
networks:
  node-red-net:

Update

If we don't mind having a random name for the volume, the following solution also works fine:

version: "3.7"

services:
  node-red:
    image: nodered/node-red:latest
    environment:
      - TZ=Europe/Amsterdam
    ports:
      - "2000:1880"
    volumes:
      - /home/user/node-red1:/data
    container_name: node-red
Related