Shared Volume in Docker through Vagrant

2019-03-13 08:58发布

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?

3条回答
疯言疯语
2楼-- · 2019-03-13 09:50

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
查看更多
在下西门庆
3楼-- · 2019-03-13 09:50

Not sure about the why of your problem. I'm not sure how the VM persist state of running programs during reboots, so I'd say the safe thing to do is to reload the container anyway when you vagrant up.

You can automate the reloading of the container using vagrant provisioning (in your Vagrantfile):

config.vm.provision :shell, :inline => <<<EOF
docker kill mycontainer
docker rm mycontainer
docker run -name mycontainer -d -v /vagrant/path:/docker/path my/image
EOF
查看更多
戒情不戒烟
4楼-- · 2019-03-13 09:58

I can confirm I have the same issues regarding reboots that do not run the provisioning. This happens because when the docker daemon is brought up after a reboot, the synced folder source is still empty (that is, the folder syncing phase runs after docker is launched at startup). Another option I found is to simply restart the docker service in the host with sudo service docker restart.

查看更多
登录 后发表回答