Search code examples
javadockerfabric8

Push docker image to remote registry using fabric8 java client


I am trying to push a docker image to dockerhub using fabric8 docker client for java.

I am getting following error when trying to push the image.

The push refers to repository [docker.io/anuruddhal/hello-world-docker] Failure:denied: requested access to the resource is denied

How can I properly authenticate the client?

Following is my code:

public static void pushImage(DockerModel dockerModel) throws InterruptedException, IOException {

        String dockerUrl = "unix:///var/run/docker.sock";
        String image = dockerModel.getName();
        String registry = "index.docker.io";
        String namespace = "anuruddhal";

        String repositoryName = registry + "/" + namespace + "/" + image;

        Config config = new ConfigBuilder()
                .withDockerUrl(dockerUrl)
                .withUsername("anuruddhal")
                .withPassword("xxxxxxxxx")
                .build();

        DockerClient client = new DefaultDockerClient(config);
        final CountDownLatch pushDone = new CountDownLatch(1);

        client.image().withName(image).tag().inRepository(repositoryName).force().withTagName("1.0");

        OutputHandle handle = client.image().withName(repositoryName).push()
                .usingListener(new EventListener() {
                    @Override
                    public void onSuccess(String message) {
                        printSuccess("Success:" + message);
                        pushDone.countDown();
                    }

                    @Override
                    public void onError(String messsage) {
                        printError("Failure:" + messsage);
                        pushDone.countDown();
                    }

                    @Override
                    public void onError(Throwable t) {
                        printError(t.getMessage());
                        pushDone.countDown();
                    }

                    @Override
                    public void onEvent(String event) {
                        printDebug(event);
                    }
                })
                .withTag("1.0")
                .toRegistry();

        pushDone.await();
        handle.close();
        client.close();
    }

Solution

  • I figured it out.

    You need to add auth config to the client along with the registry URL.

    AuthConfig authConfig = new AuthConfigBuilder().withUsername("anuruddhal").withPassword
                    ("xxxxxxx")
                    .build();
            Config config = new ConfigBuilder()
                    .withDockerUrl(dockerUrl)
                    .addToAuthConfigs("index.docker.io", authConfig)
                    .build();