Pushing images to docker registry using docker-client api

风流意气都作罢 提交于 2019-12-12 21:07:43

问题


While exploring docker-client api (java) what certificates are exactly required for setting up connection to the docker daemon running on a vm.

The code which I found online :

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

// Pull an image
docker.pull("busybox");

}

The link to above example is available at : https://github.com/spotify/docker-client

What I intend to do is access docker-daemon running on my master node(deployed through magnum api) and push my java applications docker-image onto the registry so that I can create pods on my cluster using these images.

I am not sure what more needs to be done as I am new to this.


回答1:


I did something similar, created images on a remote docker daemon and pushed it to a repository and further spawned containers. I used docker-java client and it works fine. You might want to go through their wiki, the issues page as usual proved to be very helpful for me.

Basic Steps:

  1. Basically you need to create a DockerClientConfig which is
    something like:

    public DockerClientConfig dockerClientConfig() {
       return DefaultDockerClientConfig.createDefaultConfigBuilder()
           .withDockerHost("IP_where_docker_daemon_is_running_with_port")
           .withDockerTlsVerify("Transport_Layer_Security_accepts_Boolean")
           .withDockerCertPath("If_tls_is_true")
           .build();
    }
    
  2. Then you need to create a DockerClient

    public DockerClient dockerClient() {
        DockerClient dockerClient = DockerClientBuilder.getInstance(dockerClientConfig())
            .withDockerCmdExecFactory(nettyDockerCmdExecFactory())
            .build();
        return dockerClient;
    }
    
  3. Now you're ready to make calls to create Image, push Image, startContainer etc.

       File baseDir = new File(appImage.getSourceUri());
    
        BuildImageResultCallback callback = new BuildImageResultCallback(){
            @Override
            public void onNext(BuildResponseItem item){
                System.out.println("It's done - > " + item);
                super.onNext(item);
            }
        };
    
        PushImageResultCallback pushImageResultCallback = new PushImageResultCallback(){
            @Override
            public void onNext(PushResponseItem item){
                System.out.println("It's done too - >" + item);
                super.onNext(item);
            }
        };
    
    
        dockerClient.buildImageCmd(baseDir).withTag(appImage.getRegistryEndpoint()+"/apps/test:" + appImage
                               .getName()).exec(callback).awaitImageId();
    
        dockerClient.listImagesCmd().withShowAll(true).exec();
    
        AuthConfig authConfig = new AuthConfig().withUsername("admin").withPassword("admin")
                                                .withRegistryAddress("http://some_ip/v2/");
    
        dockerClient.pushImageCmd(appImage.getName())
                    .withAuthConfig(authConfig)
                    .withName(appImage
                    .getRegistryEndpoint()+ "/apps/test")
                    .withTag(appImage.getName())
                    .exec(pushImageResultCallback).awaitSuccess();
    
        CreateContainerResponse containerResponse = dockerClient.createContainerCmd(appImage
            .getRegistryEndpoint()+ "/apps/test:" + appImage.getName())
            .exec();
    
        dockerClient.startContainerCmd(containerResponse.getId()).exec();
    

That should be enough to get you started.




回答2:


Perhaps that you could also simply use Jib core APIs which is a simple Java library that doesn't need any docker installed to build + push an image!

Read this Google blog post to know more about it.



来源:https://stackoverflow.com/questions/44624805/pushing-images-to-docker-registry-using-docker-client-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!