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

问题描述 投票:19回答:2

我正在运行的流浪盒(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

vagrant
2个回答
22
投票

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

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

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


6
投票

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

vagrant ssh -- -L 3000:localhost:3000

这会将主机上的端口3000转发到guest虚拟机上的端口3000。

第一个号码是主机。要将主机上的端口7001转发到guest虚拟机上的默认postgresql端口:

vagrant ssh -- -L 7001:localhost:5432

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

© www.soinside.com 2019 - 2024. All rights reserved.