Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A simple docker client for the JVM

License

NotificationsYou must be signed in to change notification settings

naku/docker-client

 
 

Repository files navigation

Build StatuscodecovMaven CentralLicense

This is aDocker client written in Java.It is used it in many critical production systems at Spotify.

Version compatibility

docker-client is built and tested against the six most recent minor releases of Docker.Right now these are 1.9.1 - 17.04.0-ce (specifically the oneshere).We upload the artifact tested on Docker 1.13.1.SeeDocker docs on the mapping between Docker version and API version.

Download

Download the latest JAR or grabvia Maven.

<dependency>  <groupId>com.spotify</groupId>  <artifactId>docker-client</artifactId>  <version>LATEST-VERSION</version></dependency>

Usage Example

// Create a client based on DOCKER_HOST and DOCKER_CERT_PATH env varsfinalDockerClientdocker =DefaultDockerClient.fromEnv().build();// Pull an imagedocker.pull("busybox");// Bind container ports to host portsfinalString[]ports = {"80","22"};finalMap<String,List<PortBinding>>portBindings =newHashMap<>();for (Stringport :ports) {List<PortBinding>hostPorts =newArrayList<>();hostPorts.add(PortBinding.of("0.0.0.0",port));portBindings.put(port,hostPorts);}// Bind container port 443 to an automatically allocated available host port.List<PortBinding>randomPort =newArrayList<>();randomPort.add(PortBinding.randomPort("0.0.0.0"));portBindings.put("443",randomPort);finalHostConfighostConfig =HostConfig.builder().portBindings(portBindings).build();// Create container with exposed portsfinalContainerConfigcontainerConfig =ContainerConfig.builder()    .hostConfig(hostConfig)    .image("busybox").exposedPorts(ports)    .cmd("sh","-c","while :; do sleep 1; done")    .build();finalContainerCreationcreation =docker.createContainer(containerConfig);finalStringid =creation.id();// Inspect containerfinalContainerInfoinfo =docker.inspectContainer(id);// Start containerdocker.startContainer(id);// Exec command inside running container with attached STDOUT and STDERRfinalString[]command = {"sh","-c","ls"};finalExecCreationexecCreation =docker.execCreate(id,command,DockerClient.ExecCreateParam.attachStdout(),DockerClient.ExecCreateParam.attachStderr());finalLogStreamoutput =docker.execStart(execCreation.id());finalStringexecOutput =output.readFully();// Kill containerdocker.killContainer(id);// Remove containerdocker.removeContainer(id);// Close the docker clientdocker.close();

Getting Started

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

Prerequisites

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

A note on using Docker for Mac

If you are using Docker for Mac andDefaultDockerClient.fromEnv(), it might not be clearwhat value to use for theDOCKER_HOST environment variable. The value you should use isDOCKER_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() usesunix:///var/run/docker.sock on OS X by default.

Testing

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

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

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

Releasing

Commits to the master branch will trigger our continuous integration agent to build the jar andrelease 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 usedfor 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 onthe shaded dependencies in the docker-client jar.

Standard:

<dependency>  <groupId>com.spotify</groupId>  <artifactId>docker-client</artifactId>  <version>3.5.12</version></dependency>

Shaded:

<dependency>  <groupId>com.spotify</groupId>  <artifactId>docker-client</artifactId>  <classifier>shaded</classifier>  <version>3.5.12</version></dependency>

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

Code of conduct

This project adheres to theOpen Code of Conduct. By participating, you areexpected to honor this code.

About

A simple docker client for the JVM

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java99.7%
  • Shell0.3%

[8]ページ先頭

©2009-2025 Movatter.jp