A simple docker client for the JVM
Java Shell
Latest commit 85697ea Feb 22, 2017 @davidxia davidxia committed on GitHub Merge pull request #643 from giftig/master
Add "Stop a container" section to user manual


Docker Client

Build Status codecov Maven Central License

This is a simple Docker client written in Java. We build and test docker-client on Docker versions 1.6 - 1.12 (specifically the ones here). We upload the artifact tested on Docker 1.12.1. See Docker docs on the mapping between Docker version and API version. We consider this library pretty stable and use it in many critical production systems.


Download the latest JAR or grab via Maven.


Usage Example

// Create a client based on DOCKER_HOST and DOCKER_CERT_PATH env vars
final DockerClient docker = DefaultDockerClient.fromEnv().build();

// Pull an image

// Pull an image from a private repository
// Server address defaults to "https://index.docker.io/v1/"
RegistryAuth registryAuth = RegistryAuth.builder().email("foo@bar.com").username("foobar")
docker.pull("foobar/busybox-private:latest", registryAuth);

// You can also set the RegistryAuth for the DockerClient instead of passing everytime you call pull()
DockerClient docker = DefaultDockerClient.fromEnv().registryAuth(registryAuth).build();

// Bind container ports to host ports
final String[] ports = {"80", "22"};
final Map<String, List<PortBinding>> portBindings = new HashMap<>();
for (String port : ports) {
    List<PortBinding> hostPorts = new ArrayList<>();
    hostPorts.add(PortBinding.of("", port));
    portBindings.put(port, hostPorts);

// Bind container port 443 to an automatically allocated available host port.
List<PortBinding> randomPort = new ArrayList<>();
portBindings.put("443", randomPort);

final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();

// Create container with exposed ports
final ContainerConfig containerConfig = ContainerConfig.builder()
    .cmd("sh", "-c", "while :; do sleep 1; done")

final ContainerCreation creation = docker.createContainer(containerConfig);
final String id = creation.id();

// Inspect container
final ContainerInfo info = docker.inspectContainer(id);

// Start container

// Exec command inside running container with attached STDOUT and STDERR
final String[] command = {"bash", "-c", "ls"};
final ExecCreation execCreation = docker.execCreate(
    id, command, DockerClient.ExecCreateParam.attachStdout(),
final LogStream output = docker.execStart(execCreation.id());
final String execOutput = output.readFully();

// Kill container

// Remove container

// Close the docker client

Getting Started

If you're looking for how to use docker-client, see the User Manual. If you're looking for how to build and develop it, keep reading.


docker-client should be buildable on any platform with Docker 1.6+, JDK7+, and a recent version of Maven 3.

A note on using Docker for Mac

If you are using Docker for Mac and DefaultDockerClient.fromEnv(), it might not be clear what value to use for the DOCKER_HOST environment variable. The value you should use is DOCKER_HOST=unix:///var/run/docker.sock, at least as of version 1.11.1-beta11.

As of version 4.0.8 of docker-client, DefaultDockerClient.fromEnv() uses unix:///var/run/docker.sock on OS X by default.


If you're running a recent version of docker (>= 1.12), which contains native swarm support, please ensure that you run docker swarm init to initialize the docker swarm.

Make sure Docker daemon is running and that you can do docker ps.

You can run tests on their own with mvn test. Note that the tests start and stop a large number of containers, so the list of containers you see with docker ps -a will start to get pretty long after many test runs. You may find it helpful to occassionally issue docker rm $(docker ps -aq).


Commits to the master branch will trigger our continuous integration agent to build the jar and release by uploading to Sonatype. If you are a project maintainer with the necessary credentials, you can also build and release locally by running the below.

mvn clean [-DskipTests -Darguments=-DskipTests] -Dgpg.keyname=<key ID used for signing artifacts> release:prepare release:perform

A note on shading

Please note that in releases 2.7.6 and earlier, the default artifact was the shaded version. When upgrading to version 2.7.7, you will need to include the shaded classifier if you relied on the shaded dependencies in the docker-client jar.





This is particularly important if you use Jersey 1.x in your project. To avoid conflicts with docker-client and Jersey 2.x, you will need to explicitly specify the shaded version above.

Code of conduct

This project adheres to the Open Code of Conduct. By participating, you are expected to honor this code.