Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring file changes in Docker volumes

I have a docker container that is running a python script: waiting for input requests and processing data accordingly.

Since I am using docker for development, I would like that, whenever I change the source code of that python file (in my machine, not the container), the container would stop the python script and relaunch it with the new code. Because right now I have to manually stop the container and relaunch it. I could also monitor the file changes on my side (rather than inside the container) but I would like to avoid that and do it within the container itself.

I am using docker-compose's volumes option to share the source code between my FS and the container's.

To monitor the file changes, I've been trying to use the watchmedo shell utility from the the watchdog python module. I just have this weird problem that I can't notice the file changes of that python source file unless I am editing it from the inside of the container and not in my local FS, even though they are mounted with the volumes option.

I get the feeling that this is something to do with how docker works and maybe the volumes thing too. I've been trying to read up on it online, but didn't get much luck. Any ideas? I'm totally stuck!

EDIT: Here's a gif that better explains it. The top to panes are connected to the same container and the bottom two to my local machine. All the panes are pointed to the same folder. gif

like image 346
nunos Avatar asked May 26 '15 11:05

nunos


People also ask

Do Docker containers retain file changes?

Modifications to your container filesystem persist until you remove the container. Modifications in volumes persist until you remove the volume.

How can I see the contents of a docker volume?

You can use the docker volume ls command to view a list of data volumes. Use the docker volume inspect command to view the data volume details.

Which command is used to inspect changes on a containers file system?

Using Docker Diff The Docker CLI has a built-in command for this purpose. Running docker diff will enumerate all the changes made to files and directories within a particular container. It accepts the ID or name of the container you want to inspect.


1 Answers

You could have your container run something like this ( need inotify installed ):

while true
do
        inotifywait -e create -e modify  /path/to/python/script
        pkill python
        python /path/to/python/script
done

Basically wait for changes on the file, kill python on the machine, run script again. If the python script is not running in the background/deamonized in any way you could use an & like so: the python /path/to/python/script &

Put this into run.sh, add something like this to your Dockerfile

COPY run.sh /run.sh
CMD ["bash", "-l", "/run.sh"]

... and you should be good.

like image 51
Armin Braun Avatar answered Oct 14 '22 05:10

Armin Braun