Docker client for the Docker API version 1.21.
- Rich DSL
- Unix Domain Socket
- Docker Server Mock
- OSGi support (includes Apache Karaf Feature Repository)
Config config = new ConfigBuilder()
.withDockerUrl("http://someip:2375")
.build();
DockerClient client = new DefaultDockerClient(config);
The config instance can be configured via ConfigBuilder or via enviornment variables (e.g. DOCKER_HOST). In the later case, you can directly isntantiate the DefaultDockerClient:
DockerClient client = new DefaultDockerClient();
To list all images:
client.image().list().allImages();
To list images without the intermediate
client.image().list().endImages();
To add some key/values as filters:
client.image().list().filters('someKey','someVal').allImages();
To create an image from a folder:
client.build().withRepositoryName("myimage").fromFolder(".")
To create an image from an external tarball:
client.build().withRepositoryName("myimage").fromTar("/path/to/tarball")
or using a stream to the tarball:
client.build().withRepositoryName("myimage").fromTar(tarballInputStream)
Build operations are executed asynchronously, so you may need to use callbacks for, progress, success or failure:
OutputHandle handle = client.build().withRepositoryName("my/image")
.usingListener(new EventListener() {
@Override
public void onSuccess(String message) {
System.out.println("Success:" + message);
}
@Override
public void onError(String messsage) {
System.err.println("Failure:" +messsage);
}
@Override
public void onEvent(String event) {
System.out.println(event);
}
})
.fromFolder(".");
If you just need to grab the output rather than using callbacks you can either redirect output to the handle:
OutputHandle handle = client.build().withRepositoryName("my/image")
.redirectingOutput()
.fromFolder(".");
OutputStream out = handle.getOutput();
...
handle.close();
or even bring you own stream:
OutputHandle handle = client.build().withRepositoryName("my/image")
.writingOutput(System.out)
.fromFolder(".");
There are tons of options as described in the docker remote api and all of them are exposed as methods in the DSL.
To tag an image:
client.image().withName("my/image").tag().inRepository("192.168.1.10:5000/my/image").withTagName("v1");
In the example above we tag and image into a repository prefixed with a local docker registry address.
To push a tag into the registry:
client.image().withName("192.168.1.10:5000/my/image").push().withTag("v1").toRegistry();
ImageInspect inspect = client.image().withName("my/image").inspect();
client.image().withName("192.168.1.10:5000/my/image").pull().withTag("v1").fromRegistry();
Boolean deleted = client.image().withName("192.168.1.10:5000/my/image").delete();
To up a mysql container, environment variables should be given. it is can be done using ".withEnv(envVariable)". This is an example.
Map<String, String> envVariable = new HashMap<String, String>();
envVariable.put("MYSQL_ROOT_PASSWORD", "admin");
envVariable.put("MYSQL_USER","admin");
envVariable.put("MYSQL_PASSWORD", "admin");
envVariable.put("MYSQL_DATABASE", "test_demo");
ContainerCreateResponse container = client.container().createNew()
.withName("mysql_server")
.withEnv(envVariable)
.withImage("mysql")
.done();
Map<String, ArrayList<PortBinding>> portBinding = new HashMap<>();
ArrayList<PortBinding> hostPort = new ArrayList<>();
PortBinding portBinding1 = new PortBinding("localhost", "6060");
hostPort.add(portBinding1);
portBinding.put("3306/tcp", hostPort);
HostConfig hostConfig = new HostConfig();
hostConfig.setPortBindings(portBinding);
ContainerCreateResponse container = client.container().createNew()
.withName("mysql_server")
.withHostConfig(hostConfig)
.withEnv(envVariable)
.withImage("mysql")
.done();