Shared Volume in Docker Through Vagrant

Shared Volume in Docker through Vagrant

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

Vagrant + Docker + Wordpress

You sure can achieve this, and it's probably just the missing line

define( 'FS_METHOD', 'direct' );

in your wp-config file. You should setup each WordPress outside of the docker environment first IMHO; and then import the database, and WordPress files etc into docker using your Dockerfile(s) as part of staging and deployment, which should be distinct from development (although some of deployment will be shared).

On better ways of managing, I would not put docker inside vagrant if at all possible; it adds unnecessary complexity to that stage of development. I would use vagrant exclusively, focus on getting my provisioning scripts ready (as I use scripts between vagrant and docker), and work via SFTP directly to the vagrant box; committing changes via git. You can then focus your efforts on the necessary code and pull what you need when you need to.

Once it gets to the stage of testing, or staging, I use the provisioner scripts to help me build my docker environment consistently, (probably sharing some of the provisioning code). I Can then pull a specific release from my repo, and build it into my docker image, which I can deploy.

Other alternatives if you really want spin up, delete WordPress, get to grips with the WP-CLI command-line tools, which can install WordPress, plugins, manage updates and install integrity.



Related Topics



Leave a reply



Submit