I have been working on a workaround so that if a container is started and meanwhile a duplicate container is started, so it should not proceed to run rather blocked until first container get exited successfully. To achieve this i used flock but this certainly is not helping me, it works sometimes, i.e, One container aquire flock while another get blocked on it but sometimes both aquire flock and proceed to run. My use case is other container should wait until other container get exited successfully. Here is the code i'm using:
// Flock locks the jiva.lock file. If the file does not exist, it is
// created. If a new process is trying to access the same file it will
// return an error "resource temporarily unavailable".
func Flock(dir string) error {
logrus.Info("Take flock on jiva.lock file")
file, err := os.OpenFile(dir+"/jiva.lock", os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
// golang.org/x/sys/unix
err = unix.Flock(int(file.Fd()), unix.LOCK_EX|unix.LOCK_NB)
if err != nil {
file.Close()
return fmt.Errorf("Failed to flock, error: %v", err)
}
logrus.Infof("flock successful on file: %v", file.Name())
return nil
}
I'm using bind mount so that the file jiva.lock
is get shared bw two containers. This is not working even if i'm running the binaries directly on my system and in docker container most of the time in both cases.I also tried to debug it with strace but it also shows that both have acquired flock successfully. Any help will be appreciated.