public class DockerAccessWithHcClient extends Object implements DockerAccess
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).Modifier and Type | Field and Description |
---|---|
static String |
API_VERSION |
Constructor and Description |
---|
DockerAccessWithHcClient(String baseUrl,
String certPath,
int maxConnections,
org.eclipse.jkube.kit.common.KitLogger log)
Create a new access for the given URL
|
Modifier and Type | Method and Description |
---|---|
void |
buildImage(String image,
File dockerArchive,
BuildOptions options)
Create an docker image from a given archive
|
void |
copyArchive(String containerId,
File archive,
String targetPath)
Copy an archive (must be a tar) into a running container
Get all containers matching a certain label.
|
String |
createContainer(ContainerCreateConfig containerConfig,
String containerName)
Create a container from the given image.
|
String |
createExecContainer(String containerId,
org.eclipse.jkube.kit.config.image.build.Arguments arguments)
Sets up an exec instance for a running container id
|
String |
createNetwork(NetworkCreateConfig networkConfig)
Create a custom network from the given configuration.
|
String |
createVolume(VolumeCreateConfig containerConfig)
Create a volume
|
String |
fetchApiVersionFromServer(String baseUrl,
ApacheHttpClientDelegate delegate) |
ContainerDetails |
getContainer(String containerIdOrName)
Get a container
|
List<Container> |
getContainersForImage(String image,
boolean all)
Get all containers which are build from an image.
|
ExecDetails |
getExecContainer(String containerIdOrName)
Get an exec container which is the result of executing a command in a running container.
|
String |
getImageId(String name)
Get the image id of a given name or
null if no such image exists |
LogGetHandle |
getLogAsync(String containerId,
LogCallback callback)
Get logs asynchronously.
|
void |
getLogSync(String containerId,
LogCallback callback)
Get logs for a container up to now synchronously.
|
String |
getServerApiVersion()
Get the API version of the running server
|
boolean |
hasImage(String name)
Check whether the given name exists as image at the docker daemon
|
List<Network> |
listNetworks()
List all networks
|
void |
loadImage(String image,
File tarArchive)
Load an image from an archive.
|
void |
pullImage(String image,
AuthConfig authConfig,
String registry)
Pull an image from a remote registry and store it locally.
|
void |
pushImage(String image,
AuthConfig authConfig,
String registry,
int retries)
Push an image to a registry.
|
void |
removeContainer(String containerId,
boolean removeVolumes)
Remove a container with the given id
|
boolean |
removeImage(String image,
boolean... forceOpt)
Remove an image from this docker installation
|
boolean |
removeNetwork(String networkId)
Remove a custom network
|
void |
removeVolume(String name)
Removes a volume.
|
void |
saveImage(String image,
String filename,
org.eclipse.jkube.kit.config.image.build.ArchiveCompression compression)
Save an image to a tar file
|
void |
shutdown()
Lifecycle method which must be called when this object is not needed anymore.
|
void |
start()
Lifecycle method for this access class which must be called before any other method is called.
|
void |
startContainer(String containerId)
Start a container.
|
void |
startExecContainer(String containerId,
LogOutputSpec outputSpec)
Starts a previously set up exec instance (via
DockerAccess.createExecContainer(String, Arguments) container
this API sets up a session with the exec command. |
void |
stopContainer(String containerId,
int killWait)
Stop a container.
|
void |
tag(String sourceImage,
String targetImage,
boolean force)
Alias an image in the repository with a complete new name.
|
public static final String API_VERSION
public DockerAccessWithHcClient(String baseUrl, String certPath, int maxConnections, org.eclipse.jkube.kit.common.KitLogger log) throws IOException
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 informationIOException
- in case of I/O exceptionpublic String getServerApiVersion() throws DockerAccessException
getServerApiVersion
in interface DockerAccess
DockerAccessException
- if the api version could not be obtainedpublic void startExecContainer(String containerId, LogOutputSpec outputSpec) throws DockerAccessException
DockerAccess
DockerAccess.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).startExecContainer
in interface DockerAccess
containerId
- id of the exec containeroutputSpec
- how to print out the output of the commandDockerAccessException
- if the container could not be created.public String createExecContainer(String containerId, org.eclipse.jkube.kit.config.image.build.Arguments arguments) throws DockerAccessException
DockerAccess
createExecContainer
in interface DockerAccess
containerId
- id of the running container which the exec container will be created forarguments
- container exec commands to runDockerAccessException
- if the container could not be created.public String createContainer(ContainerCreateConfig containerConfig, String containerName) throws DockerAccessException
DockerAccess
The container id
will be set on the container
upon successful creation.
createContainer
in interface DockerAccess
containerConfig
- container configurationcontainerName
- name container should be created with or null
for a docker provided nameDockerAccessException
- if the container could not be created.public void startContainer(String containerId) throws DockerAccessException
DockerAccess
startContainer
in interface DockerAccess
containerId
- id of the container to startDockerAccessException
- if the container could not be started.public void stopContainer(String containerId, int killWait) throws DockerAccessException
DockerAccess
stopContainer
in interface DockerAccess
containerId
- the container idkillWait
- the time to wait between stop and kill (in seconds)DockerAccessException
- if the container could not be stopped.public void buildImage(String image, File dockerArchive, BuildOptions options) throws DockerAccessException
DockerAccess
buildImage
in interface DockerAccess
image
- name of the image to build or null
if none should be useddockerArchive
- from which the docker image should be buildoptions
- additional query arguments to add when building the image. Can be null.DockerAccessException
- if docker host reports an error during building of an imagepublic void copyArchive(String containerId, File archive, String targetPath) throws DockerAccessException
DockerAccess
copyArchive
in interface DockerAccess
containerId
- container to copy intoarchive
- local archive to copy intotargetPath
- target path to useDockerAccessException
- if the archive could not be copiedpublic void getLogSync(String containerId, LogCallback callback)
DockerAccess
getLogSync
in interface DockerAccess
containerId
- container idcallback
- which is called for each line receivedpublic LogGetHandle getLogAsync(String containerId, LogCallback callback)
DockerAccess
getLogAsync
in interface DockerAccess
containerId
- id of the container for which to fetch the logscallback
- to call when log data arrivespublic List<Container> getContainersForImage(String image, boolean all) throws DockerAccessException
DockerAccess
getContainersForImage
in interface DockerAccess
image
- for which its container are looked upall
- whether to fetch also stopped containers. If false only running containers are returnedContainer
objects or an empty list if none is foundDockerAccessException
- if the request failspublic ContainerDetails getContainer(String containerIdOrName) throws DockerAccessException
DockerAccess
getContainer
in interface DockerAccess
containerIdOrName
- container id or nameContainerDetails
representing the container or null if none could be foundDockerAccessException
- if the container could not be inspectedpublic ExecDetails getExecContainer(String containerIdOrName) throws DockerAccessException
DockerAccess
getExecContainer
in interface DockerAccess
containerIdOrName
- exec container id or nameExecDetails
representing the container or null if none could be foundDockerAccessException
- if the container could not be inspectedpublic boolean hasImage(String name) throws DockerAccessException
DockerAccess
hasImage
in interface DockerAccess
name
- image name to checkDockerAccessException
- docker access exceptionpublic String getImageId(String name) throws DockerAccessException
DockerAccess
null
if no such image existsgetImageId
in interface DockerAccess
name
- name to lookupnull
DockerAccessException
- docker access exceptionpublic void removeContainer(String containerId, boolean removeVolumes) throws DockerAccessException
DockerAccess
removeContainer
in interface DockerAccess
containerId
- container id for the container to removeremoveVolumes
- if true, will remove any volumes associated to containerDockerAccessException
- if the container couldn't be removed.public void loadImage(String image, File tarArchive) throws DockerAccessException
DockerAccess
loadImage
in interface DockerAccess
image
- the image to pull.tarArchive
- archive fileDockerAccessException
- if the image couldn't be loaded.public void pullImage(String image, AuthConfig authConfig, String registry) throws DockerAccessException
DockerAccess
pullImage
in interface DockerAccess
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.DockerAccessException
- if the image couldn't be pulled.public void pushImage(String image, AuthConfig authConfig, String registry, int retries) throws DockerAccessException
DockerAccess
pushImage
in interface DockerAccess
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 errorDockerAccessException
- in case pushing failspublic void saveImage(String image, String filename, org.eclipse.jkube.kit.config.image.build.ArchiveCompression compression) throws DockerAccessException
DockerAccess
saveImage
in interface DockerAccess
image
- image to savefilename
- target filenamecompression
- compression to use for the archiveDockerAccessException
- if an image cannot be removedpublic void tag(String sourceImage, String targetImage, boolean force) throws DockerAccessException
DockerAccess
tag
in interface DockerAccess
sourceImage
- full name (including tag) of the image to aliastargetImage
- the alias nameforce
- forced taggingDockerAccessException
- if the original image doesn't exist or another error occurs somehow.public boolean removeImage(String image, boolean... forceOpt) throws DockerAccessException
DockerAccess
removeImage
in interface DockerAccess
image
- image to removeforceOpt
- if set to true remove containers as well (only the first vararg is evaluated)DockerAccessException
- if an image cannot be removedpublic List<Network> listNetworks() throws DockerAccessException
DockerAccess
listNetworks
in interface DockerAccess
Network
objectsDockerAccessException
- if the networks could not be listedpublic String createNetwork(NetworkCreateConfig networkConfig) throws DockerAccessException
DockerAccess
createNetwork
in interface DockerAccess
networkConfig
- network configurationDockerAccessException
- if the container could not be created.public boolean removeNetwork(String networkId) throws DockerAccessException
DockerAccess
removeNetwork
in interface DockerAccess
networkId
- network to removeDockerAccessException
- if an image cannot be removedpublic String createVolume(VolumeCreateConfig containerConfig) throws DockerAccessException
DockerAccess
createVolume
in interface DockerAccess
containerConfig
- volume configurationDockerAccessException
- if the volume could not be created.public void removeVolume(String name) throws DockerAccessException
DockerAccess
removeVolume
in interface DockerAccess
name
- volume name to removeDockerAccessException
- if the volume could not be removedpublic void start()
DockerAccess
start
in interface DockerAccess
public void shutdown()
DockerAccess
shutdown
in interface DockerAccess
public String fetchApiVersionFromServer(String baseUrl, ApacheHttpClientDelegate delegate) throws IOException
IOException
Copyright © 2020. All rights reserved.