How to disable “Cannot Render Console from…” on Rails

后端 未结 11 1295
心在旅途
心在旅途 2020-12-07 10:35

I\'m using Ubuntu/vagrant as my development environment. I\'m getting these messages on rails console:

Started GET \"/assets/home-fcec5b5a277ac7c20cc9f45a209         


        
11条回答
  •  死守一世寂寞
    2020-12-07 10:58

    Hardcoding an IP into a configuration file isn't good. What about other devs? What if the ip changes?

    Docker-related config should not leak into the rails app whenever possible. That's why you should use env vars in the config/environments/development.rb file:

    class Application < Rails::Application
      # Check if we use Docker to allow docker ip through web-console
      if ENV['DOCKERIZED'] == 'true'
        config.web_console.whitelisted_ips = ENV['DOCKER_HOST_IP']
      end
    end
    

    You should set correct env vars in a .env file, not tracked into version control.

    In docker-compose.yml you can inject env vars from this file with env_file:

    app:
      build: .
      ports:
       - "3000:3000"
      volumes:
        - .:/app
      links:
        - db
      environment:
        - DOCKERIZED=true
      env_file:
        - ".env"
    

    Based on the feebdack received in comments, we can also build a solution without environment variables:

    class Application < Rails::Application
      # Check if we use Docker to allow docker ip through web-console
      if File.file?('/.dockerenv') == true
        host_ip = `/sbin/ip route|awk '/default/ { print $3 }'`.strip
        config.web_console.whitelisted_ips << host_ip
      end
    end
    

    I'll leave the solutions with env var for learning purposes.

提交回复
热议问题