Delete and recreate a bind-mounted directory on the host causes abnormal access within the container

Expected behavior

If a directory is volume mounted, all changes on the host should by visible in the docker container.

Actual behavior

If a directory within the mounted volume is deleted, then re-created with the same name, docker no longer sees the directory contents and cannot write to the directory any more.

Docker version

Client: Docker Engine - Community
 Version:           24.0.2
 API version:       1.43
 Go version:        go1.20.4
 Git commit:        cb74dfc
 Built:             Thu May 25 21:52:22 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          24.0.2
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.4
  Git commit:       659604f
  Built:            Thu May 25 21:52:22 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.21
  GitCommit:        3dce8eb055cbb6872793272b4f20ed16117344f8
 runc:
  Version:          unknown
  GitCommit:        
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Yes. If you delete the folder on host the bind mount breakes and is not re-established when you re-create the folder (probably because internal low-level ids change).

You need to restart your service/container.

Is this considered an issue? Besides restarting the container, are there any other solutions?

I don’t think it’s considered an issue. You can’t change the wheels on a car while it is driving.

Instead of deleting the folder, why not just delete/replace the files inside?

Because I want to mount /dev/serial into the container, but if I unplug all serial devices, the /dev/serial folder will be deleted by udev. In this case, if I plug in a device again, the container won’t be able to recognize it.
The premise is that I don’t want to mount the entire /dev directory.

/dev/serial isn’t even a real folder.

Linux might have the option with “udev rules” to trigger a script when the device is available, then it could automatically restart the container.

I understand. Thank you for your help!