Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create hard link to file in a docker volume

I am in the process of refactoring and "dockerizing" a legacy application made of shell scripts, C++ binaries, and various open-sources packages (among which httpd)

Is there a way to create, in a docker container, hard links to files located in a docker volume ?

I am planning on architecting containers as follows:

services:
  legacy-app:
    image: my-legacy-app
    volumes:
      - http-files:/var/www/html/

  httpd:
    image: httpd:2.4
    volumes:
      - http-files:/usr/local/apache2/htdocs/

volumes:
  http-files:

Some of the init scripts in the legacy application create hard links in the /var/www/html/ directory pointing to other files in the file system. They now return the following errors :

ln: creating hard link `/var/www/html/1/application' to `/home/conf/application': Invalid cross-device link

I tried with symlinks and it works. However, the reason hard links were chosen here is to have the file removed once all links to it are removed.

Is there a way to create hard links accross docker volumes ?

like image 471
E. Mabille Avatar asked Mar 27 '19 15:03

E. Mabille


1 Answers

No, there is no way to do what you want.

The problem is that Linux forbids hard links across different file systems. In your case, /home/conf/application belongs to the container's root file system, which is mounted at / (it may be an overlay mount, or aufs, or something else), while the volume (/var/www/html/ and everything under this directory) belongs to another file system, which lies under the host's /var/lib/docker directory (or wherever your volume is located at the host).

But that's not all. To enforce the "no cross-filesystem hard links" restriction, when you try to create a hard link, the kernel checks that the mounts, to which the source and the destination belong, are the same. This means, that even if the source and the destination belong to the same file system, but also belong to different mount points, the creation of hard link is still forbidden.

The consequence is that you can not create a hard link between different Docker volumes, even if these volumes belong to the same file system. Finally, even if you mount the same volume to different locations in the container, you still can not create hard links between these locations, as they still belong to different mounts.

like image 128
Danila Kiver Avatar answered Oct 08 '22 22:10

Danila Kiver