Search code examples
javadockergoogle-cloud-platformgoogle-bigqueryhttp-proxy

Proxy settings for Big Query Java client


I am writing a Java application that interacts with the Big Query APIs and which will also run in a docker container. I need help in setting up http and https for my application. I am not sure whether specifying only environment variables for docker container is sufficient or only setting the proxy in java code is required or both and how can I do the same.

Thanks in Advance


Solution

  • There are multiple options to achieve this. The cleanest way is to tell the JVM to use system proxies and define the proxy as environment variables for your Docker container. All options are described below.

    Option 1: Setting the proxy directly in Java

    You can define the proxy directly in your code using System.setProperty(String, String):

    System.setProperty("http.proxyHost", "proxy.example.com");
    System.setProperty("http.proxyPort", "8080");
    

    Note that the proxy is hardcoded. This solution only works if the proxy stays the same for all environments (local development, deployment on server / cloud).

    Option 2: Sepcifying the proxy when invoking the JVM

    You can set the proxy as command line parameters when invoking the VM. You don't need additional configurations in your code.

    java -Dhttp.proxyHost=proxy.example.com -Dhttp.proxyPort=8080 YourApplication
    

    You can also use environment variables here, if you have them set. That way the proxy settings could dynamically change depending on the environment.

    Option 3: Using system proxies

    The third option is to tell the JVM to use the configured system proxies (which you can do as described below). This is again achieved by setting a command line parameter.

    java -Djava.net.useSystemProxies=true YourApplication
    

    Setting the system proxy

    To set the system proxies for Docker, you again have two options.

    Option a: Use environment variables

    You can use environment variables directly in your Dockerfile:

    ENV HTTP_PROXY "http://proxy.example.com:8080"
    

    Or you can specify the environment variables in your docker run command:

    docker run --env HTTP_PROXY="http://proxy.example.com:8080" your-container
    

    Option b: Configuring the Docker client

    On the Docker client, create or edit the file ~/.docker/config.json and set the proxy:

    {
     "proxies":
     {
       "default":
       {
         "httpProxy": "http://proxy.example.com:8080"
       }
     }
    }
    

    This option only configures your local client, you would need to configure other environments accordingly.