Linux – Moving files between volumes in Docker is slow

Moving files between volumes in Docker is slow… here is a solution to the problem.

Moving files between volumes in Docker is slow

I’m mounting two volumes this way:

    volumes:
      - /home/username/videos:/var/www/symfony/ftp
      - .docker/data/uploads/videos:/var/www/symfony/public/uploads/videos

Both folders on the host are on the same partition. If I run mv on the host for a file between /home/username/videos and .docker/data/uploads/videos, it’s basically instantaneous. However, if I run mv between var/www/symfony/ftp and /var/www/symfony/public/uploads/videos, it would be painfully slow.

Basically, it seems to be copying files as if it were two completely different physical drives. The host is running Ubuntu 20.04 and the Docker image Alpine 3.13.

Is this behavior expected? Do I know how to improve it if possible? (except for placing both folders on the same volume).

Solution

Behind the scenes, Linux implements these host mounts as two separate binds mounted into the container’s mount namespace. When you run mv, it checks if you can do a simple rename when the file system is the same. Unfortunately, when you encounter bind, install linux treats these as separate filesystems, even bind. The underlying file system is mounted the same.

Related Problems and Solutions