Docker for Mac磁盘性能低下的解决办法

最近备份恢复Gogs的时候,发现同样的配置参数在Mac上访问Gogs的用户首页特别慢,大概耗时8秒,而在Linux上面运行的时候不到1秒钟。后来就上网搜索,加上自己试验,最后找到了原因应该是Gogs镜像本身的问题(可能是采用了alpine这一内核,但是具体原因不详,有待进一步实验)。

这个过程中曾经怀疑是Docker运行MySQL过慢的原因,虽然最终发现不是,但是搜索过程中发现了Docker for Mac确实有性能问题,那就是对大量磁盘IO的操作性能会非常差,大概会差10倍。

具体讨论可以看附录的帖子,这里大概摘录一下:

It seems the VM calls the virtio-blk implementation of flush in hyperkit approximately 25000 times. Each of these is implemented currently by an fsync(F_FULLFSYNC) to avoid writes being partially written or re-ordered over a power loss (as recommended by the Apple docs). Unfortunately each fsync(F_FULLFSYNC) seems to take about 10ms which accounts for the slowdown.

I agree that there are important use-cases where data persistence is less important than throughput, especially on developer setups or CI builds where containers are ephemeral. I’ll investigate the possibility of a configuration option.

解决办法

帖子里面也给出了解决办法,就是通过修改配置文件。以下方法适应于17.03.0-CE。

1
2
3
4
5
6
7
8
$ cd ~/Library/Containers/com.docker.docker/Data/database/
git reset --hard
HEAD is now at 5e56922 last-start-time changed at 1487162086
$ echo os > com.docker.driver.amd64-linux/disk/on-flush
$ git add com.docker.driver.amd64-linux/disk/on-flush
$ git commit -s -m 'Use fsync'
[master d0b523f] Use fsync
1 file changed, 1 insertion(+), 1 deletion(-)

附录A. 参考资料

热评文章