码头集装箱和主机之间的同步文件

情况是; 我有一个static文件夹,它包含一个名为sample_old.txt文件,并使用docker-compose.yml static文件安装到容器中。 然后,我使用docker-compose up启动我的docker服务。 然后调用一个函数在static文件夹内创建一个名为sample_new.txt的新文件。结果,它在静态内生成了新文件(通过使用sudo docker exec -it container_id bash进入容器进行验证)但是问题是,新生成的文件只在不在主机操作系统中的容器中可用。

我怎样才能提供容器内新生成的文件主机? 我可以一直同步目录吗? 我不知道是否可能。 如果可能请提供解决方案。

泊坞窗,compose.yml

 version: "3" services: web: build: context: . dockerfile: Dockerfile command: "python my_celery.py" ports: - "8000:8000" networks: - webnet volumes: - .:/celery_sample - /static:/celery_sample/static networks: webnet: 

目录结构 – 主机操作系统

 . ├── docker-compose.yml ├── Dockerfile ├── __init__.py ├── my_celery.py ├── README.md ├── requirements.txt └── static └── sample_old.txt 

目录结构 – 容器

 . ├── docker-compose.yml ├── Dockerfile ├── __init__.py ├── my_celery.py ├── README.md ├── requirements.txt └── static └── sample_old.txt └── sample_new.txt 

烧瓶fucnction文件生成

 @flask_app.route('/text') def generate_file(): file_dir_path = os.path.join(os.getcwd(), "static") if not os.path.exists(file_dir_path): os.mkdir(file_dir_path) with open(os.path.join(file_dir_path, "sample_new.txt"), "wb+") as fo: fo.write("This is just s test".encode()) return "Writed to file" 

问题在于没有很好定义docker-compose.yml文件; 你错过了一个.static挂载点的定义中,它应该是./static因为它在当前的工作目录中。

 services: web: ... - ./static:/celery_sample/static 

我在工作中使用了伟大的码头同步库,我喜欢它的开发和双向数据同步。 它使用Unison快速同步容器和本地文件之间的文件。