使用docker-compose在本地安装AWS EFS

问题描述

我想知道是否有一种简单的方法可以将Amazon EFS(弹性文件系统)作为卷安装在本地docker-compose设置中。

原因是为了进行本地开发,创建的卷将保留在我的笔记本电脑上-如果要更换机器,则无法访问任何该基础数据。云NFS可以解决此问题,因为它可以随时随地使用。

AWS文档(https://docs.aws.amazon.com/efs/latest/ug/efs-onpremises.html)似乎建议使用AWS Direct Connect / VPN-是否可以通过在侦听所有IP地址的安全组中打开端口2049(NFS流量)来避免这种情况,并将该安全组应用于新创建的EFS?

这是我的docker-compose.yml:

version: "3.2"
 
services:
  postgres_db:
    container_name: "postgres"
    image: "postgres:13"
    ports:
      - 5432:5432
    volumes:
      - type: volume
        source: postgres_data
        target: /var/lib/postgresql/data
        volume:
          nocopy: true
    environment: 
      POSTGRES_USER: 'admin'
      POSTGRES_PASSWORD: "password"
 
volumes: 
  postgres_data:
    driver_opts:
      type: "nfs"
      o: "addr=xxx.xx.xx.xx,nolock,soft,rw"
      device: ":/docker/example"

我收到以下错误

ERROR: for postgres_db  Cannot start service postgres_db: error while mounting volume '/var/lib/docker/volumes/flowstate_example/_data': Failed to mount local volume: mount :/docker/example:/var/lib/docker/volumes/flowstate_example/_data,data: addr=xxx.xx.xx.xx,soft: connection refused

我解释为我的笔记本电脑连接不属于AWS EFS VPC的一部分,因此无法安装EFS。

为增加上下文,我正在寻求对网络抓取设置进行泊坞处理,并将数据量持久存储在云中,以便可以从任何地方连接到它。

解决方法

EFS 假设为 nfs4,因此:

volumes: 
  postgres_data:
    driver_opts:
      type: "nfs4"
      o: "addr=xxx.xx.xx.xx,nolock,soft,rw"
      device: ":/docker/example"

确保在重新创建堆栈之前手动删除任何此类/名称错误的旧 docker 卷:

docker volume ls
docker volume rm