简体   繁体   中英

How can I programatically delete files in another Docker container?

I am currently running a Dash (Flask) app in a Docker container that saves user data onto the filesystem within a folder called assets. In another container, I have a Python script called worker.py from where I want to delete a folder that is within the first container. How could I go about doing this? The directory structure is shown below:

(container1):
|- src
|  - app.py
|  - assets
|    - FolderIWantToDelete

(container2):
|- worker
|  - worker.py (the file I am running from)
|  - Dockerfile

I began by typing out shutil.rmtree("Path/To/Folder/I/Want/To/Delete") but got stuck as I believe you can't specify a path into container1 from container2, since Docker containers are isolated from one another.

A thought might be to send an http delete request to container1 from worker.py container2 specifying the delete function to use and path to the folder; however, I am not a web developer so this confuses me.

I can edit this post with more code/information if needed. Thanks for the help!

Solution: As pointed out by users below creating a shared volume within your docker-compose file and assigning it to both containers can solve this problem. Personally, I went with a different route, I removed container1 and ran worker.py from container2 as a thread that was made within app.py.

You can create a docker.network and attack both containers onto it, in which case you can reference the other container's path by IMAGENAME://HOSTNAME/PATH_TO_FILE_TO_DELETE . Here is a guide on how to perform this using docker-compose

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM