0

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.

TomGrill Games
  • 1,553
  • 2
  • 16
  • 25
  • Please mention what errors are you getting with this code. – mchawre Jun 21 '19 at 15:47
  • I don't get any error. I just don't know how to use the docker-java API to add the mentioned flag. Did search doc and source code but did not find anything. – TomGrill Games Jun 21 '19 at 15:49
  • Is it fine if the registry auth is provided in your dockerClient object itself? – mchawre Jun 21 '19 at 15:52
  • Building DockerClient object with `.withRegistryUsername(), .withRegistryPassword()` did not change anything. – TomGrill Games Jun 21 '19 at 15:56
  • What was the poblem with these two options, or you just got no output. – mchawre Jun 21 '19 at 15:58
  • Have you tried all the ways mentioned here https://github.com/docker-java/docker-java#programmatic to specify the registry details. – mchawre Jun 21 '19 at 16:02
  • I did, the command runs. Service is created and then fails cause it cannot download the image on my node. I assume registry authentication details are not sent to swarm agents cause the flag is missing. – TomGrill Games Jun 21 '19 at 16:07

2 Answers2

0

Specify registry auth details in dockerClient object itself in your code.

Check this out.

If it didn't worked. First verify whether the registry auth details you have are the correct one and manually try to pull docker image using docker cli.

Put registry config in .docker/config.json file and try to pull docker images. And then backtrace the issues.

mchawre
  • 10,744
  • 4
  • 35
  • 57
  • Thanks, docker cli works, see question. I think either docker-java does not provide a way to pass the option/flag or I cannot find how. – TomGrill Games Jun 21 '19 at 16:32
0

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("foo@bar.com")
                .withRegistryAddress("your.registry.address.here");

        dockerClient.createServiceCmd(new ServiceSpec()
                .withName(SERVICE_NAME)
                .withTaskTemplate(new TaskSpec()
                        .withContainerSpec(new ContainerSpec()
                                .withImage(DEFAULT_IMAGE))))
                .withAuthConfig(authConfig)
                .exec();