[英]sync files between docker container and host machine
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
. 我有一个
static
文件夹,它包含一个名为sample_old.txt
文件,并使用docker-compose.yml
将其static
安装到容器中。 Then I start my docker services using docker-compose up
. 然后我使用
docker-compose up
启动docker服务。 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. 然后调用一个函数在
static
文件夹中创建一个名为sample_new.txt
的新文件,结果在static内部生成了新文件(通过使用sudo docker exec -it container_id bash
进入容器进行了验证),但是问题是,新生成的文件仅在容器中可用,而在主机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 泊坞窗,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"
The problem is in the docker-compose.yml
file that is not well defined; 问题出在定义不明确
docker-compose.yml
文件中。 you are missing a .
您缺少一个
.
in the definition of the static
mount point, it should be ./static
because it is in the current working directory. 在
static
挂载点的定义中,它应该是./static
因为它在当前工作目录中。
services:
web:
...
- ./static:/celery_sample/static
I use the great docker-sync library at work and I love it for development and two way data syncing. 我在工作时使用了出色的docker-sync库,我喜欢它用于开发和两种方式的数据同步。 It uses Unison to quickly sync the files between the container and your local files.
它使用Unison快速在容器和本地文件之间同步文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.