Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared Volume in Docker through Vagrant

I have a Vagrant virtualbox which hosts a Docker container. The host machine has a folder which needs to be accessible in the vm and the container:

Host: /host/path => VM: /vagrant/path => Container: /docker/path

Background: /host/path/ holds the development files for a project which are available at container level to ensure automatic reloads when a change was made.

Configuration

Vagrant:

Vagrant.configure("2") do |config|
  config.vm.synced_folder "/host/path", "/vagrant/path"
end

Docker:

docker run -name mycontainer -d -v /vagrant/path:/docker/path my/image

Problem

This configuration works until i reload the vm. For example, when i restart my computer and start the vm with vagrant up, the docker container only recognizes an empty folder in /docker/path. I guess that could be some timing or sequencing issue. /vagrant/path is not empty and has the correct content.

My workaround at the moment is to reload the container after each restart of the vm:

docker rm mycontainer 
docker kill mycontainer 
docker run -name mycontainer -d -v /vagrant/path:/docker/path my/image

That feels wrong. Any ideas?

like image 326
Alp Avatar asked Nov 27 '13 11:11

Alp


People also ask

Can Docker volumes be shared?

You can manage volumes using Docker CLI commands or the Docker API. Volumes work on both Linux and Windows containers. Volumes can be more safely shared among multiple containers. Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.

Can I use Vagrant with Docker?

Vagrant comes with support out of the box for using Docker as a provider. This allows for your development environments to be backed by Docker containers rather than virtual machines. Additionally, it provides for a good workflow for developing Dockerfiles.

Can two Docker containers share the same volume?

Multiple containers can run with the same volume when they need access to shared data. Docker creates a local volume by default.


1 Answers

I had the same issues. The posted solutions didn't fit my requirements.

Here is my solution. If you run more than one container iterate over the cids in /var/lib/vagrant/cids/

The first script disables the docker-deamon container autostart at boot. The second script starts the container by its CID only if it isn't running.

This is working for the initial vagrant up and following vagrant [ up | reload ] --provision

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

$disableAutostart = <<SCRIPT
if [ -e /home/vagrant/docker_autostart_disabled ]
then
  echo "Docker Container autostart already disabled ... "
else
  echo "Disable Docker Container autostart ..."
  echo "DOCKER_OPTS=\"-r=false ${DOCKER_OPTS}\"" > /etc/default/docker
  touch /home/vagrant/docker_autostart_disabled
fi
SCRIPT

$startContainer = <<SCRIPT
CID_FILE_PATH=/var/lib/vagrant/cids/
CID_FILE=$CID_FILE_PATH$(ls $CID_FILE_PATH)
if [ -e $CIDFILE ]
then
  CID=$(cat $CID_FILE)
  if ! $(docker inspect --format "{{.State.Running}}" $CID) 
    then
      echo "Starting Container ..."
      docker start $CID > /dev/null
    else
      echo "Docker Container already running ..."
  fi
else
  echo "No Container to start ... maybe something went wrong ..."
  echo "Keep calm and try vagrant destroy && vagrant up"
fi
SCRIPT

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

... VM cfg Stuff ...

  #Disable Docker autostart container
  config.vm.provision "shell" ,
   inline: $disableAutostart

  config.vm.provision :docker,
     version: "1.0.0"  do |d|
     d.build_image "/container/pseudo",
         args:"-t cdh5/pseudo-base"

     ... more container

     d.run "cdh5/data",
        auto_assign_name: false,
        args:"-v /vagrant/share:/home/student/share -h cdh5-single-node"
  end

      config.vm.provision :shell ,
        inline:  $startContainer,
        run:  "always"

end
like image 142
Dennis Fietz Avatar answered Oct 19 '22 19:10

Dennis Fietz