将现有卷附加到Docker容器

时间:2019-07-06 14:38:02

标签: docker-compose

我创建了三个卷:

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

我想以某种方式告诉我的docker-compose.yml文件,使用这些现有的卷,而不是创建新的卷。我无法找到有关此ATM的文档。

我的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 

错误:

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

我的问题不是:

Create volume with docker volume with a custom Mountpoint

Docker Create Volume With Mountpoint

2 个答案:

答案 0 :(得分:1)

创建的 dbdata 和 myvolume 驱动器正在使用 docker volume create 命令。下面的撰写没有任何问题。

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

答案 1 :(得分:0)

您必须在卷名后面提及安装点。

示例:

version: "3.7"
services:
  db:
    image: postgres
    volumes:
      - data:/var/lib/postgresql/data
volumes:
  data:
    external: true
您的Yaml中缺少

/var/lib/postgresql/data。添加并尝试。