How to connect two virtual machines (postgreSQL and tomee) using Vagrant?

I am trying to create a virtual environment with Virtualbox and Vagrant. The goal is to have access from the pc browser to an application running on a "virtual machine" server that interacts with the "db" virtual machine.

I managed to create virtual machines.

The first one works with running Apache Tomee, and I can access it through my browser. It is working correctly.

The second works with running Postgres, and I can access it through my browser. It also works correctly.

When I try to connect VM 'server' with 'db', I get an http 500 error (only when I try to perform a db operation with the application), in particular:

"Connection to localhost: 2222 refused. Check that the hostname and port 
 are correct and that the postmaster is accepting TCP / IP connections.  
 javax.faces.webapp.FacesServlet.service (FacesServlet.java:229) " 

I tested two machines from the pc browser, and they work correctly (in fact, in the db machine I see added objects).

This is the Vagrantfile:

config.vm.define "server" do |node|
    node.vm.hostname = "server"    
    node.vm.network "private_network", ip: "10.12.1.101",       virtualbox__intnet: true

  [...]

    node.vm.network "forwarded_port", guest: 22, host: 2211, id: 'ssh', auto_correct: true
    node.vm.network "forwarded_port", guest: 8080, host: 2212

    node.ssh.forward_agent = true

[...]
 end

config.vm.define "client" do |node|
    node.vm.hostname = "client"    
    node.vm.network "private_network", ip: "10.12.1.201", virtualbox__intnet: true

   [...]

    node.vm.network "forwarded_port", guest: 22, host: 2221, id: 'ssh', auto_correct: true
    node.vm.network "forwarded_port", guest: 5432, host: 2222

end

In the tomee.xml file present in apache-tomee / conf, I specified the same db connection code that I use in testing (which works correctly from a virtual environment)

<tomee>
  <Resource id="PostgreSQL Database" type="DataSource">
          JdbcDriver   org.postgresql.Driver
      JdbcUrl  jdbc:postgresql://localhost:2222/music
      UserName     postgres
      Password     postgres
      JtaManaged      true
      DefaultAutoCommit   false
    </Resource>
</tomee>  

In db VM I changed both postgresql.conf and pg_hba.conf by adding

listen_addresses = '*'

in the first and

host all all 0.0.0.0/0 md5

in the second. I do not understand why, when tomee is running on my computer, I can access two virtual machines, but the "server" cannot communicate with "db".

"sudo netstat -tulpn | grep postgres" db VM

tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 5743/postgres
tcp6 0 0: 5432: * LISTEN 5743/postgres

+4
1

localhost IP- , ( ). .

+1

All Articles