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"
The problem is in the docker-compose.yml file that is not well defined; you are missing a . in the definition of the static mount point, it should be ./static because it is in the current working directory.
services:
web:
...
- ./static:/celery_sample/static
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With