本文整理汇总了Java中com.github.dockerjava.api.model.Container类的典型用法代码示例。如果您正苦于以下问题:Java Container类的具体用法?Java Container怎么用?Java Container使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Container类属于com.github.dockerjava.api.model包,在下文中一共展示了Container类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: containerNames
import com.github.dockerjava.api.model.Container; //导入依赖的package包/类
public List<String> containerNames ( boolean showAll ) {
List<String> names = new ArrayList<>();
try {
List<Container> containers = dockerClient.listContainersCmd().withShowAll( showAll ).exec();
containers.forEach( container -> {
logger.debug( "container: \n {}", container.toString() );
String label = Arrays.asList( container.getNames() ).toString();
if ( container.getNames().length == 1 ) {
label = container.getNames()[0];
}
names.add( label );
} );
;
} catch (Exception e) {
logger.warn( "Failed connecting to docker: {}", CSAP.getCsapFilteredStackTrace( e ) );
names.add( "Unable to get listing" );
}
return names;
}
示例2: verify_containers_created_date
import com.github.dockerjava.api.model.Container; //导入依赖的package包/类
@Test
public void verify_containers_created_date ()
throws Exception {
Optional<Container> matchContainer = findContainerByName( "/peter" );
if ( matchContainer.isPresent() ) {
Container c = matchContainer.get();
LocalDateTime date = LocalDateTime.ofInstant( Instant.ofEpochMilli( c.getCreated() * 1000 ), ZoneId.systemDefault() );
logger.info( "name: {}, created: {} , text: {}",
Arrays.asList( c.getNames() ), c.getCreated(),
date.format( DateTimeFormatter.ofPattern( "HH:mm:ss MMM d, yyyy" ) ) );
}
}
示例3: DockerService
import com.github.dockerjava.api.model.Container; //导入依赖的package包/类
public DockerService(Container container, String host) {
this.host = host;
containerId = container.getId();
containerNames = Arrays.stream(container.getNames()).collect(Collectors.toList());
if (!containerNames.isEmpty()) {
name = containerNames.get(0);
} else {
name = containerId;
}
for (ContainerPort port : container.getPorts()) {
Record record = createRecord(container, port);
if (record != null) {
records.add(record);
}
}
}
示例4: isNotRunning
import com.github.dockerjava.api.model.Container; //导入依赖的package包/类
private boolean isNotRunning(String containerId, List<Container> running) {
if (running == null) {
// No running container
return true;
}
for (Container container : running) {
if (container.getId().equalsIgnoreCase(containerId)) {
// Found in the running list
return false;
}
}
// Not found in the running list
return true;
}
示例5: setUp
import com.github.dockerjava.api.model.Container; //导入依赖的package包/类
@Before
public void setUp() {
init();
client = DockerClientBuilder.getInstance().build();
List<Container> running = client.listContainersCmd().withStatusFilter("running").exec();
if (running != null) {
running.forEach(container -> client.stopContainerCmd(container.getId()).exec());
}
vertx = Vertx.vertx();
discovery = ServiceDiscovery.create(vertx);
bridge = new DockerServiceImporter();
discovery.registerServiceImporter(bridge,
new JsonObject().put("scan-period", -1));
await().until(() -> bridge.started);
}
示例6: containerInfo
import com.github.dockerjava.api.model.Container; //导入依赖的package包/类
/**
* Print running containers, with an asterisk (*) when a particular name is matched
*/
public static void containerInfo(@Nullable String nameToHighlight){
Info info = DockerClientFacade.getClient().infoCmd().exec();
int runningContainers = info.getContainersRunning();
logger.info(String.format("Running containers: %d", runningContainers));
for (Container container : dockerClient.listContainersCmd().exec()){
String normalizedName = container.getNames()[0].substring(1);
String shortHash = container.getId().substring(0,12);
if (nameToHighlight != null && normalizedName.equals(nameToHighlight)){
logger.info(String.format("* Running containers: %s, id: %s", normalizedName, shortHash));
}
else{
logger.info(String.format("Running containers: %s, id: %s", normalizedName, shortHash));
}
}
}
示例7: removeContainer
import com.github.dockerjava.api.model.Container; //导入依赖的package包/类
public static void removeContainer(@Nonnull String containerName){
List<Container> containers = dockerClient
.listContainersCmd()
.withLabelFilter(label(containerName))
.withShowAll(true)
.exec();
if (containers.isEmpty()){
logger.info(String.format("Found zero containers named '%s'.", containerName));
}
else {
for (int i = 0; i < containers.size(); i++){
String id = containers.get(i).getId();
dockerClient.stopContainerCmd(id).exec();
dockerClient.removeContainerCmd(id).exec();
}
logger.info(String.format("Removed %s container(s) named '%s'",
containers.size(),
containerName
));
}
}
示例8: getUnusedDockerImages
import com.github.dockerjava.api.model.Container; //导入依赖的package包/类
/**
* Generates lists of images that are safe to delete, in the order that is safe to delete them (children before
* parents). The function starts with the set of all local images and then filters out images that are used now
* and images that have been used recently (because they might be re-used again in near future).
*
* @param images List of all the local images
* @param containers List of all the containers, including the ones that are stopped
* @return List of image tags of unused images, if unused image has no tag, will return image ID instead.
*/
public List<DockerImage> getUnusedDockerImages(List<Image> images, List<Container> containers) {
Map<String, Image> dockerImageByImageId = images.stream().collect(Collectors.toMap(Image::getId, img -> img));
Map<String, Image> unusedImagesByContainers = filterOutImagesUsedByContainers(dockerImageByImageId, containers);
Map<String, Image> unusedImagesByRecent = filterOutRecentImages(unusedImagesByContainers);
return unusedImagesByRecent.keySet().stream()
.sorted((o1, o2) -> {
// If image2 is parent of image1, image1 comes before image2
if (imageIsDescendantOf(unusedImagesByRecent, o1, o2)) return -1;
// If image1 is parent of image2, image2 comes before image1
else if (imageIsDescendantOf(unusedImagesByRecent, o2, o1)) return 1;
// Otherwise, sort lexicographically by image name (For testing)
else return o1.compareTo(o2);
})
.flatMap(imageId -> {
// Deleting an image by image ID with multiple tags will fail -> map IDs to all the tags refering to the ID
String[] repoTags = unusedImagesByRecent.get(imageId).getRepoTags();
return (repoTags == null) ? Stream.of(imageId) : Stream.of(repoTags);
})
.map(DockerImage::new)
.collect(Collectors.toList());
}
示例9: filterOutImagesUsedByContainers
import com.github.dockerjava.api.model.Container; //导入依赖的package包/类
private Map<String, Image> filterOutImagesUsedByContainers(
Map<String, Image> dockerImagesByImageId, List<com.github.dockerjava.api.model.Container> containerList) {
Map<String, Image> filteredDockerImagesByImageId = new HashMap<>(dockerImagesByImageId);
for (com.github.dockerjava.api.model.Container container : containerList) {
String imageToSpare = container.getImageId();
do {
// May be null if two images have have the same parent, the first image will remove the parent, the
// second will get null.
Image sparedImage = filteredDockerImagesByImageId.remove(imageToSpare);
imageToSpare = sparedImage == null ? "" : sparedImage.getParentId();
} while (!imageToSpare.isEmpty());
}
return filteredDockerImagesByImageId;
}
示例10: filterByName
import com.github.dockerjava.api.model.Container; //导入依赖的package包/类
/**
* Filters the set based on the constainer name
* @param pattern regular expression pattern of the container name
* @return filtered set
*/
public DockerContainersUtil filterByName( String pattern ) {
if( this.containers == null ) {
return this;
}
Set<Container> matched = new HashSet<>();
for (Container container : containers) {
String[] names = container.getNames();
for (String name : names) {
// all names start with '/'
if (name.substring(1).matches(pattern)) {
matched.add(container);
}
}
}
return new DockerContainersUtil(dockerClient, matched);
}
示例11: filterByImage
import com.github.dockerjava.api.model.Container; //导入依赖的package包/类
/**
* Filters the set based on the constainer name
* @param pattern regular expression pattern of the container name
* @return filtered set
*/
public DockerContainersUtil filterByImage( String pattern ) {
if( this.containers == null ) {
return this;
}
Set<Container> matched = new HashSet<>();
for (Container container : containers) {
if( container.getImage().matches(pattern) ) {
matched.add( container );
}
}
return new DockerContainersUtil(dockerClient, matched);
}
示例12: filterByName
import com.github.dockerjava.api.model.Container; //导入依赖的package包/类
/**
* Filters the set based on the container name
*
* @param pattern regular expression pattern of the container name
* @return filtered set
*/
public DockerContainersUtil filterByName(String pattern) {
if (this.containers == null) {
return this;
}
List<Container> matched = new ArrayList<>();
for (Container container : containers) {
String[] names = container.getNames();
for (String name : names) {
// all names start with '/'
if (name.substring(1).matches(pattern)) {
matched.add(container);
}
}
}
return new DockerContainersUtil(matched);
}
示例13: registerContainersForShutdown
import com.github.dockerjava.api.model.Container; //导入依赖的package包/类
private void registerContainersForShutdown() {
// Ensure that all service containers that were launched by compose will be killed at shutdown
try {
final List<Container> containers = listChildContainers();
// register with ResourceReaper to ensure final shutdown with JVM
containers.forEach(container ->
ResourceReaper.instance().registerContainerForCleanup(container.getId(), container.getNames()[0]));
// Compose can define their own networks as well; ensure these are cleaned up
dockerClient.listNetworksCmd().exec().forEach(network -> {
if (network.getName().contains(identifier)) {
spawnedNetworkIds.add(network.getId());
ResourceReaper.instance().registerNetworkIdForCleanup(network.getId());
}
});
// remember the IDs to allow containers to be killed as soon as we reach stop()
spawnedContainerIds.addAll(containers.stream()
.map(Container::getId)
.collect(Collectors.toSet()));
} catch (DockerException e) {
logger().debug("Failed to stop a service container with exception", e);
}
}
示例14: shouldKillContainers
import com.github.dockerjava.api.model.Container; //导入依赖的package包/类
@Test
public void shouldKillContainers() throws Exception {
containerChaos
.onCubeDockerHost()
.killRandomly(
ContainerChaos.ContainersType.regularExpression("^pingpong"),
ContainerChaos.IntervalType.intervalInSeconds(4),
ContainerChaos.KillSignal.SIGTERM
)
.exec();
TimeUnit.SECONDS.sleep(12);
final List<Container> containers = dockerClient.listContainersCmd().exec();
//Pumba container is not killed by itself
assertThat(containers).hasSize(1);
}
示例15: LogstashExecutorContainer
import com.github.dockerjava.api.model.Container; //导入依赖的package包/类
public LogstashExecutorContainer(DockerClient dockerClient) {
this.dockerClient = dockerClient;
await().atMost(60, TimeUnit.SECONDS).until(
() -> dockerClient.listContainersCmd().exec().size() > 0);
List<Container> containers = dockerClient.listContainersCmd().exec();
String id = null;
// Find a single executor container
for (Container container : containers) {
if (container.getImage().contains("executor")) {
id = container.getId();
break;
}
}
executorId = id;
}