在 docker 容器和主机之间同步文件
sync files between docker container and host machine
场景是;我有一个文件夹 static
,它包含一个名为 sample_old.txt
的文件,并使用 docker-compose.yml
将 static
挂载到容器中。然后我使用 docker-compose up
启动我的 docker 服务。然后调用一个函数在 static
folder.As 中创建一个名为 sample_new.txt
的新文件 结果,它在静态中生成了新文件(通过使用 sudo docker exec -it container_id bash
进入容器进行验证) 但问题是,新生成的文件只能在容器中可用,而不是在 host os.
如何使容器内新生成的文件对 host 可用?我可以一直同步目录吗?我不知道 possible 与否。如果possible 请提供解决方案.
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:
目录结构- host os
.
├── 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 函数
@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-sync library at work and I love it for development and two way data syncing. It uses Unison 来快速同步容器和本地文件之间的文件。
问题出在docker-compose.yml
文件中没有明确定义;您在 static
挂载点的定义中缺少 .
,它应该是 ./static
因为它在当前工作目录中。
services:
web:
...
- ./static:/celery_sample/static
场景是;我有一个文件夹 static
,它包含一个名为 sample_old.txt
的文件,并使用 docker-compose.yml
将 static
挂载到容器中。然后我使用 docker-compose up
启动我的 docker 服务。然后调用一个函数在 static
folder.As 中创建一个名为 sample_new.txt
的新文件 结果,它在静态中生成了新文件(通过使用 sudo docker exec -it container_id bash
进入容器进行验证) 但问题是,新生成的文件只能在容器中可用,而不是在 host os.
如何使容器内新生成的文件对 host 可用?我可以一直同步目录吗?我不知道 possible 与否。如果possible 请提供解决方案.
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:
目录结构- host os
.
├── 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 函数
@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-sync library at work and I love it for development and two way data syncing. It uses Unison 来快速同步容器和本地文件之间的文件。
问题出在docker-compose.yml
文件中没有明确定义;您在 static
挂载点的定义中缺少 .
,它应该是 ./static
因为它在当前工作目录中。
services:
web:
...
- ./static:/celery_sample/static