本文整理匯總了Java中com.spotify.docker.client.DockerClient類的典型用法代碼示例。如果您正苦於以下問題:Java DockerClient類的具體用法?Java DockerClient怎麽用?Java DockerClient使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DockerClient類屬於com.spotify.docker.client包,在下文中一共展示了DockerClient類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: PostgresContainer
import com.spotify.docker.client.DockerClient; //導入依賴的package包/類
public PostgresContainer(DockerClient docker, String host) throws InterruptedException, IOException, ClassNotFoundException, DockerException {
Class.forName("org.postgresql.Driver");
this.docker = docker;
this.host = host;
failsafeDockerPull(docker, GOVUK_POSTGRES_IMAGE);
docker.listImages(DockerClient.ListImagesParam.create("name", GOVUK_POSTGRES_IMAGE));
final HostConfig hostConfig = HostConfig.builder().logConfig(LogConfig.create("json-file")).publishAllPorts(true).build();
ContainerConfig containerConfig = ContainerConfig.builder()
.image(GOVUK_POSTGRES_IMAGE)
.hostConfig(hostConfig)
.env("POSTGRES_USER=" + DB_USERNAME, "POSTGRES_PASSWORD=" + DB_PASSWORD)
.build();
containerId = docker.createContainer(containerConfig).id();
docker.startContainer(containerId);
port = hostPortNumber(docker.inspectContainer(containerId));
registerShutdownHook();
waitForPostgresToStart();
}
示例2: getAllHostContainerId
import com.spotify.docker.client.DockerClient; //導入依賴的package包/類
private static List<String> getAllHostContainerId(){
String cacheKey = "allHostContainerIds";
int cacheTime = 28;//28秒緩存周期,保證一次采集job隻需要訪問一次docker即可
List<String> ids = (List<String>) getCache(cacheKey);
if (ids != null){
return ids;
}else {
ids = new ArrayList<>();
}
synchronized (LockForGetAllHostContainerId){
try {
List<Container> containerList = getContainers(DockerClient.ListContainersParam.allContainers());
for (Container container : containerList) {
ids.add(container.id());
}
} catch (Exception e) {
log.error("",e);
}
}
setCache(cacheKey,ids,cacheTime);
return ids;
}
示例3: followLogs
import com.spotify.docker.client.DockerClient; //導入依賴的package包/類
@Override
public void followLogs(final String containerId, final Logger logger) {
logger.info("********** Container logs **********");
try {
final LogStream logs = this.client.logs(
containerId,
DockerClient.LogsParam.stdout(),
DockerClient.LogsParam.stderr(),
DockerClient.LogsParam.follow()
);
logger.info(logs.readFully());
} catch (final DockerException | InterruptedException ex) {
throw new IllegalStateException(
"Exception when starting container with id " + containerId, ex
);
}
logger.info("************************************");
}
示例4: proceess
import com.spotify.docker.client.DockerClient; //導入依賴的package包/類
@Override
public ProcessorResult proceess(Image image, RuntimeConfiguration configuration) {
if(configuration.getUsecache() && configuration.getCacheImage() != null){
DockerRemoteHandler dockerRemoteHandler = new DockerRemoteHandler();
Registry registry = configuration.getCacheImage().getRegistry();
final DockerClient parentDockerClient = dockerRemoteHandler.buildDockerClient(registry, configuration.getDockerHost());
Result result = dockerRemoteHandler.pull(parentDockerClient, configuration.getCacheImage());
if(result.getStatus()){
return new ProcessorResult(image,configuration).success();
}else{
logger.info("pull cache image failed, continue without cache image");
return new ProcessorResult(image,configuration).success();
}
}else{
return new ProcessorResult(image,configuration).success();
}
}
示例5: predownloadImagesIfRequired
import com.spotify.docker.client.DockerClient; //導入依賴的package包/類
private static void predownloadImagesIfRequired() throws DockerException, InterruptedException {
DockerClient client = getClient();
LOG.warning("Commencing download of images.");
Collection<MappingInfo> images = getInstance().getMapping().values();
ProgressHandler handler = new LoggingBuildHandler();
for (MappingInfo image : images) {
List<Image> foundImages = client.listImages(DockerClient.ListImagesParam.byName(image.getTarget()));
if (! foundImages.isEmpty()) {
LOG.warning(String.format("Skipping download for Image [%s] because it's already available.",
image.getTarget()));
continue;
}
client.pull(image.getTarget(), handler);
}
}
示例6: shouldStartContainerWithHostEntry
import com.spotify.docker.client.DockerClient; //導入依賴的package包/類
@Test
public void shouldStartContainerWithHostEntry() throws Exception {
Map<String, String> properties = new HashMap<>();
properties.put("Image", "alpine:latest");
properties.put("Hosts", "127.0.0.2\tbaz \n192.168.5.1\tfoo\tbar\n127.0.0.1 gocd.local ");
properties.put("Command", "cat\n/etc/hosts");
DockerContainer container = DockerContainer.create(new CreateAgentRequest("key", properties, "prod"), createSettings(), docker);
containers.add(container.name());
ContainerInfo containerInfo = docker.inspectContainer(container.name());
final ImmutableList<String> extraHosts = containerInfo.hostConfig().extraHosts();
assertThat(extraHosts, containsInAnyOrder(
"baz:127.0.0.2", "foo\tbar:192.168.5.1", "gocd.local:127.0.0.1"
));
String logs = docker.logs(container.name(), DockerClient.LogsParam.stdout()).readFully();
assertThat(logs, containsString("127.0.0.2\tbaz"));
assertThat(logs, containsString("192.168.5.1\tfoo"));
assertThat(logs, containsString("127.0.0.1\tgocd.local"));
}
示例7: removeParentAndChildren
import com.spotify.docker.client.DockerClient; //導入依賴的package包/類
@Override
public void removeParentAndChildren(String parentId) {
// stop parent
removeContainer(parentId);
// find children
try {
List<Container> containers = dockerClient.listContainers(DockerClient.ListContainersParam.allContainers());
for (Container c : containers) {
if (c != null && c.labels().get(LABEL_PARENT) != null
&& c.labels().get(LABEL_PARENT).equals(parentId)) {
removeParentAndChildren(c.id());
}
}
} catch (Exception e) {
LOGGER.error("Error while removing containers: " + e.toString());
}
}
示例8: stop
import com.spotify.docker.client.DockerClient; //導入依賴的package包/類
public void stop() {
if (stopped) {
return;
}
try {
stopped = true;
System.out.println("Killing postgres container with ID: " + containerId);
LogStream logs = docker.logs(containerId, DockerClient.LogsParam.stdout(), DockerClient.LogsParam.stderr());
System.out.println("Killed container logs:\n");
logs.attach(System.out, System.err);
docker.stopContainer(containerId, 5);
docker.removeContainer(containerId);
} catch (DockerException | InterruptedException | IOException e) {
System.err.println("Could not shutdown " + containerId);
e.printStackTrace();
}
}
示例9: validate
import com.spotify.docker.client.DockerClient; //導入依賴的package包/類
@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(pluginRequest.getPluginSettings());
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;
}
示例10: validate
import com.spotify.docker.client.DockerClient; //導入依賴的package包/類
@Override
public ValidationResult validate(Map<String, String> elasticProfile) {
final ValidationResult validationResult = new ValidationResult();
try {
final DockerMounts dockerMounts = DockerMounts.fromString(elasticProfile.get("Mounts"));
if (!dockerMounts.isEmpty()) {
DockerClient dockerClient = dockerClientFactory.docker(pluginRequest.getPluginSettings());
if (!dockerApiVersionAtLeast(dockerClient, "1.26")) {
throw new RuntimeException("Docker volume mount requires api version 1.26 or higher.");
}
dockerMounts.toMount(dockerClient.listVolumes().volumes());
}
} catch (Exception e) {
validationResult.addError("Mounts", e.getMessage());
}
return validationResult;
}
示例11: shouldCreateSwarmClusterObject
import com.spotify.docker.client.DockerClient; //導入依賴的package包/類
@Test
public void shouldCreateSwarmClusterObject() throws Exception {
final DockerClient dockerClient = mock(DockerClient.class);
final Node node = mockNode("node-id", "manager", true);
final List<Node> nodeList = Arrays.asList(node);
final List<Task> taskList = Arrays.asList(mockTask(node.id()));
when(dockerClient.listNodes()).thenReturn(nodeList);
when(dockerClient.listTasks()).thenReturn(taskList);
final SwarmCluster swarmCluster = new SwarmCluster(dockerClient);
verify(dockerClient, times(1)).listNodes();
verify(dockerClient, times(1)).listTasks();
assertThat(swarmCluster.getNodes(), hasSize(1));
assertThat(swarmCluster.getNodes().get(0).getTasks(), hasSize(1));
}
示例12: createContainer
import com.spotify.docker.client.DockerClient; //導入依賴的package包/類
private ContainerCreation createContainer(DockerClient docker, int postgresPort, int exposedPort) throws Exception {
Map<String, List<PortBinding>> portBindings = new HashMap<>();
portBindings.put(String.valueOf(postgresPort + "/tcp"), Arrays.asList(PortBinding.of("0.0.0.0", exposedPort)));
HostConfig hostConfig = HostConfig.builder()
.portBindings(portBindings)
.build();
ContainerConfig containerConfig = ContainerConfig.builder()
.hostConfig(hostConfig)
.exposedPorts(postgresPort + "/tcp")
.image(imageConfig.getContainerImage())
.networkDisabled(false)
.build();
return docker.createContainer(containerConfig);
}
示例13: pull
import com.spotify.docker.client.DockerClient; //導入依賴的package包/類
@Override
public Observable<Instance> pull(Trace trace, Instance image) {
return Observable.create((Subscriber<? super Instance> subscriber) -> {
Trace.Event pulling = trace.start("RxDocker: pull(image)", image);
final DockerClient client;
try {
client = obtainDockerClient();
client.pull(image.toString(), message -> logPullProgress(trace, message));
} catch(Exception ex) {
trace.event(Level.ERROR, "RxDocker: pulling failed", ex);
pulling.end();
if (subscriber.isUnsubscribed()) return;
subscriber.onError(ex);
return;
}
pulling.end();
if (subscriber.isUnsubscribed()) return;
subscriber.onNext(image);
subscriber.onCompleted();
}).subscribeOn(scheduler);
}
示例14: startContainer
import com.spotify.docker.client.DockerClient; //導入依賴的package包/類
@Override
public Observable<String> startContainer(Trace trace, String containerId) {
return Observable.create((Subscriber<? super String> subscriber) -> {
Event starting = trace.start("RxDocker: starting container (containerId): ", containerId);
final DockerClient client;
try {
client = obtainDockerClient();
client.startContainer(containerId);
} catch(Exception ex) {
trace.event(Level.ERROR, "RxDocker: starting failed", ex);
starting.end();
if (subscriber.isUnsubscribed()) return;
subscriber.onError(ex);
return;
}
starting.end();
if (subscriber.isUnsubscribed()) return;
subscriber.onNext(containerId);
subscriber.onCompleted();
}).subscribeOn(scheduler);
}
示例15: killContainer
import com.spotify.docker.client.DockerClient; //導入依賴的package包/類
@Override
public Observable<String> killContainer(Trace trace, String containerId) {
return Observable.create((Subscriber<? super String> subscriber) -> {
Event killing = trace.start("RxDocker: killing container: ", containerId);
final DockerClient client;
try {
client = obtainDockerClient();
client.killContainer(containerId);
} catch(Exception ex) {
trace.event("RxDocker: killing failed", ex);
killing.end();
if (subscriber.isUnsubscribed()) return;
subscriber.onError(ex);
return;
}
killing.end();
if (subscriber.isUnsubscribed()) return;
subscriber.onNext(containerId);
subscriber.onCompleted();
}).subscribeOn(scheduler);
}