Replace docker desktop on macOS with Vagrant and Virtual Box

Thomas Bédu
Pictet Technologies Blog
4 min readMay 5, 2022

Docker Desktop is no longer free for company (https://www.docker.com/products/docker-desktop). In this article you will see a possible replacement of docker desktop for macOS.

Inspiration and disclaimer

I have used Docker since version 0.5.x back when Docker for desktop did not exist. Despite that, we managed to use docker on our Dev machine very well. That’s why I think it’s quite possible to remake a Dev environment without docker desktop.

There are several ways to do this desktop Docker switch. I have added here the link of a very well-done article that inspired me a lot:

The scripts here have been tested on macOS Big Sur 11.6, Vagrant 2.2.16, Virtual Box 6.1, Docker 20.10.10 and Brew 3.3.4

To test this, you will first need to uninstall docker desktop. The internet is your friend for that (https://stackoverflow.com/questions/44346109/how-to-easily-install-and-uninstall-docker-on-macos)

What we want to achieve?

To work, the docker engine can only be launched on a Linux machine. To be able to do this on a MacBook we use a virtual machine (VM).

What we want to do

To ease the installation of the VM, we will use Vagrant (https://www.vagrantup.com/). Vagrant is a great tool that allows us to do infra-as-code on VMs. We will use it to define a configuration that will be used to drive Virtual Box and thus simplify the life cycle of the VM (initialisation, configuration, provisioning and start/stop/destroy).

Install Pre-requisite on your macOS machine

The complete sources of this article are available here: https://gitlab.com/bedouin53/docker-desktop-medium, all the files presented here refer to this project.

To make Docker work, we need a Linux VM. To ease the process of creating and handling a Linux VM, we will use Vagrant, which is doing it out of the box by using infra-as-code configuration.

To ease the install process, we use homebrew (https://brew.sh/) to get all these dependencies.

After this script, you should have all the requirements to launch the VM.

Vagrant Up

Vagrant is a great tool to set up development environment virtual machine. The following file is named Vagrantfile, this file describe the configuration of the virtual machine that Virtual Box will have to create. In this example we use an ubuntu 20.04 LTS image, we could imagine using another version or distribution. You can search for several Vagrant boxes here : https://app.vagrantup.com/boxes/search.

By running vagrant up on the same folder of this file, Vagrant will download the box (ubuntu 20.04), configure the VM network, configure VM resources (CPUs, memory, disk size) and will mount the /Users folder into the VM.

After the initial configuration of the VM and when the machine is ready, vagrant will call the provison.sh file (line 29). This file is written for ubuntu 20.04 and will do the initial provisioning of the VM.

Step 1: install docker-ce with the recommended script from https://docs.docker.com/engine/install/ubuntu/

Step 2: configure docker-ce to listen on the TCP port 2375.

The step 3 is optional; it allows for the addition of certificates of a potential enterprise docker repository.

Step 4 is also optional; it enables you to start a portainer image, which is an excellent tool for people who would need a GUI.

With these 2 scripts and a vagrant up, you should have a VM up and running, containing docker-ce ready to receive calls on port 2375.

Configure your macOS machine to call the VM

The last thing to do is to set up your machine to redirect docker calls to the VM.

After this script, you will be able to run any docker or docker-compose command from your mac, and these commands will be sent to your local VM.

Opening and possible solution

This solution enables you to have a VM ready to use. This is a good first point, but the problem is that each developer should maintain it up to date. A solution would be that instead of doing the provisioning of the VM via a script at initialisation, we could provision the VM with puppet/chief/ansible and deploy a configuration server so that all VMs are automatically updated.

Troubleshooting

docker-credential-desktop

error getting credentials - err: exec: "docker-credential-desktop": executable file not found in $PATH, out: ``

This error is linked to a previous docker configuration. To correct this error:

Known issue with postgre

The mounting system of virtual box is not perfect. By default, when mounting a folder, the synced folder in the VM is linked to the user vagrant:vagrant uid: 1000, gid: 1000.

Some images need to be run with a very specific user and check the permission of the file system like postgres:12.5 very closely.

Because the filesystem is linked to vagrant, this image will not start.

A workaround (and a good practice) is to use internal volumes instead of linking them to the host machine:

version: '2'
services:
db:
image: postgres:12.5
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:

--

--

Thomas Bédu
Pictet Technologies Blog

Software Craftsman at Pictet Technologies. I love tech, DevOps, software craftsmanship and agility has changed my life of developer.