sync files between docker container and host machine

Viewed 3734

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.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"
2 Answers
Related