在docker容器和主机之间同步文件(sync files between docker container and host machine)

编程入门 行业动态 更新时间:2024-10-14 06:23:19
在docker容器和主机之间同步文件(sync files between docker container and host machine)

情景是; 我有一个static文件夹,它包含一个名为sample_old.txt文件,并使用docker-compose.yml挂载到容器中。 然后我使用docker-compose up启动我的docker服务。 然后调用一个函数在static文件夹中创建一个名为sample_new.txt的新文件。结果,它在static内部生成了新文件(通过使用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

flask-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"

The scenario is; I have a folder static and it contains one file named sample_old.txt and mounted static to the container using docker-compose.yml . Then I start my docker services using docker-compose up. Then called a function to create a new file named sample_new.txt inside the static folder.As a result, it generated the new file inside static ( verified by getting into the container using sudo docker exec -it container_id bash ) But the problem is, the newly generated file is only available in the container not in host os.

How can I make available the newly generated file inside the container to host? Can I sync the directory all the time? I don't know whether it possible or not. If possible please provide a solution . 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:

directory structure- host os

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

directory structure- container

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

flask-fucnction for file generation

@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

The problem is in the docker-compose.yml file that is not well defined; you are missing a . in the definition of the static mount point, it should be ./static because it is in the current working directory.

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

更多推荐

本文发布于:2023-07-14 14:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1105104.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   主机   文件   docker   sync

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!