Docker TOTD

If you edit a Docker bind mounted file {bash}(-v sourcefile:destitnationfile){/bash} you may have recognized that you face a {bash}stale file handle{/bash} under certain circumstances, especially if you edit the file with Vim on your Docker host. This is because Vim will copy the content of the original edit file to a new one and after you save the changes, Vim exchanges this two files. The result of this operation is, that the inode of the file will be changed. Docker uses the inode of the file for the bind mount and therefore, correctly, the file handle will be stolen after this operation.

Workaround

Just open Vim without specifying the file. Afterwords type in {bash}:set backupcopy=yes{/bash} and open the file you like to edit with {bash}:e yourfile{/bash}. With this option you will edit the original file handle and not a copy.

Source

Github issue Stackoverflow Vim-Config:
'backupcopy' 'bkc'  string  (Vi default for Unix: "yes", otherwise: "auto")
            global
            {not in Vi}
    When writing a file and a backup is made, this option tells how it's
    done.  This is a comma separated list of words.

    The main values are:
    "yes"   make a copy of the file and overwrite the original one
    "no"    rename the file and write a new one
    "auto"  one of the previous, what works best

    Extra values that can be combined with the ones above are:
    "breaksymlink"  always break symlinks when writing
    "breakhardlink" always break hardlinks when writing

    Making a copy and overwriting the original file:
    - Takes extra time to copy the file.
    + When the file has special attributes, is a (hard/symbolic) link or
      has a resource fork, all this is preserved.
    - When the file is a link the backup will have the name of the link,
      not of the real file.

    Renaming the file and writing a new one:
    + It's fast.
    - Sometimes not all attributes of the file can be copied to the new
      file.
    - When the file is a link the new file will not be a link.