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.
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
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?
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.
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.
Multiple containers can run with the same volume when they need access to shared data. Docker creates a local volume by default.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With