Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x docker容器和主机之间的同步文件_Python 3.x_Docker_Flask_Docker Compose_Docker Volume - Fatal编程技术网

Python 3.x docker容器和主机之间的同步文件

Python 3.x docker容器和主机之间的同步文件,python-3.x,docker,flask,docker-compose,docker-volume,Python 3.x,Docker,Flask,Docker Compose,Docker Volume,情况是:;我有一个文件夹static,其中包含一个名为sample\u old.txt的文件,并使用docker compose.yml将static安装到容器中。然后我使用docker compose-up启动docker服务。然后调用一个函数在static文件夹中创建一个名为sample\u new.txt的新文件。结果,它在static中生成了新文件(通过使用sudo docker exec-it container\u id bash进入容器进行验证),但问题是,新生成的文件仅在容器中可

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

如何将容器中新生成的文件提供给宿主?我可以一直同步目录吗?我不知道这是否可能。如果可能,请提供解决方案。

docker 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
用于文件生成的烧瓶功能

@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
,因为它位于当前工作目录中

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

Dockerfile或docker-compose.yml是否有其他方法可供选择?