简体   繁体   English

使用 docker-compose 在本地挂载 AWS EFS

[英]Locally mount AWS EFS using docker-compose

I am wondering if there is an easy way to make use of Amazon EFS ( Elastic File System ) to be mounted as a volume in a local docker-compose setup.我想知道是否有一种简单的方法可以使用 Amazon EFS(弹性文件系统)在本地 docker-compose 设置中作为卷安装。

Reason being that for local development, volumes created are persisted on my laptop - If I were to change machines, I can't access any of that underlying data.原因是对于本地开发,创建的卷会保留在我的笔记本电脑上 - 如果我要更换机器,我将无法访问任何底层数据。 A cloud NFS would solve this problem as it would be readily available from anywhere.云 NFS 可以解决这个问题,因为它可以从任何地方随时可用。

The AWS documentation ( https://docs.aws.amazon.com/efs/latest/ug/efs-onpremises.html ) seems to suggest the use of AWS Direct Connect / VPN - is there any way to avoid this by opening port 2049 (NFS traffic) in a security group that listens on all IP addresses, and applying that security group to a newly created EFS? AWS 文档( https://docs.aws.amazon.com/efs/latest/ug/efs-onpremises.html )似乎建议使用 AWS Direct Connect / VPN - 有没有办法通过打开端口来避免这种情况2049(NFS 流量)在侦听所有 IP 地址的安全组中,并将该安全组应用于新创建的 EFS?

Here is my docker-compose.yml:这是我的 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"

I am getting the below error:我收到以下错误:

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,nolock,soft: connection refused

Which I interpret to be that my laptop connection is not part of the AWS EFS VPC, hence it is unable to mount the EFS.我认为我的笔记本电脑连接不是 AWS EFS VPC 的一部分,因此无法挂载 EFS。

For added context, I am looking to dockerize a web scraping setup and have the data volume persisted in the cloud so I can connect to it from anywhere.对于添加的上下文,我希望将网络抓取设置 dockerize 并将数据量保存在云中,以便我可以从任何地方连接到它。

EFS assumes nfs4, so: EFS 假设 nfs4,所以:

version: '3.8'

services:

  postgres:
    volumes:
      postgres_data:/var/lib/postgresql/data

volumes: 

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

Of course, the referenced nfs-export/path must exist.当然,引用的 nfs-export/path 必须存在。 Swarm will not automatically create non-existing folders. Swarm 不会自动创建不存在的文件夹。

Make sure to delete any old docker volumes of this faulty kind/name manually (on all swarm nodes!) before recreating the stack:在重新创建堆栈之前,请确保手动删除任何这种错误类型/名称的旧 docker 卷(在所有 swarm 节点上!):

docker volume rm $(docker volume ls -f name=postgres_data -q)

This is important to understand: Docker NFS Volumes are actually only the declaration where to find the data.理解这一点很重要:Docker NFS 卷实际上只是声明在哪里可以找到数据。 It does not update when you update your docker-compose.yml, hence you must remove the volume so any new configuration will appear当您更新 docker-compose.yml 时它不会更新,因此您必须删除该卷,以便出现任何新配置

see output of见输出

docker service ps stack_postgres --no-trunc

for more information why volume couldn't be mountet有关为什么无法安装卷的更多信息

Also make sure you can mount the nfs-export via mount -t nfs4 ...还要确保您可以通过mount -t nfs4 ...挂载 nfs-export

see showmount -e your.efs.ip.address请参阅showmount -e your.efs.ip.address

volumes:
  nginx_test_vol:
    driver_opts:
      type: "nfs"
      o: "addr=fs-xxxxxxxxxxxxxxxxxx.efs.us-east-1.amazonaws.com,rw,nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport"
      device: ":/nginx-test"

This makes me use it very well这让我很好地使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM