简体   繁体   English

在Docker容器和主机之间同步文件

[英]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.

相关问题 使用简单的python tcp脚本连接同一台主机上的两个Docker容器,但出现连接被拒绝的错误 - Use a simple python tcp script to connect two Docker container on the same host machine but got Connection refused error docker-为开发和生产组成相同的配置,但仅在开发中启用主机和容器之间的代码共享 - docker-compose same config for dev and production but enable code sharing between host and container only in development 将Docker容器环回绑定到主机环回 - Bind docker container loopback to the host loopback 将外部文件挂载到Docker容器中 - Mount external files into Docker container 主机上的 Kafka 消费者未访问在 docker 中运行的 Kafka 生产者的消息 - Kafka Consumer on host machine not accessing messages of Kafka Producer running in docker 如何使Docker容器与本地主机上的geth对话 - How to make a Docker container talk to geth on local host 通过 python 在 docker 容器外写入文件 - Write files outside of a docker container via python Django静态文件未复制到Docker容器上的saticfiles文件夹中 - django static files are not copied to saticfiles folder on docker's container 在 Docker 容器中运行时,Django 静态文件不提供服务 - Django static files not serving when running inside Docker container 如何访问同一 Docker 容器中的其他文件? - How can I access other files in the same Docker container?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM