John Smith
John Smith

Reputation: 495

Attaching an existing volume to a docker container

I have created three volumes:

docker volume create -d local-persist -o mountpoint=/home/usr1/mongodb/data --name=mongodbvv1
docker volume create -d local-persist -o mountpoint=/home/usr2/mongodb/data --name=mongodbvv2
docker volume create -d local-persist -o mountpoint=/home/user2/mongodb/data --name=mongodbvv3

I want to somehow tell my docker-compose.yml file, to use these existing volumes which point, over creating new ones. I haven't been able to find documentation on this ATM.

My docker-compose.yml:

version: '3.7'
services:

  mongo:
    container_name: mongodb1
    image: mongo:latest
    command: mongod --port 27017 --bind_ip 127.0.0.1,10.77.0.2
    restart: always
    ports:
        - 27048:27017
    expose:
         - 27017
    volumes: 
      - mongodbvv1: 
    networks:
      mongodvnet:
        ipv4_address: 10.77.0.2

  mongo2:
    container_name: mongodb2
    image: mongo:latest
    command: mongod --port 27018 --bind_ip 127.0.0.1,10.77.0.3
    restart: always
    ports:
        - 27049:27018
    expose:
         - 27018
    volumes: 
      - mongodbvv2:
    networks:
      mongodvnet:
        ipv4_address: 10.77.0.3

  mongo3:
    container_name: mongodb3
    image: mongo:latest
    command: mongod --port 27019 --bind_ip 127.0.0.1,10.77.0.4
    restart: always
    ports:
        - 27050:27019
    expose:
         - 27019
    volumes: 
      - mongodbvv3:
    networks:
      mongodvnet:
        ipv4_address: 10.77.0.4

networks:
  mongodvnet:
    driver: bridge
    ipam: 
     config: 
       - subnet: 10.77.0.0/16

volumes: 
    mongodbvv1:
       external: true
    mongodbvv2:
       external: true
    mongodbvv3:
       external: true 

ERRORS:

ERROR: The Compose file './docker-compose.yml' is invalid because:
value 'device', 'o', 'type' do not match any of the regexes: '^x-'
driver_opts contains an invalid type, it should be an object
volumes 'type' is a required property

No my problem is NOT:

Create volume with docker volume with a custom Mountpoint

Docker Create Volume With Mountpoint

Upvotes: 2

Views: 4501

Answers (2)

KrishOnline
KrishOnline

Reputation: 516

Created dbdata and myvolume drives are using docker volume create command. Below compose worked without any issues.

    version: "3.6"
    services:
      db:
        image: mysql
        ports:
          - "3306:3306"
        restart: always
        environment:
          MYSQL_DATABASE: {your_db}
          MYSQL_ROOT_PASSWORD: {your_password}
        volumes:
          - dbdata:/var/lib/mysql
      api:
        image: {your_image}
        ports:
          - "80:80"
        restart: always
        volumes:
          - myvolume:/app/log
    
    volumes: 
        dbdata:
            # command to inform docker to use an existing volume
            external: true
        myvolume:
            # command to inform docker to use an existing volume
            external: true

Upvotes: 1

Prakash Krishna
Prakash Krishna

Reputation: 1267

You have to mention the mount point after the volume name.

Example :

version: "3.7"
services:
  db:
    image: postgres
    volumes:
      - data:/var/lib/postgresql/data
volumes:
  data:
    external: true

/var/lib/postgresql/data is missing in your yaml. Add it and try.

Upvotes: 2

Related Questions