Class DockerAccessWithHcClient
- java.lang.Object
-
- io.fabric8.maven.docker.access.hc.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
-
-
Field Summary
Fields Modifier and Type Field Description static StringAPI_VERSION
-
Constructor Summary
Constructors Constructor Description DockerAccessWithHcClient(String baseUrl, String certPath, int maxConnections, Logger log)Create a new access for the given URL
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbuildImage(String image, File dockerArchive, BuildOptions options)Create an docker image from a given archivevoidcopyArchiveFromContainer(String containerId, String containerPath, File archive)Copy file or directory from container as a tar archive.voidcopyArchiveToContainer(String containerId, File archive, String targetPath)Copy an archive (must be a tar) into a running container.StringcreateContainer(ContainerCreateConfig containerConfig, String containerName)Create a container from the given image.StringcreateExecContainer(String containerId, Arguments arguments)Sets up an exec instance for a running container idStringcreateNetwork(NetworkCreateConfig networkConfig)Create a custom network from the given configuration.StringcreateVolume(VolumeCreateConfig containerConfig)Create a volumeStringfetchApiVersionFromServer(String baseUrl, ApacheHttpClientDelegate delegate)ContainerDetailsgetContainer(String containerIdOrName)Get a containerList<Container>getContainersForImage(String image, boolean all)Get all containers which are build from an image.ExecDetailsgetExecContainer(String containerIdOrName)Get an exec container which is the result of executing a command in a running container.StringgetImageId(String name)Get the image id of a given name ornullif no such image existsList<String>getImageTags(String name)Get the list of tags of a given image name ornullif no such image existsLogGetHandlegetLogAsync(String containerId, LogCallback callback)Get logs asynchronously.voidgetLogSync(String containerId, LogCallback callback)Get logs for a container up to now synchronously.StringgetServerApiVersion()Get the API version of the running serverbooleanhasImage(String name)Check whether the given name exists as image at the docker daemonvoidkillContainer(String containerId)Kill a containerList<Container>listContainers(boolean all)List all containers from the Docker server.List<Image>listImages(boolean all)List the containers on the serverList<Network>listNetworks()List all networksvoidloadImage(String image, File tarArchive)Load an image from an archive.voidpullImage(String image, AuthConfig authConfig, String registry, CreateImageOptions options)Pull an image from a remote registry and store it locally.voidpushImage(String image, AuthConfig authConfig, String registry, int retries)Push an image to a registry.voidremoveContainer(String containerId, boolean removeVolumes)Remove a container with the given idbooleanremoveImage(String image, boolean... forceOpt)Remove an image from this docker installationbooleanremoveNetwork(String networkId)Remove a custom networkvoidremoveVolume(String name)Removes a volume.voidsaveImage(String image, String filename, ArchiveCompression compression)Save an image to a tar filevoidshutdown()Lifecycle method which must be called when this object is not needed anymore.voidstart()Lifecycle method for this access class which must be called before any other method is called.voidstartContainer(String containerId)Start a container.voidstartExecContainer(String containerId, LogOutputSpec outputSpec)Starts a previously set up exec instance (viaDockerAccess.createExecContainer(String, Arguments)container this API sets up a session with the exec command.voidstopContainer(String containerId, int killWait)Stop a container.voidtag(String sourceImage, String targetImage, boolean force)Alias an image in the repository with a complete new name.
-
-
-
Field Detail
-
API_VERSION
public static final String API_VERSION
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DockerAccessWithHcClient
public DockerAccessWithHcClient(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 DaemoncertPath- used to build up a keystore with the given keys and certificates found in this directorymaxConnections- 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() throws DockerAccessException
Get the API version of the running server- Specified by:
getServerApiVersionin interfaceDockerAccess- Returns:
- api version in the form "1.24"
- Throws:
DockerAccessException- if the api version could not be obtained
-
startExecContainer
public void startExecContainer(String containerId, LogOutputSpec outputSpec) throws DockerAccessException
Description copied from interface:DockerAccessStarts a previously set up exec instance (viaDockerAccess.createExecContainer(String, Arguments)container this API sets up a session with the exec command. Output is streamed to the log. This methods returns only when the exec command has finished (i.e this method calls the command in a non-detached mode).- Specified by:
startExecContainerin interfaceDockerAccess- Parameters:
containerId- id of the exec containeroutputSpec- how to print out the output of the command- Throws:
DockerAccessException- if the container could not be created.
-
createExecContainer
public String createExecContainer(String containerId, Arguments arguments) throws DockerAccessException
Description copied from interface:DockerAccessSets up an exec instance for a running container id- Specified by:
createExecContainerin interfaceDockerAccess- Parameters:
containerId- id of the running container which the exec container will be created forarguments- container exec commands to run- Throws:
DockerAccessException- if the container could not be created.
-
createContainer
public String createContainer(ContainerCreateConfig containerConfig, String containerName) throws DockerAccessException
Description copied from interface:DockerAccessCreate a container from the given image.The
container idwill be set on thecontainerupon successful creation.- Specified by:
createContainerin interfaceDockerAccess- Parameters:
containerConfig- container configurationcontainerName- name container should be created with ornullfor a docker provided name- Throws:
DockerAccessException- if the container could not be created.
-
startContainer
public void startContainer(String containerId) throws DockerAccessException
Description copied from interface:DockerAccessStart a container.- Specified by:
startContainerin interfaceDockerAccess- Parameters:
containerId- id of the container to start- Throws:
DockerAccessException- if the container could not be started.
-
stopContainer
public void stopContainer(String containerId, int killWait) throws DockerAccessException
Description copied from interface:DockerAccessStop a container.- Specified by:
stopContainerin interfaceDockerAccess- Parameters:
containerId- the container idkillWait- the time to wait between stop and kill (in seconds)- Throws:
DockerAccessException- if the container could not be stopped.
-
killContainer
public void killContainer(String containerId) throws DockerAccessException
Description copied from interface:DockerAccessKill a container- Specified by:
killContainerin interfaceDockerAccess- Parameters:
containerId- the container id- Throws:
DockerAccessException- if container failed to be killed
-
buildImage
public void buildImage(String image, File dockerArchive, BuildOptions options) throws DockerAccessException
Description copied from interface:DockerAccessCreate an docker image from a given archive- Specified by:
buildImagein interfaceDockerAccess- Parameters:
image- name of the image to build ornullif none should be useddockerArchive- from which the docker image should be buildoptions- 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
-
copyArchiveToContainer
public void copyArchiveToContainer(String containerId, File archive, String targetPath) throws DockerAccessException
Description copied from interface:DockerAccessCopy an archive (must be a tar) into a running container.- Specified by:
copyArchiveToContainerin interfaceDockerAccess- Parameters:
containerId- container to copy intoarchive- local archive to copy intotargetPath- target path to use- Throws:
DockerAccessException- if the archive could not be copied
-
copyArchiveFromContainer
public void copyArchiveFromContainer(String containerId, String containerPath, File archive) throws DockerAccessException
Description copied from interface:DockerAccessCopy file or directory from container as a tar archive.- Specified by:
copyArchiveFromContainerin interfaceDockerAccess- Parameters:
containerId- container to copy fromcontainerPath- source path of a container to copyarchive- 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:DockerAccessGet logs for a container up to now synchronously.- Specified by:
getLogSyncin interfaceDockerAccess- Parameters:
containerId- container idcallback- which is called for each line received
-
getLogAsync
public LogGetHandle getLogAsync(String containerId, LogCallback callback)
Description copied from interface:DockerAccessGet 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:
getLogAsyncin interfaceDockerAccess- Parameters:
containerId- id of the container for which to fetch the logscallback- 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:DockerAccessGet 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:
getContainersForImagein interfaceDockerAccess- Parameters:
image- for which its container are looked upall- whether to fetch also stopped containers. If false only running containers are returned- Returns:
- list of
Containerobjects 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:DockerAccessList all containers from the Docker server.- Specified by:
listContainersin interfaceDockerAccess- Parameters:
all- whether to fetch also stopped containers. If false only running containers are returned- Returns:
- list of
Containerobjects or an empty list if none is found - Throws:
DockerAccessException- if the request fails
-
getContainer
public ContainerDetails getContainer(String containerIdOrName) throws DockerAccessException
Description copied from interface:DockerAccessGet a container- Specified by:
getContainerin interfaceDockerAccess- Parameters:
containerIdOrName- container id or name- Returns:
ContainerDetailsrepresenting the container or null if none could be found- Throws:
DockerAccessException- if the container could not be inspected
-
getExecContainer
public ExecDetails getExecContainer(String containerIdOrName) throws DockerAccessException
Description copied from interface:DockerAccessGet an exec container which is the result of executing a command in a running container.- Specified by:
getExecContainerin interfaceDockerAccess- Parameters:
containerIdOrName- exec container id or name- Returns:
ExecDetailsrepresenting the container or null if none could be found- Throws:
DockerAccessException- if the container could not be inspected
-
listImages
public List<Image> listImages(boolean all) throws DockerAccessException
Description copied from interface:DockerAccessList the containers on the server- Specified by:
listImagesin interfaceDockerAccess- Parameters:
all- if true, return untagged images- Returns:
- the images list (may be empty but never null)
- Throws:
DockerAccessException- if the list couldn't be retrieved
-
hasImage
public boolean hasImage(String name) throws DockerAccessException
Description copied from interface:DockerAccessCheck whether the given name exists as image at the docker daemon- Specified by:
hasImagein interfaceDockerAccess- Parameters:
name- image name to check- Returns:
- true if the image exists
- Throws:
DockerAccessException
-
getImageId
public String getImageId(String name) throws DockerAccessException
Description copied from interface:DockerAccessGet the image id of a given name ornullif no such image exists- Specified by:
getImageIdin interfaceDockerAccess- Parameters:
name- name to lookup- Returns:
- the image id or
null - Throws:
DockerAccessException
-
getImageTags
public List<String> getImageTags(String name) throws DockerAccessException
Description copied from interface:DockerAccessGet the list of tags of a given image name ornullif no such image exists- Specified by:
getImageTagsin interfaceDockerAccess- Parameters:
name- name to lookup- Returns:
- the list of image tags or
null - Throws:
DockerAccessException
-
removeContainer
public void removeContainer(String containerId, boolean removeVolumes) throws DockerAccessException
Description copied from interface:DockerAccessRemove a container with the given id- Specified by:
removeContainerin interfaceDockerAccess- Parameters:
containerId- container id for the container to removeremoveVolumes- if true, will remove any volumes associated to container- Throws:
DockerAccessException- if the container couldn't be removed.
-
loadImage
public void loadImage(String image, File tarArchive) throws DockerAccessException
Description copied from interface:DockerAccessLoad an image from an archive.- Specified by:
loadImagein interfaceDockerAccess- Parameters:
image- the image to pull.tarArchive- archive file- Throws:
DockerAccessException- if the image couldn't be loaded.
-
pullImage
public void pullImage(String image, AuthConfig authConfig, String registry, CreateImageOptions options) throws DockerAccessException
Description copied from interface:DockerAccessPull an image from a remote registry and store it locally.- Specified by:
pullImagein interfaceDockerAccess- Parameters:
image- the image to pull.authConfig- authentication configuration used when pulling an imageregistry- 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:DockerAccessPush an image to a registry. An 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:
pushImagein interfaceDockerAccess- Parameters:
image- image name to pushauthConfig- authentication configurationregistry- 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
-
saveImage
public void saveImage(String image, String filename, ArchiveCompression compression) throws DockerAccessException
Description copied from interface:DockerAccessSave an image to a tar file- Specified by:
saveImagein interfaceDockerAccess- Parameters:
image- image to savefilename- target filenamecompression- compression to use for the archive- Throws:
DockerAccessException- if an image cannot be removed
-
tag
public void tag(String sourceImage, String targetImage, boolean force) throws DockerAccessException
Description copied from interface:DockerAccessAlias 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:
tagin interfaceDockerAccess- Parameters:
sourceImage- full name (including tag) of the image to aliastargetImage- the alias nameforce- forced tagging- Throws:
DockerAccessException- if the original image doesn't exist or another error occurs somehow.
-
removeImage
public boolean removeImage(String image, boolean... forceOpt) throws DockerAccessException
Description copied from interface:DockerAccessRemove an image from this docker installation- Specified by:
removeImagein interfaceDockerAccess- Parameters:
image- image to removeforceOpt- if set to true remove containers as well (only the first vararg is evaluated)- Returns:
- true if an image was removed, false if none was removed
- Throws:
DockerAccessException- if an image cannot be removed
-
listNetworks
public List<Network> listNetworks() throws DockerAccessException
Description copied from interface:DockerAccessList all networks- Specified by:
listNetworksin interfaceDockerAccess- Returns:
- list of
Networkobjects - Throws:
DockerAccessException- if the networks could not be listed
-
createNetwork
public String createNetwork(NetworkCreateConfig networkConfig) throws DockerAccessException
Description copied from interface:DockerAccessCreate a custom network from the given configuration.- Specified by:
createNetworkin interfaceDockerAccess- Parameters:
networkConfig- network configuration- Throws:
DockerAccessException- if the container could not be created.
-
removeNetwork
public boolean removeNetwork(String networkId) throws DockerAccessException
Description copied from interface:DockerAccessRemove a custom network- Specified by:
removeNetworkin interfaceDockerAccess- Parameters:
networkId- network to remove- Returns:
- true if an network was removed, false if none was removed
- Throws:
DockerAccessException- if an image cannot be removed
-
createVolume
public String createVolume(VolumeCreateConfig containerConfig) throws DockerAccessException
Description copied from interface:DockerAccessCreate a volume- Specified by:
createVolumein interfaceDockerAccess- Parameters:
containerConfig- volume configuration- Returns:
- the name of the Volume
- Throws:
DockerAccessException- if the volume could not be created.
-
removeVolume
public void removeVolume(String name) throws DockerAccessException
Description copied from interface:DockerAccessRemoves a volume. It is a no-op if the volume does not exist.- Specified by:
removeVolumein interfaceDockerAccess- Parameters:
name- volume name to remove- Throws:
DockerAccessException- if the volume could not be removed
-
start
public void start()
Description copied from interface:DockerAccessLifecycle method for this access class which must be called before any other method is called.- Specified by:
startin interfaceDockerAccess
-
shutdown
public void shutdown()
Description copied from interface:DockerAccessLifecycle method which must be called when this object is not needed anymore. This hook might be used for cleaning up things.- Specified by:
shutdownin interfaceDockerAccess
-
fetchApiVersionFromServer
public String fetchApiVersionFromServer(String baseUrl, ApacheHttpClientDelegate delegate) throws IOException
- Throws:
IOException
-
-