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?
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
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
):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
.