Make the Application Configurable at Runtime
Your application must be able to read settings from environment variables in the application’s container. All applications except Java EE web applications and worker applications must read the HOSTNAME
and PORT
environment variables, and use the values dynamically.
-
Your application is running inside a Docker container that has a generated host name and port. These are made available to the application in the
HOSTNAME
andPORT
environment variables.If the application is required to listen on the specified port but doesn’t, then the application creation and deployment will fail. After deployment, the service pings the application on that port to determine if it’s running. The load balancer and application ports are different. The load balancer accepts SSL traffic on port 1443, then directs requests to each application according to the port in the
PORT
environment variable. -
If your application uses other Oracle Cloud services, then service connection details (such as ports) can also be made available in environment variables.
-
You can also add your own environment variables using the user interface or the
deployment.json
file. See Create Metadata Files.
Note:
ThePORT
and ORA_PORT
environment variables have the same value. Your application can read the port using either one.
If you’re programming in Java 8, then you can use the Optional
class to retrieve the environment variables without having to use if
blocks to check for null values, as shown in this code snippet from the Grizzly Jersey sample application:
/**
* Main class
*/
public class Main{
// Base URI the Grizzly HTTP server will listen on
public static final String BASE_URI;
public static final String protocol;
public static final Optional<String> host;
public static final String path;
public static final Optional<String> port;
static{
protocol = "http://";
host = Optional.ofNullable(System.getenv("HOSTNAME"));
port = Optional.ofNullable(System.getenv("PORT"));
path = "myapp";
BASE_URI = protocol + host.orElse("localhost") + ":" + port.orElse("8080") + "/" + path + "/";
}
}