How to handle file changes in docker container


I am planning to make my application to container based. But I have a requirement that my app will write some files to specific folder during the runtime of the application and also I want to send some files as part of image.

In this situation when I try to use the volume for the newly created files then the files within the image are getting vanished.

Is there any good solution to it?


This scenario isn’t unlike what happens in the official mysql image.

At runtime, the mysql daemon writes things out to /var/lib/mysql. Using a volume means that folder is blank on firstrun. If you want certain things like tables, users, databases, or data to be present, how do you do it?

The official mysql image has a solution to this problem. Its ENTRYPOINT script will detect a firstrun condition, and has a place for you to put scripts to run to initialize things. Take a look here:

The same approach can apply to other things that aren’t mysql. The simplest implementation might be that your ENTRYPOINT would detect that the volume is empty, and do a simple cp.

Hopefully this helps.