GithubHelp home page GithubHelp logo

isabella232 / docker-client-3 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stakater/docker-client

0.0 0.0 0.0 550 KB

License: Apache License 2.0

Shell 0.01% Java 95.24% Groovy 0.19% Go 4.27% Makefile 0.29%

docker-client-3's Introduction

Docker Client

Maven Central Javadocs

Docker client for the Docker API version 1.21.

Features

  • Rich DSL
  • Unix Domain Socket
  • Docker Server Mock
  • OSGi support (includes Apache Karaf Feature Repository)

Configuring the client

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();

Images

List Images

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();

Building an image

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.

Tagging an image

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.

Pushing an image

To push a tag into the registry:

client.image().withName("192.168.1.10:5000/my/image").push().withTag("v1").toRegistry();

Inspecting an image

ImageInspect inspect = client.image().withName("my/image").inspect();

Pulling an image

client.image().withName("192.168.1.10:5000/my/image").pull().withTag("v1").fromRegistry();

Deleting an image

Boolean deleted = client.image().withName("192.168.1.10:5000/my/image").delete();

Give environment variables(-e)

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();

Port Binding

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();

docker-client-3's People

Contributors

bartoszmajsak avatar chamilad avatar dipak-pawar avatar fusesource-ci avatar imesh avatar iocanel avatar jimmidyson avatar kavinduzoysa avatar khazrak avatar rawlingsj avatar rhuss avatar thxmasj avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.