Docker Volume File permission and locking

When a docker volume is shared across multiple containers and users are collaborating on same set of files, how does docker architecture manage the file locking for read and write. Our requirement is to lock the file for one user and keep it open to read for rest in the group in a real time environment similar to web conferencing of sharing screen one at a time. Can this be achieved using a docker plug-in? Thanks for your help.

It doesn’t. Imagine multiple copies of the same process running on the same local system with the same filesystem; the application needs to work out this locking for itself.

(Since it is, in fact, multiple copies of the same process on a shared local filesystem, normal things like flock(2) should work, though.)

Hi there ! So i was using flock so that two containers can’t use the same file at the same time on shared volume.i.e, if one container proceeds with flock then other container should be blocked but it seems like only 4 out of 10 times one of the container is blocked on flock whereas 6 times flock succeeds for both the containers. What could be the reason behind this and how can i debug this ?

1 Like

Same thing happened here. I use FileLock in Jave to deal with concurrent modification on share file. It not works.

public void modifyFile() {

    try {
        File file = new File("/tmp/fileToLock.dat");

        // Creates a random access file stream to read from, and optionally to write to
        FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

        // Acquire an exclusive lock on this channel's file (blocks until lock can be retrieved)
        FileLock lock = null;

        // Attempts to acquire an exclusive lock on this channel's file (returns null or throws
        // an exception if the file is already locked.
        try {
            lock = channel.tryLock();
            if (null != lock) {
                List<String> fileToString = FileUtils.readLines(file, StandardCharsets.UTF_8);
                long l = 0l;
                if (null != fileToString && fileToString.size() > 0) {
                    l = Long.valueOf(fileToString.get(fileToString.size() - 1));
                }
                l++;
                FileUtils.writeStringToFile(file, String.valueOf(l) + "\r\n", StandardCharsets.UTF_8, true);
            }
        } catch (OverlappingFileLockException e) {
            // thrown when an attempt is made to acquire a lock on a a file that overlaps
            // a region already locked by the same JVM or when another thread is already
            // waiting to lock an overlapping region of the same file
            System.out.println("Overlapping File Lock Error: " + e.getMessage());
            channel.close();
        }

        // release the lock
        if (null != lock) {
            lock.release();
        }
        // close the channel
        channel.close();

    } catch (IOException e) {
        System.out.println("I/O Error: " + e.getMessage());
    }

}