![](/img/trans.png)
[英]Use a simple python tcp script to connect two Docker container on the same host machine but got Connection refused error
[英]sync files between docker container and host machine
场景是; 我有一个static
文件夹,它包含一个名为sample_old.txt
文件,并使用docker-compose.yml
将其static
安装到容器中。 然后我使用docker-compose up
启动docker服务。 然后调用一个函数在static
文件夹中创建一个名为sample_new.txt
的新文件,结果在static内部生成了新文件(通过使用sudo docker exec -it container_id bash
进入容器进行了验证),但是问题是,新生成的文件仅在容器中可用,而在主机os中不可用。
如何使容器内部的新生成文件可供托管? 我可以一直同步目录吗? 不知道有没有可能 如果可能,请提供解决方案。
泊坞窗,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
挂载点的定义中,它应该是./static
因为它在当前工作目录中。
services:
web:
...
- ./static:/celery_sample/static
我在工作时使用了出色的docker-sync库,我喜欢它用于开发和两种方式的数据同步。 它使用Unison快速在容器和本地文件之间同步文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.