andrchi/vagrant-provider

By andrchi

Updated 6 months ago

Docker images that can be used as a provider for Vagrant as a Linux development environment.

Image
Operating Systems

109

Docker as a provider for Vagrant

These Docker images are designed to be used by Vagrant to set up a virtualized Linux development environment. In particular, they have proved useful for quickly setting up an Ubuntu x86_64 (amd64) environment on a Mac running Apple Silicon. This is made possible by the fact that Docker Desktop includes QEMU under the hood, which enables it to run a container whose architecture does not match the host's architecture.

The Dockerfiles that were used to build these images can be found at https://github.com/andrewchi/vagrant-docker-provider, a fork of the excellent work by John Rofrano in his vagrant-docker-provider.

Modifications from Rofrano's project are minimal:

  • Update operating system build steps in the Dockerfiles for Ubuntu 22.04 and Debian 12
  • Build separate amd64 (x86_64) and arm64 images, rather than creating multi-platform images

Instructions

Requirements:

Here is a sample Vagrantfile for launching the Ubuntu x86_64 container on a Mac with Apple Silicon.

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.hostname = "ubuntu-x86"

  ############################################################
  # Provider for Docker on Intel or ARM (aarch64)
  ############################################################
  config.vm.provider :docker do |docker, override|
    override.vm.box = nil
    docker.image = "andrchi/vagrant-provider:ubuntu-x86"
    docker.remains_running = true
    docker.has_ssh = true
    docker.privileged = true
    docker.volumes = ["/sys/fs/cgroup:/sys/fs/cgroup:rw"]
    docker.create_args = ["--cgroupns=host"]
    # Uncomment to force arm64 for testing images on Intel
    # docker.create_args = ["--platform=linux/arm64", "--cgroupns=host"]
    # Uncomment to force Intel for testing images on ARM / Apple Silicon
    docker.create_args = ["--platform=linux/amd64", "--cgroupns=host"]
  end

  # Install Docker and pull an image
  # config.vm.provision :docker do |d|
  #   d.pull_images "alpine:latest"
  # end

end

Place the Vagrantfile in a directory on the host (e.g., macOS). This directory will be shared with the container. Once the Vagrantfile is in place, cd to that directory and use the following commands.

$ vagrant up       # run the container (create if necessary)
$ vagrant ssh      # login to the container
                   # guest's /vagrant directory == host's current working directory
$ vagrant halt     # stop the container
$ vagrant destroy  # delete the container

Docker Pull Command

docker pull andrchi/vagrant-provider