boot2docker-vagrant icon indicating copy to clipboard operation
boot2docker-vagrant copied to clipboard

Vagrant box stores docker images/containers on same disk at boot2docker

Open joshbenner opened this issue 9 years ago • 1 comments

The typical boot2docker VM mounts boot2docker as an ISO (like a LiveCD), and mounts a separate disk (vmdk) for /var/lib/docker and /var/lib/boot2docker where it stores things like images, containers, and other docker-related state.

This arrangement allows for the boot2docker ISO to be updated without losing all the previously downloaded images or state.

Is it possible for the Vagrantfile to do something similar? If so, we might be able to vagrant destroy then vagrant up and keep all docker state.

joshbenner avatar Mar 05 '15 15:03 joshbenner

The way I handled this (on a separate boot2docker vagrant approach) was to run a docker registry as a container, set up to mirror/cache downloads. Then I can point that at a specific folder that is mounted on the host, rather than inside boot2docker. The other approach would be to alter the boot2docker Vagrant image itself so that the docker state can be mounted externally - not sure how easy this would be though.

Here are the relevant chunks from our Vagrantfile:

  config.vm.synced_folder ".docker-registry-mirror", "/docker-registry-mirror"
  config.vm.network :private_network, ip: "192.168.33.201"

  config.vm.provision :shell do |s|
    s.inline = <<-EOT
      # Disable Docker TLS and enable the registry cache.
      sudo /etc/init.d/docker stop && sleep 5
      echo 'DOCKER_TLS=no' | sudo tee -a /var/lib/boot2docker/profile
      echo 'EXTRA_ARGS="--registry-mirror http://192.168.33.201:5000 --insecure-registry 192.168.33.201:5000"' | sudo tee -a /var/lib/boot2docker/profile
      sudo /etc/init.d/docker start && sleep 5
      # Enable registry cache and pull initial images.
      docker rm "#{DOCKER_REGISTRY}"
      docker run -d --name="#{DOCKER_REGISTRY}" -p 5000:5000 -e MIRROR_SOURCE=https://registry-1.docker.io -e MIRROR_SOURCE_INDEX=https://index.docker.io -e SQLALCHEMY_INDEX_DATABASE=sqlite:////tmp/registry/docker-registry.db -v /docker-registry-mirror:/tmp/registry registry
      # Pull the registry image again, so it's actually cached if we rebuild.
      docker pull registry
      # Any other docker pulls etc should go after this point.
    EOT
  end

  # Make sure registry cache is always running.
  config.vm.provision :shell, run: "always" do |sh|
    sh.inline = <<-EOT
      IS_RUNNING=$(docker inspect --format '{{.State.Running}}' #{DOCKER_REGISTRY} 2> /dev/null)
      if [ "$IS_RUNNING" != "true" ] ; then
        docker start #{DOCKER_REGISTRY}
      fi
    EOT
  end

grugnog avatar May 29 '15 18:05 grugnog