• Stars
    star
    101
  • Rank 326,045 (Top 7 %)
  • Language
    Groovy
  • License
    MIT License
  • Created almost 10 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Docker client for Java written in Kotlin and Groovy

Build Status Maven Central API Coverage

Docker Client

A Docker HTTP client for the Java VM written in Groovy

This client library aims at supporting all existing Docker api endpoints, which effectively allows to use it in place of the official Docker client binary.

See the supported-api.md for details about the current api coverage.

All platforms are natively supported, which includes:

Consider the client as a thin wrapper to perform HTTP requests, minimizing the need to manually configure TLS or auth encoding in your code. Most commonly known environment variables will work as expected, e.g. DOCKER_HOST, DOCKER_TLS_VERIFY, or DOCKER_CERT_PATH. Due to its thin layer this client might feel a bit less convenient, though, while it gives you a bit more freedom to access the engine api and some less popular endpoints.

The client also includes Docker Compose version 3 support as part of the docker stack --compose ... command. See the docker stack deploy docs for details. Please note that you'll need at least Java 8 when using that feature.

Plain Usage

For use in Gradle, ensure that you declared Maven Central as repository:

repositories {
  mavenCentral()
}

Then, you need to add the dependency, but please ensure to use the latest version:

dependencies {
  implementation("de.gesellix:docker-client:2021-02-20T21-57-11")
}

The tests in DockerClientImplSpec and DockerClientImplIntegrationSpec should give you an idea how to use the docker-client.

The default Docker host is expected to be available at unix:///var/run/docker.sock. When using Docker Machine the existing environment variables as configured by eval "$(docker-machine env default)" should be enough.

Even for the native packages Docker for Mac and Docker for Windows you'll be able to rely on the default configuration. For Mac the default is the same as for Linux at unix:///var/run/docker.sock, while Windows uses the named pipe at //./pipe/docker_engine.

You can override existing DOCKER_* environment variables with Java system properties like this:

System.setProperty("docker.host", "192.168.99.100")
System.setProperty("docker.cert.path", "/Users/${System.getProperty('user.name')}/.docker/machine/machines/default")

Please note that the raw responses (including headers) from the Docker daemon are returned, with the actual response body being available in the content attribute. Some endpoints return a stream, which is then available in stream. For some cases, like following the logs or events stream, you need to provide a callback which is called for every response line, see example 3 below.

Example 1: docker info

A basic example connecting to a Docker daemon running in a VM (boot2docker/machine) looks like this:

System.setProperty("docker.cert.path", "/Users/${System.getProperty('user.name')}/.docker/machine/machines/default")
def dockerClient = new DockerClientImpl(System.getenv("DOCKER_HOST"))
def info = dockerClient.info().content

Example 2: docker run

Running a container being available on the host via HTTP port 4712 can be achieved like this:

System.setProperty("docker.cert.path", "/Users/${System.getProperty('user.name')}/.docker/machine/machines/default")
def dockerClient = new DockerClientImpl(System.getenv("DOCKER_HOST"))
def image = "busybox"
def tag = "latest"
def cmds = ["sh", "-c", "ping 127.0.0.1"]
def containerConfig = ["Cmd"         : cmds,
                       "ExposedPorts": ["4711/tcp": [:]],
                       "HostConfig"  : ["PortBindings": [
                               "4711/tcp": [
                                       ["HostIp"  : "0.0.0.0",
                                        "HostPort": "4712"]]
                       ]]]
def result = dockerClient.run(image, containerConfig, tag).content

Example 3: docker logs --follow

def callback = new DockerAsyncCallback() {
    def lines = []

    @Override
    def onEvent(Object line) {
        println line
        lines << line
    }
}

dockerClient.logs("foo", [tail: 1], callback)

// callback.lines will now collect all log lines
// you might implement it as a fifo instead of the List shown above

Example 4: docker stack deploy --compose-file docker-stack.yml example

def dockerClient = new DockerClientImpl()
dockerClient.initSwarm()

def namespace = "example"
def composeStack = getClass().getResourceAsStream('docker-stack.yml')
String workingDir = Paths.get(getClass().getResource('docker-stack.yml').toURI()).parent

def deployConfig = new DeployConfigReader(dockerClient).loadCompose(namespace, composeStack, workingDir)

def options = new DeployStackOptions()
dockerClient.stackDeploy(namespace, deployConfig, options)

Usage with Gradle Docker Plugin

My personal focus implementing this Docker client was to leverage the Docker engine API in our Gradle scripts. A convenient integration in Gradle is possible by using the Gradle Docker Plugin, which will be developed along with the Docker client library.

Contributing and Future Plans

If something doesn't work as expected or if you have suggestions, please create an issue. Pull requests are welcome as well!

The developer api is quite rough, but that's where you can certainly help: I'll add a more convenient layer on top of the raw interface so that for 80% of common use cases the Docker client should help you to get it working without digging too deep into the source code. So, all I need is an indication about how you'd like that convenience layer to look like. Feel free to create an issue where we can discuss how your use case could be implemented!

Buy Me a Coffee at ko-fi.com

Publishing/Release Workflow

See RELEASE.md

License

MIT License

Copyright 2015-2021 Tobias Gesellchen (@gesellix)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

keepass-node

KeePass2 editor for Node.js with a browser frontend
HTML
81
star
2

gradle-docker-plugin

Gradle Docker plugin
Groovy
78
star
3

graceful-shutdown-spring-boot

Graceful Shutdown with Spring Boot (Demo)
Kotlin
57
star
4

couchdb-prometheus-exporter

CouchDB stats exporter for Prometheus
Go
53
star
5

docker-haproxy-network

Showcase using the Docker 1.10 private network in combination with the HAProxy 1.6 DNS resolution
HTML
42
star
6

gradle-debian-plugin

Gradle plugin to create Debian packages
Groovy
39
star
7

pipeline-with-gradle-and-docker

Continuous Deployment with Gradle and Docker example project
JavaScript
33
star
8

inject-docker-certs

Adding certificates to the Docker for Mac beta
Shell
27
star
9

Nimbus-JOSE-JWT

Fork of https://bitbucket.org/connect2id/nimbus-jose-jwt
Java
27
star
10

gradle-docker-plugin-example

Gradle-Docker-Plugin example
Kotlin
25
star
11

couchdb-cluster-config

Util to configure a CouchDB 2.x cluster with several nodes.
Go
24
star
12

drone-stack

Example setup of Drone CI in a Docker Stack using Swarm mode
Shell
21
star
13

swarm-examples

Beispiel-Code zum Artikel "Continuous Deployment mit Docker Swarm" im Java Magazin 12.17
Shell
19
star
14

go-npipe

A Windows Named Pipe Http Echo Server
Go
11
star
15

angular-pouchdb

Fully fledged sync and offline support for AngularJS with CouchDB backend
JavaScript
9
star
16

haveged

entropy matters
9
star
17

couchdb-2-elasticsearch

An Elasticsearch CouchDB River replacement
JavaScript
8
star
18

javamail-android-forked

Fork from http://code.google.com/p/javamail-android/
Java
7
star
19

health-exporter

Health Exporter for Prometheus
Go
6
star
20

unix-socket-factory

Adds unix socket support to the Apache HttpClient
Java
4
star
21

docker-client-shootout

Showcase of several Docker clients for the JVM
Java
3
star
22

prometheus-rpi

3
star
23

prometheus-indicator

Show Prometheus Alerts in Your System Tray
JavaScript
3
star
24

ossrh-test

Kotlin
2
star
25

gesellix

the one and only
CSS
2
star
26

listsubscribe

Joomla! ListSubscribe module
PHP
2
star
27

ansible-docker

An Ansible image based on Alpine Linux
2
star
28

titlelink

Joomla! TitleLink plugin
PHP
2
star
29

gioui-splash

A cross-platform splash screen for Go, based on Gio UI
Go
2
star
30

couchdb-client

A CouchDB client written in Groovy
Groovy
2
star
31

fest-assert-awt-2.x

Java
1
star
32

gesellix.github.com

github page
1
star
33

mountebank

a simple mountebank docker image
Dockerfile
1
star
34

shortlink

Joomla! Short Links extensions
PHP
1
star
35

awsume

Containerized AWSume and AWS CLI
Dockerfile
1
star
36

dotplot

DotPlot fork from thm.de
Java
1
star
37

reflections-forked

Fork from google code/SVN: http://code.google.com/p/reflections/
Java
1
star
38

auto-avd

Automatically create and start clean Android AVD snapshots for testing, with an emphasis for DEV@cloud.
Shell
1
star
39

Hypoport

Java
1
star
40

playground

personal playground
JavaScript
1
star
41

annotationprocessor-in-subproject

Example for an annotation processor in a Gradle subproject
Java
1
star
42

googlechartwrapper-forked

frok from http://code.google.com/p/googlechartwrapper/
Java
1
star
43

junixsocket-old

Fork of the Unix Domain Sockets for Java, from https://code.google.com/p/junixsocket/
Java
1
star