Achieving Seamless Volume Mounting with Colima on macOS
Author

If you’re a macOS user looking to run Docker, you may have come across the typical approach: Docker for Mac, which relies on a Linux virtual machine (VM) to run Docker containers. But if you’re looking for a more native and efficient container runtime, you might want to consider Colima.

Colima is specifically designed to run containers directly on macOS, using the Docker daemon inside an alpine based and lima based virtual machine. The virtual machine is a sort of alpine-docker bundle for your OSX development environment.

However, you might have faced an issue where mounting volumes didn’t work as expected, leaving you wondering why:

postgresql-colima-database-1  | chown: /var/lib/postgresql/data: Permission denied

When a Colima based virtual machine is created, you can access the configuration file and examine the mountType used

# /Users/<yourUserName>/.colima/deafult/colima.yaml
# Default: virtiofs (for vz), sshfs (for qemu)
mountType: sshfs

After changing the mount type to 9p, which is the recommended and most stable option for vmType QEMU, you may find that mounting volumes still does not work properly.

# Volume mount driver for the virtual machine (virtiofs, 9p, sshfs).
#
# virtiofs is limited to macOS and vmType `vz`. It is the fastest of the options.
#
# 9p is the recommended and the most stable option for vmType `qemu`.
#
# sshfs is faster than 9p but the least reliable of the options (when there are lots
# of concurrent reads or writes).
#
# Default: virtiofs (for vz), sshfs (for qemu)
mountType: 9p

To understand the challenge, it’s important to know that Colima relies on Lima, a hypervisor that enables running Linux containers natively on macOS. When your Colima based virtual machine is created, changing the “colima.yaml” file won’t have any effect because the virtual machine is already established. So, what’s the solution?

The key is to modify the mounting type before creating the virtual machine thanks to this comment, you need to instruct Lima to override the mountType configuration when creating a new virtual machine. Here’s how:

  1. Create a file named “override.yaml.”
  2. In the “override.yaml” file, add the configurations to be overridden
  3. Add this “override.yaml” file to the “.lima/_config” directory.
mountType: 9p
mounts:
  - location: "/Users/yourusername"
    writable: true
    9p:
      securityModel: mapped-xattr
      cache: mmap
  - location: "~"
    writable: true
    9p:
      securityModel: mapped-xattr
      cache: mmap
  - location: /tmp/colima
    writable: true
    9p:
      securityModel: mapped-xattr
      cache: mmap

If you already have a Colima based virtual machine running, follow these steps:

  1. Delete the existing virtual machine: colima delete
  2. Create a new virtual machine: colima start

Now, with these adjustments in place, you can easily and trouble-free mount volumes to your containers. Enjoy a smoother container runtime experience with Colima on macOS.