Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vagrant mapped folder appears to erase contents of Docker volume #7517

Closed
AgentCormac opened this issue Jun 23, 2016 · 7 comments
Closed

Vagrant mapped folder appears to erase contents of Docker volume #7517

AgentCormac opened this issue Jun 23, 2016 · 7 comments

Comments

@AgentCormac
Copy link

AgentCormac commented Jun 23, 2016

Vagrant version

Vagrant 1.8.1

Host operating system

win7

Guest operating system

Centos 7

Vagrantfile

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'docker'
DOCKER_HOST_NAME = "dockerhost"
DOCKER_HOST_VAGRANTFILE = "./host/Vagrantfile"

Vagrant.configure("2") do |config|
  config.vm.define "postgres" do |v|
    v.vm.provider "docker" do |d|
      d.image = "postgres:9.6"
      d.name = "postgres"
      d.env = {
        :POSTGRES_USER      => "root",
        :POSTGRES_PASSWORD  => "root",
        :POSTGRES_DB         => "dockertest"
      }
      d.ports =["5432:5432"]
      d.remains_running = "true"
      #map postgres data folder to (shared) folder on dockerhost
      d.volumes=["/home/vagrant/workspace/shared-data/postgres/data:/var/lib/postgresql/data"]
      d.vagrant_machine = "#{DOCKER_HOST_NAME}"
      d.vagrant_vagrantfile = "#{DOCKER_HOST_VAGRANTFILE}"
    end
  end
end

host\Vagrantfile

ENV["VAGRANT_DETECTED_OS"] = ENV["VAGRANT_DETECTED_OS"].to_s + " cygwin"
FORWARD_DOCKER_PORTS='true'
Vagrant.configure(2) do |config|
  config.vm.provision "docker"

  # The following line terminates all ssh connections. Therefore
  # Vagrant will be forced to reconnect.
  # That's a workaround to have the docker command in the PATH
  #Clear any existing ssh connections
  ####NOTE: ps aux seems to give variable results depending on rbox -> process number can be ####first #or second causing provision to fail!!!
  config.vm.provision "clear-ssh", type: "shell", inline:
        "ps aux | grep 'sshd:' | awk '{print $2}' | xargs kill" #Every other box

  $script = <<SCRIPT
  echo Environment information.... 
  cat /etc/issue
  cat /etc/*-release
  uname -a
  mkdir -p /home/vagrant/workspace/shared-data/postgres/data
  chown -R vagrant:vagrant /home/vagrant/workspace
SCRIPT

  config.vm.provision "info", type: "shell", inline: $script

  config.vm.define "dockerhost"
  config.vm.box = "bento/centos-7.2"

  #Project specific config needed on docker host
  config.vm.network "forwarded_port",guest: 8080, host: 8080    # Tomcat
  config.vm.network "forwarded_port",guest: 8000, host: 8000    # Tomcat debug. Dont think this is exposed by the image
  config.vm.network "forwarded_port",guest: 3306, host: 3306    # MySQL
  config.vm.network "forwarded_port",guest: 5432, host: 5432    # PostgreSQL
  config.vm.network "forwarded_port",guest: 9080, host: 9080    # Jenkins -> Map to 8080 in container
  config.vm.network "forwarded_port",guest: 50000, host: 50000  # Jenkins

  config.vm.synced_folder "C:/dev/vagrant/workspace/SpringMVCHibernate/shared-data/mysql", "/home/vagrant/workspace/shared-data/mysql"
  config.vm.synced_folder "C:/dev/vagrant/workspace/SpringMVCHibernate/shared-data/postgres/data", "/home/vagrant/workspace/shared-data/postgres/data"
  config.vm.synced_folder "C:/dev/vagrant/workspace/SpringMVCHibernate/shared-data/tomcat", "/home/vagrant/workspace/shared-data/tomcat"
  config.vm.synced_folder "C:/dev/vagrant/workspace/SpringMVCHibernate/shared-data/jenkins", "/home/vagrant/workspace/shared-data/jenkins"

  config.vm.provider "virtualbox" do |vb|
    vb.name = "dockerhost"
  end

  config.vm.provision "shell", inline: <<-SHELL
    sudo chkconfig docker on
    sudo yum install -y vim
  SHELL
end

Debug output

https://gist.github.com/AgentCormac/0bde7c685cabcc67fac7f6a5f453ba66

Expected behavior

Postgres database file should be in C:\dev\vagrant\workspace\SpringMVCHibernate\shared-data\mysql\data

Actual behavior

Host
C:\dev\vagrant\workspace\SpringMVCHibernate\shared-data\mysql\data is empty

Dockerhost
/home/vagrant/workspace/shared-data/postgres/data is empty

Container
/var/lib/postgresql/data is empty

Steps to reproduce

  1. vagrant up
@sethvargo
Copy link
Contributor

Hi @AgentCormac

Thank you for opening an issue. I'm having trouble following though, because you seem to switch between mysql and postgres. Can you answer the following questions:

  1. What is in C:/dev/vagrant/workspace/SpringMVCHibernate/shared-data/postgres/data. Can you do a dir or ls on that directory and send the results?

  2. SSH into the host, run these commands, and share the results:

    ls -al /home/vagrant/workspace/shared-data
    ls -al /home/vagrant/workspace/shared-data/postgres/data
    
  3. It's also worth noting that you are on an older version of Vagrant (1.8.1). Vagrant 1.8.4 is the latest at the time of this writing. Vagrant 1.8.4 brings docker-exec, which will allow you to attach to the container and inspect it as running.

@AgentCormac
Copy link
Author

AgentCormac commented Jun 25, 2016

@sethvargo. Thanks for taking a look at this.
I have upgraded to Vagrant 1.8.4 with the same result:

C:/dev/vagrant/workspace/SpringMVCHibernate/shared-data/postgres/data is empty.
[vagrant@localhost ~]$ cd workspace/
[vagrant@localhost workspace]$ cd shared-data/
[vagrant@localhost shared-data]$ ls -al
total 4
drwxr-xr-x. 6 vagrant vagrant   60 Jun 25 12:48 .
drwxr-xr-x. 3 vagrant vagrant   24 Jun 25 12:48 ..
drwxrwxrwx. 1 vagrant vagrant 4096 Jun 25 09:20 jenkins
drwxrwxrwx. 1 vagrant vagrant    0 Jun 23 19:11 mysql
drwxr-xr-x. 3 vagrant vagrant   17 Jun 25 12:48 postgres
drwxrwxrwx. 1 vagrant vagrant    0 Jun 24 21:45 tomcat
[vagrant@localhost shared-data]$ ls -al postgres/
total 8
drwxr-xr-x. 3 vagrant vagrant   17 Jun 25 12:48 .
drwxr-xr-x. 6 vagrant vagrant   60 Jun 25 12:48 ..
drwxrwxrwx. 1 vagrant vagrant 8192 Jun 23 21:14 data
[vagrant@localhost shared-data]$ ls -al postgres/data/
total 8
drwxrwxrwx. 1 vagrant vagrant 8192 Jun 23 21:14 .
drwxr-xr-x. 3 vagrant vagrant   17 Jun 25 12:48 ..

@AgentCormac
Copy link
Author

Creating a new file in the C:/dev/vagrant/workspace/SpringMVCHibernate/shared-data/postgres/data directory sees the file mirrored in the mapped folder on the dockerhost. Its almost as if the container spins up, created the data directory and contents (postgres DB files) and then Vagrant maps the host folder (empty) over the top of the populated container folder making it and the dockerhost folder empty the same as the host.

@sethvargo
Copy link
Contributor

Hi @AgentCormac

I think this is how vbox shared folders work. Have you tried using rsync or nfs instead?

@ghola
Copy link

ghola commented Jul 7, 2016

I had a similar issue in which i ended up with duplicated shared folders and duplicated volumes, but in my case i used SMB. Vagrant was defining it's default SMB folder (with a unique random name) and then there was the one defined by me.

The catch is that defining a shared folder should be done in the docker Vagrantfile and not in the host one, because the host could be used by multiple docker Vagrantfiles (possibly in different projects). This is probably why for SMB vagrant ensures that each mount inside the host VM gets a unique name. Furthermore, in my case passing that shared folder as a volume to the container was already done by vagrant so removing my own volume mapping did the trick. Try playing around with these ideas and see if they work for you.

@chrisroberts chrisroberts added this to the 1.9 milestone Oct 3, 2016
@chrisroberts
Copy link
Member

Hi there,

It looks like this has been resolved within a previously shipped version of Vagrant so I am now closing this issue. If the original issue was not fully resolved, please reopen this issue or create a new one.

Cheers!

@ghost
Copy link

ghost commented Apr 1, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants