首页 > 解决方案 > 如何使用 docker compose YAML 在 Azure 中挂载 docker 卷

问题描述

我正在尝试在 Azure 中部署 PosgreSQL Docker 容器。为此,我在 Azure 中创建了一个存储帐户和一个文件共享来存储 Docker 卷。

此外,我创建了 Docker Azure 上下文并将其设置为默认值。

要创建卷,我运行:

volume create volpostgres --storage-account mystorageaccount

我可以验证该卷是用docker volume ls.

ID                            DESCRIPTION
mystorageaccount/volpostgres   Fileshare volpostgres in mystorageaccount storage account

但是当我尝试部署时docker compose up,我得到

could not find volume source "volpostgres"

这是不起作用的 YAML 文件。如何解决?如何正确指向音量?

version: '3.7'
services:
  postgres:
    image: postgres:13.1
    container_name: cont_postgres
    networks:
      db:
        ipv4_address: 22.225.124.121
    ports:
      - "5432:5432"
    environment:  
      POSTGRES_PASSWORD: xxxxx
    volumes:
      - volpostgres:/var/lib/postgresql/data
networks:
   db:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 22.225.124.121/24
volumes:
  volpostgres:
     name: mystorageaccount/volpostgres

标签: azuredockerdocker-compose

解决方案


您可以按照此处的步骤操作。并且volumes需要将 docker-compose 文件中的部分更改为:

volumes:
  volpostgres:
    driver: azure_file
    driver_opts:
     share_name: myfileshare
     storage_account_name: mystorageaccount

推荐阅读