Class RuntimeEnvironment


  • public class RuntimeEnvironment
    extends java.lang.Object
    Helps query the runtime environment.
    Since:
    3.15.0
    • Constructor Summary

      Constructors 
      Constructor Description
      RuntimeEnvironment()
      Deprecated.
      Will be removed in 4.0.0.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static boolean fileExists​(java.lang.String path)  
      static java.lang.Boolean inContainer()
      Tests whether we are running in a container like Docker or Podman.
      (package private) static boolean inContainer​(java.lang.String dirPrefix)
      Tests whether we are running in a container like Docker or Podman.
      private static java.lang.String readFile​(java.lang.String envVarFile, java.lang.String key)
      Tests whether the /proc/N/environ file at the given path string contains a specific line prefix.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • RuntimeEnvironment

        @Deprecated
        public RuntimeEnvironment()
        Deprecated.
        Will be removed in 4.0.0.
        Constructs a new instance.
    • Method Detail

      • fileExists

        private static boolean fileExists​(java.lang.String path)
      • inContainer

        public static java.lang.Boolean inContainer()
        Tests whether we are running in a container like Docker or Podman.

        The following may change if we find better detection logic.

        We roughly follow the logic in SystemD:

        https://github.com/systemd/systemd/blob/0747e3b60eb4496ee122066c844210ce818d76d9/src/basic/virt.c#L692

        We check the `container` environment variable of process 1:

        1. If the variable is empty, we return false. This includes the case, where the container developer wants to hide the fact that the application runs in a container.
        2. If the variable is not empty, we return true.
        3. If the variable is absent, we continue.
        4. We check files in the container. According to SystemD:/
          1. /.dockerenv is used by Docker.
          2. /run/.containerenv is used by PodMan.
        Returns:
        whether we are running in a container like Docker or Podman. Never null.
        See Also:
        SystemD virt.c
      • inContainer

        static boolean inContainer​(java.lang.String dirPrefix)
        Tests whether we are running in a container like Docker or Podman.

        The following may change if we find better detection logic.

        We roughly follow the logic in SystemD:

        https://github.com/systemd/systemd/blob/0747e3b60eb4496ee122066c844210ce818d76d9/src/basic/virt.c#L692

        We check the `container` environment variable of process 1:

        1. If the variable is empty, we return false. This includes the case, where the container developer wants to hide the fact that the application runs in a container.
        2. If the variable is not empty, we return true.
        3. If the variable is absent, we continue.
        4. We check files in the container. According to SystemD:/
          1. /.dockerenv is used by Docker.
          2. /run/.containerenv is used by PodMan.
        Returns:
        Whether we are running in a container like Docker or Podman.
        See Also:
        SystemD virt.c
      • readFile

        private static java.lang.String readFile​(java.lang.String envVarFile,
                                                 java.lang.String key)
        Tests whether the /proc/N/environ file at the given path string contains a specific line prefix.
        Parameters:
        envVarFile - The path to a /proc/N/environ file.
        key - The env var key to find.
        Returns:
        value The env var value or null.