Class DockerAccessWithHcClient

  • All Implemented Interfaces:
    DockerAccess

    public class DockerAccessWithHcClient
    extends Object
    implements DockerAccess
    Implementation using Apache HttpComponents for remotely accessing the docker host.

    The design goal here is to provide only the functionality required for this plugin in order to make it as robust as possible against docker API changes (which happen quite frequently). That's also the reason, why no framework like JAX-RS or docker-java is used so that the dependencies are kept low.

    Of course, it's a bit more manual work, but it's worth the effort (as long as the Docker API functionality required is not too much).

    Since:
    26.03.14
    Author:
    roland
    • Constructor Detail

      • DockerAccessWithHcClient

        public DockerAccessWithHcClient​(@Nonnull
                                        String baseUrl,
                                        String certPath,
                                        int maxConnections,
                                        Logger log)
                                 throws IOException
        Create a new access for the given URL
        Parameters:
        baseUrl - base URL for accessing the docker Daemon
        certPath - used to build up a keystore with the given keys and certificates found in this directory
        maxConnections - maximum parallel connections allowed to docker daemon (if a pool is used)
        log - a log handler for printing out logging information
        Throws:
        IOException
    • Method Detail

      • getServerApiVersion

        public String getServerApiVersion()
        Get the API version of the running server
        Specified by:
        getServerApiVersion in interface DockerAccess
        Returns:
        api version in the form "1.24"
      • createContainer

        public String createContainer​(ContainerCreateConfig containerConfig,
                                      String containerName)
                               throws DockerAccessException
        Description copied from interface: DockerAccess
        Create a container from the given image.

        The container id will be set on the container upon successful creation.

        Specified by:
        createContainer in interface DockerAccess
        Parameters:
        containerConfig - container configuration
        containerName - name container should be created with or null for a docker provided name
        Throws:
        DockerAccessException - if the container could not be created.
      • buildImage

        public void buildImage​(String image,
                               File dockerArchive,
                               BuildOptions options)
                        throws DockerAccessException
        Description copied from interface: DockerAccess
        Create an docker image from a given archive
        Specified by:
        buildImage in interface DockerAccess
        Parameters:
        image - name of the image to build or null if none should be used
        dockerArchive - from which the docker image should be build
        options - additional query arguments to add when building the image. Can be null.
        Throws:
        DockerAccessException - if docker host reports an error during building of an image
      • copyArchiveFromContainer

        public void copyArchiveFromContainer​(String containerId,
                                             String containerPath,
                                             File archive)
                                      throws DockerAccessException
        Description copied from interface: DockerAccess
        Copy file or directory from container as a tar archive.
        Specified by:
        copyArchiveFromContainer in interface DockerAccess
        Parameters:
        containerId - container to copy from
        containerPath - source path of a container to copy
        archive - local tar archive to copy into
        Throws:
        DockerAccessException - if the archive could not be copied
      • getLogSync

        public void getLogSync​(String containerId,
                               LogCallback callback)
        Description copied from interface: DockerAccess
        Get logs for a container up to now synchronously.
        Specified by:
        getLogSync in interface DockerAccess
        Parameters:
        containerId - container id
        callback - which is called for each line received
      • getLogAsync

        public LogGetHandle getLogAsync​(String containerId,
                                        LogCallback callback)
        Description copied from interface: DockerAccess
        Get logs asynchronously. This call will start a thread in the background for doing the request. It returns a handle which can be used to abort the request on demand.
        Specified by:
        getLogAsync in interface DockerAccess
        Parameters:
        containerId - id of the container for which to fetch the logs
        callback - to call when log data arrives
        Returns:
        handle for managing the lifecycle of the thread
      • getContainersForImage

        public List<Container> getContainersForImage​(String image,
                                                     boolean all)
                                              throws DockerAccessException
        Description copied from interface: DockerAccess
        Get all containers which are build from an image. By default only the last containers are considered but this can be tuned with a global parameters.
        Specified by:
        getContainersForImage in interface DockerAccess
        Parameters:
        image - for which its container are looked up
        all - whether to fetch also stopped containers. If false only running containers are returned
        Returns:
        list of Container objects or an empty list if none is found
        Throws:
        DockerAccessException - if the request fails
      • listContainers

        public List<Container> listContainers​(boolean all)
                                       throws DockerAccessException
        Description copied from interface: DockerAccess
        List all containers from the Docker server.
        Specified by:
        listContainers in interface DockerAccess
        Parameters:
        all - whether to fetch also stopped containers. If false only running containers are returned
        Returns:
        list of Container objects or an empty list if none is found
        Throws:
        DockerAccessException - if the request fails
      • getExecContainer

        public ExecDetails getExecContainer​(String containerIdOrName)
                                     throws DockerAccessException
        Description copied from interface: DockerAccess
        Get an exec container which is the result of executing a command in a running container.
        Specified by:
        getExecContainer in interface DockerAccess
        Parameters:
        containerIdOrName - exec container id or name
        Returns:
        ExecDetails representing the container or null if none could be found
        Throws:
        DockerAccessException - if the container could not be inspected
      • removeContainer

        public void removeContainer​(String containerId,
                                    boolean removeVolumes)
                             throws DockerAccessException
        Description copied from interface: DockerAccess
        Remove a container with the given id
        Specified by:
        removeContainer in interface DockerAccess
        Parameters:
        containerId - container id for the container to remove
        removeVolumes - if true, will remove any volumes associated to container
        Throws:
        DockerAccessException - if the container couldn't be removed.
      • pullImage

        public void pullImage​(String image,
                              AuthConfig authConfig,
                              String registry,
                              CreateImageOptions options)
                       throws DockerAccessException
        Description copied from interface: DockerAccess
        Pull an image from a remote registry and store it locally.
        Specified by:
        pullImage in interface DockerAccess
        Parameters:
        image - the image to pull.
        authConfig - authentication configuration used when pulling an image
        registry - an optional registry from where to pull the image. Can be null.
        options - additional query arguments to add when creating the image. Can be null.
        Throws:
        DockerAccessException - if the image couldn't be pulled.
      • pushImage

        public void pushImage​(String image,
                              AuthConfig authConfig,
                              String registry,
                              int retries)
                       throws DockerAccessException
        Description copied from interface: DockerAccess
        Push an image to a registry. A registry can be specified which is used as target if the image name the image does not contain a registry. If an optional registry is used, the image is also tagged with the full name containing the registry as part (if not already existent)
        Specified by:
        pushImage in interface DockerAccess
        Parameters:
        image - image name to push
        authConfig - authentication configuration
        registry - optional registry to which the image should be pushed.
        retries - optional number of times the push should be retried on a 500 error
        Throws:
        DockerAccessException - in case pushing fails
      • tag

        public void tag​(String sourceImage,
                        String targetImage,
                        boolean force)
                 throws DockerAccessException
        Description copied from interface: DockerAccess
        Alias an image in the repository with a complete new name. (Note that this maps to a Docker Remote API 'tag' operation, which IMO is badly named since it also can generate a complete alias to a given image)
        Specified by:
        tag in interface DockerAccess
        Parameters:
        sourceImage - full name (including tag) of the image to alias
        targetImage - the alias name
        force - forced tagging
        Throws:
        DockerAccessException - if the original image doesn't exist or another error occurs somehow.
      • removeImage

        public boolean removeImage​(String image,
                                   boolean force)
                            throws DockerAccessException
        Description copied from interface: DockerAccess
        Remove an image from this docker installation
        Specified by:
        removeImage in interface DockerAccess
        Parameters:
        image - image to remove
        force - if set to true remove containers as well
        Returns:
        true if an image was removed, false if none was removed
        Throws:
        DockerAccessException - if an image cannot be removed
      • start

        public void start()
        Description copied from interface: DockerAccess
        Lifecycle method for this access class which must be called before any other method is called.
        Specified by:
        start in interface DockerAccess
      • shutdown

        public void shutdown()
        Description copied from interface: DockerAccess
        Lifecycle method which must be called when this object is not needed anymore. This hook might be used for cleaning up things.
        Specified by:
        shutdown in interface DockerAccess