How can i view the process running on vagrant from my browser - ruby-on-rails

I have installed Vagrant and Virtualbox-4.3 on my Ubuntu system and windows 8 system and i want to run the ruby on rails project in the vagrant. How can i tunnel the project through the browser which in my machine.
If i run the rails project on port 3000 on vagrant. how can i see the result from my browser?
Do let me know to tunnel the result through the browser and to set the static ip for vagrant. Thanks in advance.

There are two ways, the first doesn't require any setup but, it will change every time that you start up your vagrant box.
ssh into you box (vagrant ssh)
Get the IP address of the box (ifconfig)
Open your web browser and go to <ip_address>:3000
A better way to do this is to specify a forwarded port. This way, you can always access your app in the same way. Add the config.vm.network network line like so,
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 3000, host: 3000
end
After adding that line, you'll need to reboot the box. You can do this by running vagrant reload. Once that process finishes, start up your app again. You will now be able to connect to your app by going to http://127.0.0.1:3000

Related

Docker nodejs server running in VirtualBox guest OS (Ubuntu Server 20.04): access via host (Win10) localhost

I'm using docker inside VirtualBox (Ubuntu Server 20.04), since I cannot use Docker Desktop in the host (Windows 10).
I have a docker nodejs container on port 3000. In my host I can access it through 192.168.56.110:3000, where 192.168.56.110 is the IP address of the VM but I need to access it through localhost:3000.
The setup is of course similar to a docker-toolbox installation (as in this question), for which I've found that the host localhost also does not work, to put it simple.
I've tried to map localhost in Windows 10, as suggested in a few answers like this one adding to C:\Windows\System32\Drivers\etc\hosts:
192.168.56.110 localhost
192.168.56.110 dev.com
But while dev.com works right away as expected, localhost doesn't.
Is there a workaround?
When I run an Angular development server in the guest, I can access it via localhost:4200 from the host but it is not completely clear to me what happens behind the scenes. Side question, how does ng serve lead to the "redirection" of localhost in the host OS?
Run as administor a windows command prompt and enter:
netsh interface portproxy add v4tov4 listenaddress=127.0.0.1 listenport=3000 connectaddress=<replace with docker ip address> connectport=3000
To confirm, run
netsh interface portproxy show all
Good luck.

PG::ConnectionBad at /sign_in could not connect to server: Connection but I can connect with psql

I have Ubuntu in a Window (which I installed from Microsoft Store). In this Ubuntu, I have a Ruby web app in this Ubuntu which connects to Postgres in my Window (not Ubuntu). This is my database.yml (/apps/appname/config/)
development:
adapter: postgresql
encoding: unicode
pool: 5
host: localhost
port: 5432
database: xxx
username: postgres
password:
All these I have setup accordingly. When I run rails server, the web app starts and listening to localhost:3000. When I open the browser at localhost:3000, I get this error below:
> PG::ConnectionBad at /sign_in could not connect to server: Connection
> refused Is the server running on host "localhost" (127.0.0.1) and
> accepting TCP/IP connections on port 5432?
I have troubleshoot by running psql and I can connect to the database at port 5432.
Server [localhost]:
Database [postgres]: xxx
Port [5432]:
Username [postgres]:
Password for user postgres:
psql (10.15)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
How do I troubleshoot this further? I realize that I didn't run Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux in Windows PowerShell in Window before I install Ubuntu. Could the issue due to this step which I miss out?
First, I'm assuming this is a WSL2 instance, not WSL1.
WSL2 instances really do run in a VM. The "VirtualMachinePlatform" component is a subset of Hyper-V. The WSL2 instances are running NAT'd (not bridged) behind their own virtual NIC vEthernet (WSL).
localhost to the WSL2 instance points to the virtual interface, not to the Windows host. On the flip side, Windows does attempt to detect ports bound inside a WSL2 instance and allow Windows applications (such as a web-browser) access to the services on those ports via localhost. However, this seems to break down for some people every so often, requiring a wsl --shutdown of the VM in order to restore the functionality.
So yes, you can access the Rails app (running in WSL) using your web browser (running in Windows) at localhost. However, you can't access the Postgres server running in Windows from the Rails server running in WSL2 via localhost.
Or at least you shouldn't. I'm surprised that the psql command seems to be working. Is it that:
psql is running under WSL? (shouldn't work via localhost)
Or perhaps you mean that you are running psql from PowerShell or cmd? That would of course work via localhost
Or perhaps the psql command is in the Windows path that gets propagated to WSL? That would also allow it to work via localhost.
But ultimately, what you should need for accessing the Windows Postgres server from within WSL is to simply use a Windows IP or address pointing to it. I've detailed some options in this answer a few days ago. But short answer:
Use mDNS, the ".local" domain. If your hostname is stevesdesktop, then try replacing localhost in your database.yml with stevesdesktop.local.
If that doesn't work, use the Windows IP directly.
Or edit /etc/hosts with the IP and a name to assign.
When you say Ubuntu in a Window I suppose you have a Ubuntu running on a VM on your Windows OS.
From the Ubuntu system point of view, localhost is it's own IP, not the one where PostgreSQL is running.
The virtualization engine usually assigns an IP to the VMs running on it, depending on the configuration of the engine, the network configuration of the VM in it and the network configuration of the system inside the VM (usually, plain DHCP). I'd consider looking at all of them.
If you are creating an internal network (which is the default on some virtualization engines and usually is in the range 192.168.122.0/24), the IP of your host (the system running in the hardware, the one running the virtualization engine, the one running the PostgreSQL server, not the VM) inside this private network will probably be 192.168.122.1. That will be the IP where the VM will be able to connect to the PostgreSQL server.
Note: You'll need to ensure PostgreSQL is started after the virtualization engine starts the virtual network, so PostgreSQL will open the port on this network, too (well, I don't know very much about how Windows handles opening TCP ports, but it's a good practice).
Edit
While the above commands and tips are still useful, Ubuntu in a Window runs WSL, which is not a VM engine. As stated in the WSL FAQs:
WSL shares the IP address of Windows, as it is running on Windows. As such you can access any ports on localhost e.g. if you had web content on port 1234 you could https://localhost:1234 into your Windows browser.
the network stack is shared, so localhost and 127.0.0.1 are good hosts to connect applications across Windows and Linux apps. Also, as stated in the FAQs, your firewall might be blocking network connections. If disabling the firewall solves the problem, you'll need to explicitly allow the desired connection.

Vagrant access Dart webdev server

I am learning Dart. I already installed Dart, and also the CLI tools (webdev and stagehand).
I successfully made a Dart console app and ran it without any issues. Then I started a bare-bones web app project and I ran the app with the following command:
webdev serve
And it started to run the web app in localhost on port 8080:
Serving web on http://127.0.0.1:8080
I already changed the forwarded port to 8080 in the vagrant file like this:
config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
But I still cannot access/view the Dart web app, so my best guest is that I am doing something wrong with the port forwarding. How can I access my Dart web app running in vagrant from the browser?
I am using an ubuntu box.

Access Webrick Rails Server from local network

Hello i am trying to access my Webrick rails server that i started on my ubuntu to my windows 7 pc locally. I checked on ifconfig in my virtual box and used the inet:10.0.2.15 when i run my rails server and also used port 80 like this:
rails server -b 10.0.2.15 -p 80
It didn't gave me an error in the terminal but when i try to go to 10.0.2.15 on my windows browser, it says it can't connect. I tried disabling the firewall in windows but still wont work. Thank you.
Choose another network type for your VM ubuntu. I suggest the Bridged Network. Reboot vm, it should obtain ip in same network as your windows host (example 192.168.0.XXX). Then you will be able reach your rails server from network At any port 80 or 3000.
Can you share your ubuntu with the vm, via the apache server too? is that started? In macs you have to enable web sharing (or something similar to that wording). ON ubuntu what happens when you do localhost:80... if i recall it should say it works! or something like that.

Access webrick/rails from another computer on local network

I have a rails application running on localhost:3000. I wish to access it from another computer on the same network. I feel like i've done this before with ease, but it's giving me some grief. I can ping the IP of the computer just fine, but hitting ip:3000 in the browser doesnt work. I tried launching rails s -b ipaddress as well, and no luck.
Suggestions?
After making sure your server side firewall is open to the incoming connection on high ports (this is normally true and the default port is 3000, so you probably don't have to do anything) you must also start the server like this:
rails server -b 0.0.0.0
which binds it to the universal address. It binds to localhost by default.
Using this method you don't have to bind to port 80, but you can like this:
rails server -b 0.0.0.0 -p 80
(If you're using rvm then you may need to use rvmsudo)
To make this change more permanent edit your config/boot.rb and add this:
require 'rails/commands/server'
module Rails
class Server
def default_options
super.merge(Host: '0.0.0.0', Port: 3000)
end
end
end
Then you should only have to use rails s
Source: https://stackoverflow.com/a/29562898/1795429
rails server -b 0.0.0.0 -p 8000
This worked for me. No firewall issues, and no need to give super user permissions.
Yes, this was a good answer in general:
rails server -b 0.0.0.0
If you use Ubuntu, you probably have to open the port in the firewall:
sudo ufw allow 3000
If your system is running in VirtualBox, you have to check your Network Settings.
In the case of network mode NAT you have to click to the extended options and there to Port Forwarding. Add a rule for TCP protocoll, host port 3000 (or any other), and guest port 3000.
Assuming Webrick starts without issue, this is 100% a firewall issue. You should provide some specifications, like what operating system your host is running and whether or not you have administrator privileges as far as controlling the firewall.
If you're on Linux and running the iptables firewall service, you need to add a rule to accept traffic over port 3000. It would look something like:
iptables -A INPUT -p tcp --dport 3000 -j ACCEPT
That command would be a one-time-only solution though, you'd need to extend your current iptables rules script to make it permanent every time your system boots or logs in.
If you're running Windows, depending on whether you're running XP or Vista/7, you'd need to do something similar. I'm going to assume you're in the Vista/7 environment, and you would just need to follow the steps provided through this guide http://windows.microsoft.com/en-US/windows7/Open-a-port-in-Windows-Firewall.
Try running the server on port 80 instead, your firewall is probably blocking port 3000.
I am using foreman to manage my Procfile-based application.
Adding -b 0.0.0.0 to my bundle exec rails s command in Procfile worked for me.
One reason is your ip is not bind to the rails server. You can bind the ip with -b command option.
Usage: rails server [mongrel, thin etc] [options]
-p, --port=port Runs Rails on the specified port.
Default: 3000
-b, --binding=IP Binds Rails to the specified IP.
Default: localhost

Resources