Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read/write to the same file from multiple docker containers

I have 4 containers(java app) in 4 docker hosts. they need to read/write to the same file.

file lock cannot be used because of different OS.

So, I tried to create a .lock file, if one of the 4 containers created the .lock file, the other containers will have to wait. But this still isn't working well. The other containers cannot see the .lock file created by other containers sometimes(not real-time).

Are there other solutions?

like image 870
Dong Avatar asked Jan 26 '26 02:01

Dong


1 Answers

I suggest you rethink your assumptions:

  • What if you have not 4 but 400 containers?
  • What if they are on servers not sharing a file system?

The clean way to do this is to write a very basic server (if the load allows it, this can be nginx+PHP and be done in 10 minutes) that does the file writing, run this in another container and connect to it from the other containers. This will give you:

  • file locking easy and reliable, as the file is seen only by one server
  • scalability
  • clusterability
  • abstraction
like image 77
Eugen Rieck Avatar answered Jan 28 '26 14:01

Eugen Rieck