宿主机无法访问虚拟机中搭建的web内容

发布于 2022-09-04 07:10:53 字数 6109 浏览 18 评论 0

看一本书的时候,依照书里的提示安装了vagrant和若干个虚拟机,实际上只是clone了Github的代码然后运行vagrant up,本来应该可以从宿主机的浏览器访问虚拟机的web内容,当实际上显示无法访问

vbox使用的是网络设置是nat,vagrantfile如下

ENV['VAGRANT_DEFAULT_PROVIDER'] = "docker"

host_vagrantfile = "./Vagrantfile.dockerhost"
force_host_vm = TRUE

Vagrant.configure("2") do |config|

    # -------------- Web server --------------

    config.vm.define "web" do |web|
    
        web.vm.provider "docker" do |d|
            d.image = "scrapybook/web"
            #d.build_dir = "../scrapybook-docker-web"
            d.name = "web"

            d.vagrant_machine = "docker-provider"
            d.vagrant_vagrantfile = host_vagrantfile
            d.force_host_vm = force_host_vm
        end
        
        web.vm.synced_folder ".", "/vagrant", disabled: true
        
        web.vm.network "forwarded_port", guest: 9312, host: 9312, auto_correct: true
        #web.vm.network :private_network, ip: "192.168.33.10"
        web.vm.hostname = "web"
    end

    # -------------- Spark server --------------

    config.vm.define "spark" do |spark|
    
        spark.vm.provider "docker" do |d|
            d.image = "scrapybook/spark"
            #d.build_dir = "../scrapybook-docker-spark"
            d.name = "spark"

            d.vagrant_machine = "docker-provider"
            d.vagrant_vagrantfile = host_vagrantfile
            d.force_host_vm = force_host_vm
        end
        
        spark.vm.synced_folder ".", "/root/book"
        
        spark.vm.network "forwarded_port", guest: 21, host: 21
        (30000..30009).each do |port|
            spark.vm.network "forwarded_port", guest: port, host: port, auto_correct: true
        #spark.vm.network :private_network, ip: "192.168.33.11"
        end
        spark.vm.hostname = "spark"
    end


    # -------------- ES server --------------

    config.vm.define "es" do |es|
    
        es.vm.provider "docker" do |d|
            d.image = "scrapybook/es"
            #d.build_dir = "../scrapybook-docker-es"
            d.name = "es"

            d.vagrant_machine = "docker-provider"
            d.vagrant_vagrantfile = host_vagrantfile
            d.force_host_vm = force_host_vm
        end
        
        es.vm.synced_folder ".", "/vagrant", disabled: true
        
        es.vm.network "forwarded_port", guest: 9200, host: 9200, auto_correct: true
        #es.vm.network :private_network, ip: "192.168.33.12"
        es.vm.hostname = "es"
    end
    

    # -------------- Redis server --------------

    config.vm.define "redis" do |redis|
    
        redis.vm.provider "docker" do |d|
            d.image = "scrapybook/redis"
            #d.build_dir = "../scrapybook-docker-redis"
            d.name = "redis"

            d.vagrant_machine = "docker-provider"
            d.vagrant_vagrantfile = host_vagrantfile
            d.force_host_vm = force_host_vm
        end
        
        redis.vm.synced_folder ".", "/vagrant", disabled: true
        
        redis.vm.network "forwarded_port", guest: 6379, host: 6379, auto_correct: true
        #redis.vm.network :private_network, ip: "192.168.33.13"
        redis.vm.hostname = "redis"
    end
    

    # -------------- MySQL server --------------

    config.vm.define "mysql" do |mysql|
    
        mysql.vm.provider "docker" do |d|
            d.image = "scrapybook/mysql"
            #d.build_dir = "../scrapybook-docker-mysql"
            d.name = "mysql"

            d.vagrant_machine = "docker-provider"
            d.vagrant_vagrantfile = host_vagrantfile
            d.force_host_vm = force_host_vm
        end
        
        mysql.vm.synced_folder ".", "/vagrant", disabled: true
        
        mysql.vm.network "forwarded_port", guest: 3306, host: 3306, auto_correct: true
        #mysql.vm.network :private_network, ip: "192.168.33.14"
        mysql.vm.hostname = "mysql"
    end
    # -------------- 3 Scrapyd servers --------------

    {
        "scrapyd1" => 6801,
        "scrapyd2" => 6802, 
        "scrapyd3" => 6803,
    }.each do |host, port|
    
        config.vm.define host do |scp|

            scp.vm.provider "docker" do |d|
                d.image = "scrapybook/dev"
                #d.build_dir = "../scrapybook-docker-dev"
                d.name = host
                
                d.link("spark:spark")
                d.link("web:web")

                d.vagrant_machine = "docker-provider"
                d.vagrant_vagrantfile = host_vagrantfile
                d.force_host_vm = force_host_vm
            end
        
            scp.vm.synced_folder ".", "/vagrant", disabled: true
        
            scp.vm.network "forwarded_port", guest: 6800, host: port, auto_correct: true
            #scp.vm.network :private_network, ip: "192.168.33.15"
            scp.vm.hostname = host
        end
    end

    # -------------- Dev machine --------------

    config.vm.define "dev", primary: true do |dev|
    
        dev.vm.provider "docker" do |d|
            d.image = "scrapybook/dev"
            #d.build_dir = "../scrapybook-docker-dev"
            d.name = "dev"

            d.link("web:web")
            d.link("spark:spark")
            d.link("scrapyd1:scrapyd1")
            d.link("scrapyd2:scrapyd2")
            d.link("scrapyd3:scrapyd3")
            d.link("mysql:mysql")
            d.link("redis:redis")
            d.link("es:es")

            d.vagrant_machine = "docker-provider"
            d.vagrant_vagrantfile = host_vagrantfile
            d.force_host_vm = force_host_vm
        end
        
        dev.vm.synced_folder ".", "/root/book"
        
        dev.vm.network "forwarded_port", guest: 6800, host: 6800, auto_correct: true
        #dev.vm.network :private_network, ip: "192.168.33.16"
        dev.vm.hostname = "dev"
    end

    config.ssh.username = 'root'
    config.ssh.private_key_path = 'insecure_key'
end

端口转接之后我应该可以在宿主浏览器里访问http:localhost:9312,但是没有响应,我只有在通过vagrant ssh web进入web里然后curl http:localhost:9312才不会报错
看了vagrant的官方文档但是好像没有找到这方面的内容
非常感谢

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的海 2022-09-11 07:10:53

问题已经解决,因为安装的时候曾发生端口冲突的情况,在编辑Vagrantfile.dockerhost文件解决端口冲突时有个语句加到了不应该的循环里,造成了乌龙错误……而且vagrant还正常运行了……

千紇 2022-09-11 07:10:53

请问你也是又看过《精通Python爬虫框架Scrapy》这本书吗?我使用vagrant up后一直不能成功,请问你是怎样布置的?
邮箱157870845@qq.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文