Search code examples
dockerdocker-java

docker-java equivalent of "docker service create --with-registry-auth"


Using https://github.com/docker-java/docker-java and looking for a way to add the --with-registry-auth option from https://docs.docker.com/v17.12/engine/reference/commandline/service_create/#options

val createCmd = dockerClient.createServiceCmd(
                ServiceSpec()
                        .withName("name")

                        .withTaskTemplate(TaskSpec()
                                .withContainerSpec(ContainerSpec()
                                        .withEnv(envs)
                                        .withImage("image")
                                        .withMounts(mounts)
                                )
                                .withNetworks(networks)
                                .withPlacement(ServicePlacement()
                                        .withConstraints(constraints))
                        )
        )

To clarify this more:

I am looking for docker-java way to do this command (this does work!):

docker service create --with-registry-auth --constraint 'node.labels.mynodeid==7' myprivateregistry.foo:5000/imagename:latest

Removing the --with-registry-auth like this

docker service create --constraint 'node.labels.mynodeid==7' myprivateregistry.foo:5000/imagename:latest

will bringt up this error: No such image: myprivateregistry.foo:5000/imagename:latest because the credentials, which are valid btw, are not passed to the node.


Solution

  • As of docker-java 3.2.0-rc5 you can now specify authConfig to pull images from a private registry

            AuthConfig authConfig = new AuthConfig()
                    .withUsername("testuser")
                    .withPassword("testpassword")
                    .withEmail("[email protected]")
                    .withRegistryAddress("your.registry.address.here");
    
            dockerClient.createServiceCmd(new ServiceSpec()
                    .withName(SERVICE_NAME)
                    .withTaskTemplate(new TaskSpec()
                            .withContainerSpec(new ContainerSpec()
                                    .withImage(DEFAULT_IMAGE))))
                    .withAuthConfig(authConfig)
                    .exec();