com.spotify.docker.client.DockerClient Java Examples
The following examples show how to use
com.spotify.docker.client.DockerClient.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PushPullIT.java From docker-client with Apache License 2.0 | 6 votes |
@Test public void testPushHubPrivateImageWithAuth() throws Exception { // Push an image to a private repo on Docker Hub and check it succeeds final String dockerDirectory = Resources.getResource("dockerDirectory").getPath(); final RegistryAuth registryAuth = RegistryAuth.builder() .username(HUB_AUTH_USERNAME) .password(HUB_AUTH_PASSWORD) .build(); final DockerClient client = DefaultDockerClient .fromEnv() .registryAuthSupplier(new FixedRegistryAuthSupplier( registryAuth, RegistryConfigs.create(singletonMap(HUB_NAME, registryAuth)))) .build(); client.build(Paths.get(dockerDirectory), HUB_PRIVATE_IMAGE); client.push(HUB_PRIVATE_IMAGE); }
Example #2
Source File: AgentStatusReportExecutor.java From docker-swarm-elastic-agent-plugin with Apache License 2.0 | 6 votes |
public GoPluginApiResponse execute() { String elasticAgentId = request.getElasticAgentId(); JobIdentifier jobIdentifier = request.getJobIdentifier(); LOG.info(String.format("[status-report] Generating status report for agent: %s with job: %s", elasticAgentId, jobIdentifier)); try { final DockerClient dockerClient = dockerClientFactory.docker(request.getClusterProfileProperties()); Service dockerService = findService(elasticAgentId, jobIdentifier, dockerClient); DockerServiceElasticAgent elasticAgent = DockerServiceElasticAgent.fromService(dockerService, dockerClient); final String statusReportView = builder.build(builder.getTemplate("agent-status-report.template.ftlh"), elasticAgent); JsonObject responseJSON = new JsonObject(); responseJSON.addProperty("view", statusReportView); return DefaultGoPluginApiResponse.success(responseJSON.toString()); } catch (Exception e) { return StatusReportGenerationErrorHandler.handle(builder, e); } }
Example #3
Source File: RedisContainer.java From pay-publicapi with MIT License | 6 votes |
void stop() { if (stopped) { return; } try { stopped = true; System.err.println("Killing redis container with ID: " + containerId); LogStream logs = docker.logs(containerId, DockerClient.LogsParam.stdout(), DockerClient.LogsParam.stderr()); System.err.println("Killed container logs:\n"); logs.attach(System.err, System.err); docker.stopContainer(containerId, 5); docker.removeContainer(containerId); } catch (DockerException | InterruptedException | IOException e) { System.err.println("Could not shutdown " + containerId); e.printStackTrace(); } }
Example #4
Source File: SystemTestBase.java From helios with Apache License 2.0 | 6 votes |
protected List<Container> listContainers(final DockerClient dockerClient, final String needle) throws DockerException, InterruptedException { final List<Container> containers = dockerClient.listContainers(); final List<Container> matches = Lists.newArrayList(); for (final Container container : containers) { if (container.names() != null) { for (final String name : container.names()) { if (name.contains(needle)) { matches.add(container); break; } } } } return matches; }
Example #5
Source File: SupervisorFactory.java From helios with Apache License 2.0 | 6 votes |
public SupervisorFactory(final AgentModel model, final DockerClient dockerClient, final Map<String, String> envVars, final ServiceRegistrar registrar, final List<ContainerDecorator> containerDecorators, final DockerHost dockerHost, final String host, final SupervisorMetrics supervisorMetrics, final String namespace, final String defaultRegistrationDomain, final List<String> dns) { this.dockerClient = dockerClient; this.namespace = namespace; this.model = checkNotNull(model, "model"); this.envVars = checkNotNull(envVars, "envVars"); this.registrar = registrar; this.containerDecorators = containerDecorators; this.dockerHost = dockerHost; this.host = host; this.metrics = supervisorMetrics; this.defaultRegistrationDomain = checkNotNull(defaultRegistrationDomain, "defaultRegistrationDomain"); this.dns = checkNotNull(dns, "dns"); this.agentRunningInContainer = checkIfAgentRunningInContainer(); }
Example #6
Source File: AbstractDockerMojo.java From docker-maven-plugin with Apache License 2.0 | 6 votes |
protected DockerClient buildDockerClient() throws MojoExecutionException { final DefaultDockerClient.Builder builder; try { builder = getBuilder(); final String dockerHost = rawDockerHost(); if (!isNullOrEmpty(dockerHost)) { builder.uri(dockerHost); } final Optional<DockerCertificatesStore> certs = dockerCertificates(); if (certs.isPresent()) { builder.dockerCertificates(certs.get()); } } catch (DockerCertificateException ex) { throw new MojoExecutionException("Cannot build DockerClient due to certificate problem", ex); } builder.registryAuthSupplier(authSupplier()); return builder.build(); }
Example #7
Source File: BuildMojoTest.java From docker-maven-plugin with Apache License 2.0 | 6 votes |
public void testBuildGeneratedDockerFile_CopiesEntireDirectory() throws Exception { final File pom = getPom("/pom-build-copy-entire-directory.xml"); final BuildMojo mojo = setupMojo(pom); final DockerClient docker = mock(DockerClient.class); mojo.execute(docker); verify(docker).build(eq(Paths.get("target/docker")), eq("test-copied-directory"), any(AnsiProgressHandler.class)); final List<String> expectedDockerFileContents = ImmutableList.of( "FROM busybox", "ADD /data /data", "ENTRYPOINT echo" ); assertEquals("wrong dockerfile contents", expectedDockerFileContents, Files.readAllLines(Paths.get("target/docker/Dockerfile"), UTF_8)); assertFileExists("target/docker/data/file.txt"); assertFileExists("target/docker/data/nested/file2"); }
Example #8
Source File: RemoveImageMojoTest.java From docker-maven-plugin with Apache License 2.0 | 6 votes |
public void testRemoveMissingImage() throws Exception { final File pom = getTestFile("src/test/resources/pom-removeImage.xml"); assertNotNull("Null pom.xml", pom); assertTrue("pom.xml does not exist", pom.exists()); final RemoveImageMojo mojo = (RemoveImageMojo) lookupMojo("removeImage", pom); assertNotNull(mojo); final DockerClient docker = mock(DockerClient.class); Mockito.when(docker.removeImage("imageToRemove", true, false)) .thenThrow(new ImageNotFoundException("imageToRemove")); try { mojo.execute(docker); verify(docker).removeImage("imageToRemove", true, false); } catch (DockerException e){ assertFalse("image to remove was missing", e instanceof ImageNotFoundException); } }
Example #9
Source File: DnsServerTest.java From helios with Apache License 2.0 | 6 votes |
@Test public void testDnsParam() throws Exception { final String server1 = "127.0.0.1"; final String server2 = "127.0.0.2"; startDefaultMaster(); startDefaultAgent(testHost(), "--dns", server1, "--dns", server2); awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS); final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, asList("cat", "/etc/resolv.conf")); deployJob(jobId, testHost()); final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED); try (final DockerClient dockerClient = getNewDockerClient()) { final LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout(), stderr()); final String log = logs.readFully(); assertThat(log, containsString(server1)); assertThat(log, containsString(server2)); } }
Example #10
Source File: PushPullIT.java From docker-client with Apache License 2.0 | 6 votes |
@Test public void testPushHubPublicImageWithAuth() throws Exception { // Push an image to a public repo on Docker Hub and check it succeeds final String dockerDirectory = Resources.getResource("dockerDirectory").getPath(); final RegistryAuth registryAuth = RegistryAuth.builder() .username(HUB_AUTH_USERNAME) .password(HUB_AUTH_PASSWORD) .build(); final DockerClient client = DefaultDockerClient .fromEnv() .registryAuthSupplier(new FixedRegistryAuthSupplier( registryAuth, RegistryConfigs.create(singletonMap(HUB_NAME, registryAuth)))) .build(); client.build(Paths.get(dockerDirectory), HUB_PUBLIC_IMAGE); client.push(HUB_PUBLIC_IMAGE); }
Example #11
Source File: DockerSecretValidator.java From docker-swarm-elastic-agent-plugin with Apache License 2.0 | 6 votes |
@Override public ValidationResult validate(Map<String, String> elasticProfile) { final ValidationResult validationResult = new ValidationResult(); try { final DockerSecrets dockerSecrets = DockerSecrets.fromString(elasticProfile.get("Secrets")); if (!dockerSecrets.isEmpty()) { DockerClient dockerClient = dockerClientFactory.docker(createAgentRequest.getClusterProfileProperties()); if (!dockerApiVersionAtLeast(dockerClient, "1.26")) { throw new RuntimeException("Docker secret requires api version 1.26 or higher."); } dockerSecrets.toSecretBind(dockerClient.listSecrets()); } } catch (Exception e) { validationResult.addError("Secrets", e.getMessage()); } return validationResult; }
Example #12
Source File: RunnablePipelineContainer.java From repairnator with MIT License | 6 votes |
/** * In case of timeout, we kill the container. * @param remove if true, it will remove both the container and the volumes */ public void killDockerContainer(DockerClient docker, boolean remove) { if (this.containerId == null) { LOGGER.error("Error while trying to kill docker container: the container id is not available. Maybe the container is not started yet."); } else { LOGGER.info("Killing the docker container with id "+containerId+". Forced killing date: "+this.limitDateBeforeKilling); try { docker.killContainer(containerId); if (remove) { docker.removeContainer(containerId); this.removeVolumes(docker); } this.poolManager.removeSubmittedRunnablePipelineContainer(this); serialize("INTERRUPTED"); } catch (DockerException|InterruptedException e) { LOGGER.error("Error while killing docker container "+containerId, e); } } }
Example #13
Source File: NetworkModeTest.java From helios with Apache License 2.0 | 6 votes |
@Test public void test() throws Exception { final CreateJobResponse created = client.createJob(job).get(); assertEquals(CreateJobResponse.Status.OK, created.getStatus()); final JobId jobId = job.getId(); // Wait for agent to come up awaitHostRegistered(client, testHost(), LONG_WAIT_SECONDS, SECONDS); awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS); // Deploy the job on the agent final Deployment deployment = Deployment.of(jobId, START); final JobDeployResponse deployed = client.deploy(deployment, testHost()).get(); assertEquals(JobDeployResponse.Status.OK, deployed.getStatus()); // Wait for the job to run final TaskStatus taskStatus = awaitJobState( client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS); try (final DockerClient docker = getNewDockerClient()) { final HostConfig hostConfig = docker.inspectContainer(taskStatus.getContainerId()).hostConfig(); assertEquals(NETWORK_MODE, hostConfig.networkMode()); } }
Example #14
Source File: DockerHelper.java From repairnator with MIT License | 6 votes |
public static String findDockerImage(String imageName, DockerClient docker) { if (docker == null) { throw new RuntimeException("Docker client has not been initialized. No docker image can be found."); } try { List<Image> allImages = docker.listImages(DockerClient.ListImagesParam.allImages()); String imageId = null; for (Image image : allImages) { if (image.repoTags() != null && image.repoTags().contains(imageName)) { imageId = image.id(); break; } } if (imageId == null) { throw new RuntimeException("There was a problem when looking for the docker image with argument \""+imageName+"\": no image has been found."); } return imageId; } catch (InterruptedException|DockerException e) { throw new RuntimeException("Error while looking for the docker image",e); } }
Example #15
Source File: TagMojoTest.java From docker-maven-plugin with Apache License 2.0 | 6 votes |
public void testTag2() throws Exception { final File pom = getPom("/pom-tag2.xml"); final TagMojo mojo = (TagMojo) lookupMojo("tag", pom); assertNotNull(mojo); final DockerClient docker = mock(DockerClient.class); final ArgumentCaptor<String> image = ArgumentCaptor.forClass(String.class); final ArgumentCaptor<String> name = ArgumentCaptor.forClass(String.class); mojo.execute(docker); verify(docker).tag(image.capture(), name.capture(), eq(false)); assertEquals("wrong image", "imageToTag", image.getValue()); final String[] split = name.getValue().split(":"); assertEquals("wrong name", "newRepo", split[0]); assertTrue(String.format("tag '%s' should be git commit ID at least 7 characters long ", split[1]), split[1].length() >= 7); }
Example #16
Source File: BuildMojoTest.java From docker-maven-plugin with Apache License 2.0 | 6 votes |
public void testBuildWithMultipleCompositePushTag() throws Exception { final File pom = getPom("/pom-build-push-tags-composite.xml"); assertNotNull("Null pom.xml", pom); assertTrue("pom.xml does not exist", pom.exists()); final BuildMojo mojo = setupMojo(pom); final DockerClient docker = mock(DockerClient.class); mojo.execute(docker); verify(docker).build(eq(Paths.get("target/docker")), eq("busybox:compositeTag"), any(AnsiProgressHandler.class)); verify(docker).push(eq("busybox:compositeTag"), any(AnsiProgressHandler.class)); verify(docker).push(eq("busybox:late"), any(AnsiProgressHandler.class)); verify(docker).push(eq("busybox:later"), any(AnsiProgressHandler.class)); verify(docker).push(eq("busybox:latest"), any(AnsiProgressHandler.class)); }
Example #17
Source File: MultiplePortJobTest.java From helios with Apache License 2.0 | 6 votes |
@Test public void testPortEnvVars() throws Exception { startDefaultMaster(); startDefaultAgent(testHost()); awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS); final Map<String, PortMapping> ports = ImmutableMap.of("bar", staticMapping1); try (final DockerClient dockerClient = getNewDockerClient()) { final JobId jobId = createJob(testJobName + 1, testJobVersion, BUSYBOX, asList("sh", "-c", "echo $HELIOS_PORT_bar"), EMPTY_ENV, ports); deployJob(jobId, testHost()); final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED); final String log; try (final LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout(), stderr())) { log = logs.readFully(); } assertEquals(testHost() + ":" + externalPort1, log.trim()); } }
Example #18
Source File: DockerServiceElasticAgent.java From docker-swarm-elastic-agent-plugin with Apache License 2.0 | 5 votes |
public static DockerServiceElasticAgent fromService(Service service, DockerClient client) throws DockerException, InterruptedException { DockerServiceElasticAgent agent = new DockerServiceElasticAgent(); agent.id = service.id(); agent.name = service.spec().name(); agent.createdAt = service.createdAt(); agent.jobIdentifier = JobIdentifier.fromJson(service.spec().labels().get(JOB_IDENTIFIER_LABEL_KEY)); LogStream logStream = client.serviceLogs(service.id(), DockerClient.LogsParam.stdout(), DockerClient.LogsParam.stderr()); agent.logs = logStream.readFully(); logStream.close(); TaskSpec taskSpec = service.spec().taskTemplate(); agent.image = taskSpec.containerSpec().image(); agent.hostname = taskSpec.containerSpec().hostname(); agent.limits = resourceToString(taskSpec.resources().limits()); agent.reservations = resourceToString(taskSpec.resources().reservations()); agent.command = listToString(taskSpec.containerSpec().command()); agent.args = listToString(taskSpec.containerSpec().args()); agent.placementConstraints = listToString(taskSpec.placement().constraints()); agent.environments = toMap(taskSpec); agent.hosts = listToString(taskSpec.containerSpec().hosts()); final List<Task> tasks = client.listTasks(Task.Criteria.builder().serviceName(service.id()).build()); if (!tasks.isEmpty()) { for (Task task : tasks) { agent.tasksStatus.add(new TaskStatus(task)); } } return agent; }
Example #19
Source File: DockerContainers.java From docker-elastic-agents-plugin with Apache License 2.0 | 5 votes |
private List<ContainerStatusReport> getContainerStatus(DockerClient dockerClient) throws Exception { List<ContainerStatusReport> containerStatusReportList = new ArrayList<>(); for (DockerContainer dockerContainer : instances.values()) { containerStatusReportList.add(dockerContainer.getContainerStatusReport(dockerClient)); } return containerStatusReportList; }
Example #20
Source File: DockerSecretValidatorTest.java From docker-swarm-elastic-agent-plugin with Apache License 2.0 | 5 votes |
@Before public void setUp() throws Exception { dockerClientFactory = mock(DockerClientFactory.class); createAgentRequest = mock(CreateAgentRequest.class); ClusterProfileProperties clusterProfileProperties = mock(ClusterProfileProperties.class); dockerClient = mock(DockerClient.class); when(createAgentRequest.getClusterProfileProperties()).thenReturn(clusterProfileProperties); when(dockerClientFactory.docker(clusterProfileProperties)).thenReturn(dockerClient); }
Example #21
Source File: ProfileValidateRequestExecutorTest.java From docker-swarm-elastic-agent-plugin with Apache License 2.0 | 5 votes |
@Before public void setUp() throws Exception { dockerClientFactory = mock(DockerClientFactory.class); dockerClient = mock(DockerClient.class); when(dockerClientFactory.docker(mock(ClusterProfileProperties.class))).thenReturn(dockerClient); }
Example #22
Source File: DockerHelper.java From repairnator with MIT License | 5 votes |
public static DockerClient initDockerClient() { DockerClient docker; try { docker = DefaultDockerClient.fromEnv().build(); } catch (DockerCertificateException e) { throw new RuntimeException("Error while initializing docker client."); } return docker; }
Example #23
Source File: AbstractDockerMojo.java From dockerfile-maven with Apache License 2.0 | 5 votes |
@Nonnull private DockerClient openDockerClient() throws MojoExecutionException { final RegistryAuthSupplier authSupplier = createRegistryAuthSupplier(); try { return DefaultDockerClient.fromEnv() .readTimeoutMillis(readTimeoutMillis) .connectTimeoutMillis(connectTimeoutMillis) .registryAuthSupplier(authSupplier) .useProxy(useProxy) .build(); } catch (DockerCertificateException e) { throw new MojoExecutionException("Could not load Docker certificates", e); } }
Example #24
Source File: TerminationTest.java From helios with Apache License 2.0 | 5 votes |
@Test public void testNoIntOnExit() throws Exception { startDefaultMaster(); final String host = testHost(); startDefaultAgent(host); final HeliosClient client = defaultClient(); awaitHostStatus(client, host, UP, LONG_WAIT_SECONDS, SECONDS); // Note: signal 2 is SIGINT final Job jobToInterrupt = Job.newBuilder() .setName(testJobName) .setVersion(testJobVersion) .setImage(BUSYBOX) .setCommand(asList("/bin/sh", "-c", "trap handle 2; handle() { echo int; exit 0; }; " + "while true; do sleep 1; done")) .build(); final JobId jobId = createJob(jobToInterrupt); deployJob(jobId, host); awaitTaskState(jobId, host, RUNNING); client.setGoal(new Deployment(jobId, Goal.STOP, Deployment.EMTPY_DEPLOYER_USER, Deployment.EMPTY_DEPLOYER_MASTER, Deployment.EMPTY_DEPLOYMENT_GROUP_NAME), host); final TaskStatus taskStatus = awaitTaskState(jobId, host, STOPPED); final String log; try (final DockerClient dockerClient = getNewDockerClient(); LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout())) { log = logs.readFully(); } // No message expected, since SIGINT should not be sent assertEquals("", log); }
Example #25
Source File: PushMojo.java From docker-maven-plugin with Apache License 2.0 | 5 votes |
@Override protected void execute(DockerClient docker) throws MojoExecutionException, DockerException, IOException, InterruptedException { pushImage(docker, imageName, imageTags, getLog(), null, getRetryPushCount(), getRetryPushTimeout(), isSkipDockerPush()); }
Example #26
Source File: SyslogRedirectionTest.java From helios with Apache License 2.0 | 5 votes |
@Before public void setup() throws Exception { try (final DockerClient docker = getNewDockerClient()) { // Build an image with an ENTRYPOINT and CMD prespecified final String dockerDirectory = Resources.getResource("syslog-test-image").getPath(); docker.build(Paths.get(dockerDirectory), testImage); // Figure out the host IP from the container's point of view (needed for syslog) final ContainerConfig config = ContainerConfig.builder() .image(BUSYBOX) .cmd(asList("ip", "route", "show")) .build(); final ContainerCreation creation = docker.createContainer(config); final String containerId = creation.id(); docker.startContainer(containerId); // Wait for the container to exit. // If we don't wait, docker.logs() might return an epmty string because the container // cmd hasn't run yet. docker.waitContainer(containerId); final String log; try (LogStream logs = docker.logs(containerId, stdout(), stderr())) { log = logs.readFully(); } final Matcher m = DEFAULT_GATEWAY_PATTERN.matcher(log); if (m.find()) { syslogHost = m.group("gateway"); } else { fail("couldn't determine the host address from '" + log + "'"); } } }
Example #27
Source File: PushPullIT.java From docker-client with Apache License 2.0 | 5 votes |
@Test public void testBuildHubPrivateImageWithAuth() throws Exception { final String dockerDirectory = Resources.getResource("dockerDirectoryNeedsAuth").getPath(); final RegistryAuth registryAuth = RegistryAuth.builder() .username(HUB_AUTH_USERNAME2) .password(HUB_AUTH_PASSWORD2) .build(); final DockerClient client = DefaultDockerClient .fromEnv() .registryAuthSupplier(new FixedRegistryAuthSupplier( registryAuth, RegistryConfigs.create(singletonMap(HUB_NAME, registryAuth)))) .build(); client.build(Paths.get(dockerDirectory), "testauth", BuildParam.pullNewerImage()); }
Example #28
Source File: DockerContainers.java From docker-elastic-agents-plugin with Apache License 2.0 | 5 votes |
@Override public void refreshAll(ClusterProfileProperties clusterProfileProperties) throws Exception { if (!refreshed) { DockerClient docker = docker(clusterProfileProperties); List<Container> containers = docker.listContainers(DockerClient.ListContainersParam.withLabel(Constants.CREATED_BY_LABEL_KEY, Constants.PLUGIN_ID)); for (Container container : containers) { register(DockerContainer.fromContainerInfo(docker.inspectContainer(container.id()))); } refreshed = true; } }
Example #29
Source File: DockerContainer.java From docker-elastic-agents-plugin with Apache License 2.0 | 5 votes |
public void terminate(DockerClient docker) throws DockerException, InterruptedException { try { LOG.debug("Terminating instance " + this.name()); docker.stopContainer(name, 2); docker.removeContainer(name); } catch (ContainerNotFoundException ignore) { LOG.warn("Cannot terminate a container that does not exist " + name); } }
Example #30
Source File: DockerContainer.java From docker-elastic-agents-plugin with Apache License 2.0 | 5 votes |
private String readLogs(DockerClient dockerClient) { try { return dockerClient.logs(id, DockerClient.LogsParam.stdout(), DockerClient.LogsParam.stderr()).readFully(); } catch (Exception e) { LOG.debug("Could not fetch logs", e); return ""; } }