Shared Volume in Docker through Vagrant

前端 未结 3 1397
独厮守ぢ
独厮守ぢ 2021-02-06 02:13

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/p         


        
相关标签:
3条回答
  • 2021-02-06 02:38

    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.

    0 讨论(0)
  • 2021-02-06 02:45

    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
    
    0 讨论(0)
  • 2021-02-06 02:59

    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
    
    0 讨论(0)
提交回复
热议问题