情景是; 我有一个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.txtflask-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.txtdirectory structure- container
. ├── docker-compose.yml ├── Dockerfile ├── __init__.py ├── my_celery.py ├── README.md ├── requirements.txt └── static └── sample_old.txt └── sample_new.txtflask-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/staticThe 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更多推荐
发布评论