在现有的vagrant box上添加端口转发

时间:2014-06-27 21:27:19

标签: vagrant

我正在运行的流浪盒(ubuntu,在OS X Mavericks主机上)运行正常。

我正在尝试从主机设置pgAdmin,并且无法为sql流量打开新端口。

我在我的Vagrantfile(最后一个)中添加了一行:

config.vm.network "forwarded_port", guest: 3000, host: 8080   # http
config.vm.network :forwarded_port, guest: 35729, host: 35729
config.vm.network "forwarded_port", guest: 5432, host: 7001   # postgres

我跑了流浪汉提供并多次弹回Vagrant盒子。重新启动时,未列出新端口转发:

==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 3000 => 8080 (adapter 1)
    default: 35729 => 35729 (adapter 1)
    default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!

Curl也给出了否定回答:

➜  ~  curl -v 'http://localhost:7001/'
* Adding handle: conn: 0x7fde1a004400
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fde1a004400) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 7001 (#0)
*   Trying ::1...
*   Trying 127.0.0.1...
*   Trying fe80::1...
* Failed connect to localhost:7001; Connection refused
* Closing connection 0
curl: (7) Failed connect to localhost:7001; Connection refused

相关帖子:

Vagrant Port Forwarding not working

Cannot connect to Postgres running on VM from host machine using MD5 method

2 个答案:

答案 0 :(得分:22)

由于5432< - > 7001端口映射未在Vagrant up序列中列出,因此它没有发生。

我会尝试vagrant reload,它应该重新加载Vagrantfile的那部分。

如果这不起作用,您还可以尝试手动添加端口映射,至少确认与您的应用程序的连接。 Change Vagrant port forwarding on a running system的已接受答案解释了如何在VirtualBox UI中执行此操作。

答案 1 :(得分:6)

除了打开VirtualBox设置之外,另一种设置临时终端的方法是使用vagrant ssh,并在--之后加上其他参数:

vagrant ssh -- -L 3000:localhost:3000

这会将主机上的端口3000转发到来宾上的端口3000。

第一个数字用于主机。要将主机上的端口7001转发到来宾上的默认postgresql端口:

vagrant ssh -- -L 7001:localhost:5432

这只会持续到ssh会话。如果ssh断开连接,请再次运行它。要使其重新启动后仍然存在,请将其添加到您的Vagrantfile中。