Class Docker

  • All Implemented Interfaces:
    com.oracle.bedrock.Option

    public class Docker
    extends Object
    implements com.oracle.bedrock.Option
    An encapsulation of the various settings required to run Docker commands.

    A Docker instance is immutable, methods that add options and configuration to this Docker environment return a new instance of a Docker environment with the modifications applied

    Copyright (c) 2016. All Rights Reserved. Oracle Corporation.
    Oracle is a registered trademark of Oracle Corporation and/or its affiliates.

    Author:
    Jonathan Knight
    • Field Detail

      • ENV_DOCKER_API_VERSION

        public static final String ENV_DOCKER_API_VERSION
        The API version to use (e.g. 1.19)
        See Also:
        Constant Field Values
      • ENV_DOCKER_CONFIG

        public static final String ENV_DOCKER_CONFIG
        The location of your client configuration files.
        See Also:
        Constant Field Values
      • ENV_DOCKER_CERT_PATH

        public static final String ENV_DOCKER_CERT_PATH
        The location of your authentication keys.
        See Also:
        Constant Field Values
      • ENV_DOCKER_NOWARN_KERNEL_VERSION

        public static final String ENV_DOCKER_NOWARN_KERNEL_VERSION
        Prevent warnings that your Linux kernel is unsuitable for Docker.
        See Also:
        Constant Field Values
      • ENV_DOCKER_RAMDISK

        public static final String ENV_DOCKER_RAMDISK
        If set this will disable ‘pivot_root’.
        See Also:
        Constant Field Values
      • ENV_DOCKER_TLS_VERIFY

        public static final String ENV_DOCKER_TLS_VERIFY
        When set Docker uses TLS and verifies the remote.
        See Also:
        Constant Field Values
      • ENV_DOCKER_CONTENT_TRUST

        public static final String ENV_DOCKER_CONTENT_TRUST
        When set Docker uses notary to sign and verify images. Equates to --disable-content-trust=false for build, create, pull, push, run.
        See Also:
        Constant Field Values
      • ENV_DOCKER_CONTENT_TRUST_SERVER

        public static final String ENV_DOCKER_CONTENT_TRUST_SERVER
        The URL of the Notary server to use. This defaults to the same URL as the registry.
        See Also:
        Constant Field Values
      • ARG_CONFIG

        public static final String ARG_CONFIG
        Location of client config files E.G. --config=~/.docker
        See Also:
        Constant Field Values
      • ARG_TLS_CA_CERT

        public static final String ARG_TLS_CA_CERT
        Trust certs signed only by this CA E.G. --tlscacert=~/.docker/ca.pem
        See Also:
        Constant Field Values
      • ARG_TLS_CERT

        public static final String ARG_TLS_CERT
        Path to TLS certificate file E.G. --tlscert=~/.docker/cert.pem
        See Also:
        Constant Field Values
      • ARG_TLS_KEY

        public static final String ARG_TLS_KEY
        Path to TLS key file E.G. --tlskey=~/.docker/key.pem
        See Also:
        Constant Field Values
      • ARG_TLS_VERIFY

        public static final String ARG_TLS_VERIFY
        Use TLS and verify the remote E.G. --tlsverify=false
        See Also:
        Constant Field Values
      • ARG_VERSION

        public static final String ARG_VERSION
        Print version information and quit E.G. --version=false
        See Also:
        Constant Field Values
      • DEFAULT_EXECUTABLE

        public static final String DEFAULT_EXECUTABLE
        The default name of the executable to use to execute Docker commands.
        See Also:
        Constant Field Values
    • Method Detail

      • withDaemonAddress

        public Docker withDaemonAddress​(String address)
        Obtain a Docker environment that is the same as this Docker environment with the specified Docker daemon address.

        Equates to the Docker --host command option.

        Parameters:
        address - the address of the Docker daemon
        Returns:
        a Docker environment that is the same as this Docker environment with the specified Docker daemon address
      • getDaemonAddress

        public String getDaemonAddress()
        Obtain the Docker address that this Docker environment will use to communicate with the Docker daemon.

        Returns:
        the Docker address that this Docker environment will use to communicate with the Docker daemon
      • dockerExecutableOf

        public Docker dockerExecutableOf​(String executable)
        Set the executable name to use to run Docker client commands.

        This would typically be "docker" but some third-party add-ons provide other clients.

        Parameters:
        executable - the name of the executable or null to use the default of "docker"
        Returns:
        a new instance of Docker that is a copy of this instance with the addition of the executable name change
      • getDockerExecutable

        public String getDockerExecutable()
        Obtain the executable name to use to run Docker client commands.
        Returns:
        the executable name to use to run Docker client commands
      • getArguments

        public List<com.oracle.bedrock.runtime.options.Argument> getArguments()
        Obtain an immutable copy of the command line Arguments for this Docker environment.
        Returns:
        an immutable copy of the command line Arguments for this Docker environment
      • getEnvironmentVariables

        public List<com.oracle.bedrock.runtime.options.EnvironmentVariable> getEnvironmentVariables()
        Obtain an immutable copy of the EnvironmentVariables for this Docker environment.
        Returns:
        an immutable copy of the EnvironmentVariables for this Docker environment
      • withBaseImage

        public Docker withBaseImage​(Class<? extends com.oracle.bedrock.runtime.Application> applicationClass,
                                    String baseImageName)
        Add a default base image to use for a specific type of application class.
        Parameters:
        applicationClass - the Class of the application
        baseImageName - the base image name to use
        Returns:
        a new instance of Docker that is a copy of this instance with the addition of the base image default
        Throws:
        IllegalArgumentException - if the application class is null or if the base image name is null or blank
        See Also:
        getBaseImage(Class)
      • getBaseImage

        public String getBaseImage​(Class<? extends com.oracle.bedrock.runtime.Application> applicationClass)
        Obtain the default base image name to use for applications of a given Class.
        Parameters:
        applicationClass - the application Class
        Returns:
        the name of the base image to use
        See Also:
        withBaseImage(Class, String)
      • withDefaultNetwork

        public Docker withDefaultNetwork​(String networkName)
        Obtain a new Docker environment that is a copy of this environment with the addition of the specified default network name.
        Parameters:
        networkName - the name of the default network to connect automatically created containers to
        Returns:
        a new Docker environment that is a copy of this environment with the addition of the specified default network name
      • getDefaultNetworkName

        public String getDefaultNetworkName()
        Obtain the name of the default network to connect automatically run containers to.
        Returns:
        the name of the default network to connect automatically run containers to
      • configAt

        public Docker configAt​(Object config)
        Obtain a copy of this Docker environment with the addition of the specified location of client config files.

        Equates to the Docker --config command option.

        Parameters:
        config - the config
        Returns:
        a copy of this Docker environment with the addition of the specified location of client config files
      • debug

        public Docker debug​(boolean enabled)
        Obtain a copy of this Docker environment with the addition of the specified debug setting.

        Equates to the Docker --debug command option.

        Parameters:
        enabled - true to enable debug, false to disable debug
        Returns:
        a copy of this Docker environment with the addition of the specified debug setting
      • logLevel

        public Docker logLevel​(String level)
        Obtain a copy of this Docker environment with the addition of the specified log level.

        Equates to the Docker --log-level command option.

        Parameters:
        level - the logging level to use, for example "info"
        Returns:
        a copy of this Docker environment with the addition of the specified log level
      • tls

        public Docker tls​(boolean enabled)
        Obtain a copy of this Docker environment with the addition of the specified tls setting.

        Equates to the Docker --tls command option.

        Parameters:
        enabled - true to enables TLS, false to disable TLS
        Returns:
        a copy of this Docker environment with the addition of the specified tls setting
      • tlsCACert

        public Docker tlsCACert​(File location)
        Obtain a copy of this Docker environment with the addition of the specified Trust certs signed only by this CA.

        Equates to the Docker --tlscacert command option.

        Parameters:
        location - the location of the certificates
        Returns:
        a copy of this Docker environment with the addition of the specified Trust certs signed only by this CA
      • tlsCert

        public Docker tlsCert​(File location)
        Obtain a copy of this Docker environment with the addition of the specified path to TLS certificate file.

        Equates to the Docker --tlscert command option.

        Parameters:
        location - the location of the certificate file
        Returns:
        a copy of this Docker environment with the addition of the specified path to TLS certificate file
      • tlsKey

        public Docker tlsKey​(File location)
        Obtain a copy of this Docker environment with the addition of the specified path to TLS key file.

        Equates to the Docker --tlskey command option.

        Parameters:
        location - the location of the key file
        Returns:
        a copy of this Docker environment with the addition of the specified path to TLS key file
      • tlsVerify

        public Docker tlsVerify​(boolean verify)
        Obtain a copy of this Docker environment with the addition of the specified TLS verification setting.

        Equates to the Docker --tlsverify command option.

        Parameters:
        verify - true to enable TLS verification, false to disable TLS verification
        Returns:
        a copy of this Docker environment with the addition of the specified TLS verification setting
      • apiVersion

        public Docker apiVersion​(String version)
        Obtain a copy of this Docker environment with the addition of the specified DOCKER_API_VERSION environment variable.
        Parameters:
        version - The API version to use (e.g. 1.19)
        Returns:
        a copy of this Docker environment with the addition of the specified DOCKER_API_VERSION environment variable
      • driver

        public Docker driver​(String driver)
        Obtain a copy of this Docker environment with the addition of the specified DOCKER_DRIVER environment variable.
        Parameters:
        driver - The graph driver to use
        Returns:
        a copy of this Docker environment with the addition of the specified DOCKER_DRIVER environment variable
      • noWarnKernelVersion

        public Docker noWarnKernelVersion​(boolean noWarn)
        Obtain a copy of this Docker environment with the addition of the specified DOCKER_NOWARN_KERNEL_VERSION environment variable.
        Parameters:
        noWarn - true to prevent warnings that your Linux kernel is unsuitable for Docker
        Returns:
        a copy of this Docker environment with the addition of the specified DOCKER_NOWARN_KERNEL_VERSION environment variable
      • ramDisk

        public Docker ramDisk​(boolean use)
        Obtain a copy of this Docker environment with the addition of the specified DOCKER_RAMDISK environment variable.
        Parameters:
        use - true to disable ‘pivot_root’
        Returns:
        a copy of this Docker environment with the addition of the specified DOCKER_RAMDISK environment variable
      • contentTrustEnabled

        public Docker contentTrustEnabled​(boolean enabled)
        Obtain a copy of this Docker environment with the addition of the specified DOCKER_CONTENT_TRUST environment variable.

        Equates to --disable-content-trust=false for build, create, pull, push, run

        Parameters:
        enabled - true to set Docker to use notary to sign and verify images
        Returns:
        a copy of this Docker environment with the addition of the specified DOCKER_CONTENT_TRUST environment variable
      • contentTrustAt

        public Docker contentTrustAt​(String url)
        Obtain a copy of this Docker environment with the addition of the specified DOCKER_CONTENT_TRUST_SERVER environment variable.

        This defaults to the same URL as the registry.

        Parameters:
        url - the URL of the Notary server to use.
        Returns:
        a copy of this Docker environment with the addition of the specified DOCKER_CONTENT_TRUST_SERVER environment variable
      • tempFilesAt

        public Docker tempFilesAt​(File temp)
        Obtain a copy of this Docker environment with the addition of the specified DOCKER_TMPDIR environment variable.
        Parameters:
        temp - the location for temporary Docker files
        Returns:
        a copy of this Docker environment with the addition of the specified DOCKER_TMPDIR environment variable
      • withCommandOptions

        public Docker withCommandOptions​(com.oracle.bedrock.runtime.options.Argument... opts)
        Obtain a copy of this Docker environment with the addition of the specified command line options.

        Docker command line options are placed on the command line before the name of the command to be executed. For example if the command being executed is the build command the command line would be: docker command-options build command-args

        Parameters:
        opts - the command line options
        Returns:
        a copy of this Docker environment with the addition of the specified command line options
      • withEnvironmentVariables

        public Docker withEnvironmentVariables​(com.oracle.bedrock.runtime.options.EnvironmentVariable... environmentVariables)
        Obtain a copy of this Docker environment with the addition of the specified EnvironmentVariables.

        The EnvironmentVariables will be applied to any process running Docker commands using this Docker environment.

        Parameters:
        environmentVariables - the EnvironmentVariables to use
        Returns:
        a copy of this Docker environment with the addition of the specified EnvironmentVariables
      • withEnvironmentVariables

        public Docker withEnvironmentVariables​(List<com.oracle.bedrock.runtime.options.EnvironmentVariable> environmentVariables)
        Obtain a copy of this Docker environment with the addition of the specified EnvironmentVariables.

        The EnvironmentVariables will be applied to any process running Docker commands using this Docker environment.

        Parameters:
        environmentVariables - the EnvironmentVariables to use
        Returns:
        a copy of this Docker environment with the addition of the specified EnvironmentVariables
      • getValidLocalAddress

        public InetAddress getValidLocalAddress()
        Try to find a valid local InetAddress that is routable to the Docker daemon.

        If the daemon address does not have the tcp:// prefix then it is probably local, i.e. unix:// or fd:// so the address returned will be the address of the LocalPlatform.

        For a tcp:// address the host and port are parsed out and a socket opened to the address. The local address of the socket is returned.

        If any errors occur the address of the LocalPlatform is returned.

        Returns:
        an InetAddress that should be visible from the daemon
      • getDaemonInetAddress

        public InetAddress getDaemonInetAddress​(com.oracle.bedrock.runtime.Platform clientPlatform)
      • auto

        @Default
        public static Docker auto()
        Configure Docker from the current environment.

        Using auto configuration assumes that Docker is already configured on the client platform with environment variables pointing to the Docker daemon or the Docker daemon is local to the client platform.

        Returns:
        a new Docker
      • machine

        public static Docker machine​(String machineName)
        Configure Docker using the specified Docker Machine machine name.

        Docker client commands will be executed with the correct settings to connect to the Docker daemon on the specified Docker Machine instance.

        Parameters:
        machineName - the name of a Docker Machine machine
        Returns:
        a new Docker
      • machine

        public static Docker machine​(String machineName,
                                     DockerMachine machine,
                                     com.oracle.bedrock.Option... options)
        Configure Docker using the specified Docker Machine machine name.

        Docker client commands will be executed with the correct settings to connect to the Docker daemon on the specified Docker Machine instance.

        Parameters:
        machineName - the name of a Docker Machine machine
        machine - the DockerMachine environment
        options - the Options
        Returns:
        a new Docker
      • daemonAt

        public static Docker daemonAt​(String address)
        Manually configure Docker.

        The Docker client commands will be executed with parameters to connect to the Docker daemon at the specified address.

        Parameters:
        address - the address of the Docker daemon
        Returns:
        a new Docker