本文整理汇总了Java中com.spotify.docker.client.messages.ContainerInfo类的典型用法代码示例。如果您正苦于以下问题:Java ContainerInfo类的具体用法?Java ContainerInfo怎么用?Java ContainerInfo使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ContainerInfo类属于com.spotify.docker.client.messages包,在下文中一共展示了ContainerInfo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isHostNet
import com.spotify.docker.client.messages.ContainerInfo; //导入依赖的package包/类
/**
* 容器网络模式是否为host模式
* @param containerId
* @return
*/
public static boolean isHostNet(String containerId){
String cacheKey = "isHostNet" + containerId;
Boolean v = (Boolean) getCache(cacheKey);
if (v != null){
return v;
}
Boolean value = false;
try {
ContainerInfo containerInfo = getContainerInfo(containerId);
if (containerInfo != null) {
ImmutableMap<String, AttachedNetwork> networks = containerInfo.networkSettings().networks();
if (networks != null && !networks.isEmpty()){
value = networks.get("host") != null && StringUtils.isNotEmpty(networks.get("host").ipAddress());
setCache(cacheKey,value);
}else {
log.warn("容器{}无Networks配置",containerInfo.name());
}
}
} catch (Exception e) {
log.error("",e);
}
return value;
}
示例2: getAccessToPort
import com.spotify.docker.client.messages.ContainerInfo; //导入依赖的package包/类
/**
* Obtains the access information for the requested containerPort
* @param containerPort the port to locate according to the <code>EXPOSE</code>d/internal container port
* @return a socket address populated with external host and port of the container
* @throws DockerException if an issue occurred containing the Docker daemon
* @throws InterruptedException if interrupted while contacting the Docker daemon
*/
public InetSocketAddress getAccessToPort(int containerPort) throws DockerException, InterruptedException {
ContainerInfo containerInfo = dockerClient.inspectContainer(container.id());
NetworkSettings networkSettings = containerInfo.networkSettings();
List<PortBinding> portBindings = networkSettings.ports().get(String.format("%d/tcp", containerPort));
if (portBindings != null && !portBindings.isEmpty()) {
PortBinding portBinding = portBindings.get(0);
return new InetSocketAddress(dockerClient.getHost(), Integer.parseInt(portBinding.hostPort()));
}
else {
return null;
}
}
示例3: unregisteredAfterTimeout
import com.spotify.docker.client.messages.ContainerInfo; //导入依赖的package包/类
private DockerContainers unregisteredAfterTimeout(PluginSettings settings, Agents knownAgents) throws Exception {
Period period = settings.getAutoRegisterPeriod();
DockerContainers unregisteredContainers = new DockerContainers();
for (String containerName : instances.keySet()) {
if (knownAgents.containsAgentWithId(containerName)) {
continue;
}
ContainerInfo containerInfo;
try {
containerInfo = docker(settings).inspectContainer(containerName);
} catch (ContainerNotFoundException e) {
LOG.warn("The container " + containerName + " could not be found.");
continue;
}
DateTime dateTimeCreated = new DateTime(containerInfo.created());
if (clock.now().isAfter(dateTimeCreated.plus(period))) {
unregisteredContainers.register(DockerContainer.fromContainerInfo(containerInfo));
}
}
return unregisteredContainers;
}
示例4: shouldStartContainerWithCorrectEnvironment
import com.spotify.docker.client.messages.ContainerInfo; //导入依赖的package包/类
@Test
public void shouldStartContainerWithCorrectEnvironment() throws Exception {
Map<String, String> properties = new HashMap<>();
properties.put("Image", "busybox:latest");
properties.put("Environment", "A=B\nC=D\r\nE=F\n\n\nX=Y");
PluginSettings settings = createSettings();
settings.setEnvironmentVariables("GLOBAL=something");
DockerContainer container = DockerContainer.create(new CreateAgentRequest("key", properties, "prod"), settings, docker);
containers.add(container.name());
ContainerInfo containerInfo = docker.inspectContainer(container.name());
assertThat(containerInfo.config().env(), hasItems("A=B", "C=D", "E=F", "X=Y", "GLOBAL=something"));
DockerContainer dockerContainer = DockerContainer.fromContainerInfo(containerInfo);
assertThat(dockerContainer.properties(), is(properties));
}
示例5: shouldStartContainerWithHostEntry
import com.spotify.docker.client.messages.ContainerInfo; //导入依赖的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"));
}
示例6: startContainer
import com.spotify.docker.client.messages.ContainerInfo; //导入依赖的package包/类
@Test
public void startContainer() throws Exception {
String parentId = manager.startContainer(busyboxImageName, Constants.CONTAINER_TYPE_SYSTEM, null,
sleepCommand);
assertNotNull(parentId);
containers.add(parentId);
String containerId = manager.startContainer(busyboxImageName, Constants.CONTAINER_TYPE_SYSTEM, parentId,
sleepCommand);
assertNotNull(containerId);
containers.add(containerId);
ContainerInfo containerInfo = dockerClient.inspectContainer(containerId);
assertEquals(containerInfo.id(), containerId);
assertTrue(containerInfo.state().running());
assertEquals(containerInfo.config().labels().get(ContainerManagerImpl.LABEL_TYPE),
Constants.CONTAINER_TYPE_SYSTEM);
assertEquals(containerInfo.config().labels().get(ContainerManagerImpl.LABEL_PARENT), parentId);
assertTrue(Arrays.equals(containerInfo.config().cmd().toArray(), sleepCommand));
}
示例7: removeContainer
import com.spotify.docker.client.messages.ContainerInfo; //导入依赖的package包/类
@Test
public void removeContainer() throws Exception {
// start new test container
String testContainer = manager.startContainer(busyboxImageName, Constants.CONTAINER_TYPE_SYSTEM, null, sleepCommand);
assertNotNull(testContainer);
containers.add(testContainer);
// stop it immediately
manager.stopContainer(testContainer);
// remove it
manager.removeContainer(testContainer);
try {
// try to get info on removed container
ContainerInfo containerInfo = dockerClient.inspectContainer(testContainer);
containerInfo.state().running();
// we expected an exception
fail();
} catch (Exception e) {
assertNotNull(e);
}
}
示例8: shouldLabelContainerSingleLabel
import com.spotify.docker.client.messages.ContainerInfo; //导入依赖的package包/类
@Test
public void shouldLabelContainerSingleLabel() throws Throwable {
DockerRule testee = DockerRule.builder()//
.imageName("busybox:1.25.1")//
.cmd("sh", "-c", "echo 01stdout; sleep 100")//
.addLabel("label-key", "label-value")
.build();
testee.before();
try {
ContainerInfo containerInfo = testee.getDockerClient().inspectContainer(testee.getContainerId());
Map<String, String> labels = containerInfo.config().labels();
assertTrue(labels.containsKey("label-key"));
assertEquals("label-value", labels.get("label-key"));
} finally {
testee.after();
}
}
示例9: get
import com.spotify.docker.client.messages.ContainerInfo; //导入依赖的package包/类
/**
* Obtains the details of a running container.
*
* @param containerId the Docker container ID
* @param authUsername if null, only accesses public containers, otherwise this is used to narrow
* access to only those containers owned by this given user
* @return the details of the container
* @throws DockerException
* @throws InterruptedException
*/
public ContainerDetails get(String containerId, String authUsername) throws DockerException, InterruptedException {
return proxy.access(dockerClient -> {
final ContainerInfo containerInfo = dockerClient.inspectContainer(containerId);
if (isOurs(containerInfo) && canRead(containerInfo, authUsername)) {
final ContainerDetails containerDetails = new ContainerDetails(containerInfo);
final ContainerSummary summary = ContainerSummary.from(containerInfo,
getDockerHostIp(), mccySettings.getConnectUsingHost());
metadataConversionService.fillFromEnv(containerInfo.config().env(), summary);
containerDetails.setSummary(summary);
return containerDetails;
} else {
return null;
}
});
}
示例10: delete
import com.spotify.docker.client.messages.ContainerInfo; //导入依赖的package包/类
public void delete(String containerId) throws DockerException, InterruptedException {
proxy.access(dockerClient -> {
final ContainerInfo containerInfo = dockerClient.inspectContainer(containerId);
if (!isOurs(containerInfo)) {
throw new IllegalArgumentException("The given container is not managed by us");
}
if (containerInfo.state().running()) {
dockerClient.stopContainer(containerId, mccySettings.getSecondsToWaitOnStop());
}
dockerClient.removeContainer(containerId);
return null;
});
}
示例11: start
import com.spotify.docker.client.messages.ContainerInfo; //导入依赖的package包/类
public void start(String containerId) throws DockerException, InterruptedException {
proxy.access(dockerClient -> {
final ContainerInfo containerInfo = dockerClient.inspectContainer(containerId);
if (isOurs(containerInfo)) {
if (containerInfo.state().running()) {
throw new IllegalArgumentException("Container is already running");
}
dockerClient.startContainer(containerId);
} else {
throw new IllegalArgumentException("The given container is not managed by us");
}
return null;
});
}
示例12: stop
import com.spotify.docker.client.messages.ContainerInfo; //导入依赖的package包/类
public void stop(String containerId) throws DockerException, InterruptedException {
proxy.access(dockerClient -> {
final ContainerInfo containerInfo = dockerClient.inspectContainer(containerId);
if (isOurs(containerInfo)) {
if (!containerInfo.state().running()) {
throw new IllegalArgumentException("Container was not running");
}
dockerClient.stopContainer(containerId, mccySettings.getSecondsToWaitOnStop());
} else {
throw new IllegalArgumentException("The given container is not managed by us");
}
return null;
});
}
示例13: createContainerInfo
import com.spotify.docker.client.messages.ContainerInfo; //导入依赖的package包/类
private ContainerInfo createContainerInfo(String isPublic, String owner) {
final Map<String, String> labels = new HashMap<>();
labels.put(MccyConstants.MCCY_LABEL, "true");
labels.put(MccyConstants.MCCY_LABEL_PUBLIC, isPublic);
labels.put(MccyConstants.MCCY_LABEL_OWNER, owner);
labels.put(MccyConstants.MCCY_LABEL_NAME, "container_name");
ContainerInfo containerInfo = new ContainerInfo();
setField(containerInfo, "config",
ContainerConfig.builder()
.labels(labels)
.build());
setField(containerInfo, "networkSettings",
NetworkSettings.builder()
.build());
setField(containerInfo, "state",
new ContainerState());
setField(containerInfo, "name", "container_name");
return containerInfo;
}
示例14: stop
import com.spotify.docker.client.messages.ContainerInfo; //导入依赖的package包/类
public void stop() throws Exception {
if (jettyServer != null) {
jettyServer.stop();
}
if (dockerClient != null) {
ContainerInfo containerInfo = dockerClient.inspectContainer(containerId);
Set<String> volumeIds = containerInfo.mounts().stream()
.filter(containerMount -> "volume".equals(containerMount.type()))
.map(ContainerMount::name)
.collect(Collectors.toSet());
dockerClient.stopContainer(containerId, 5);
dockerClient.removeContainer(containerId);
for(String volumeId : volumeIds) {
dockerClient.removeVolume(volumeId);
}
}
}
示例15: randomlyPickOneMatchingElement
import com.spotify.docker.client.messages.ContainerInfo; //导入依赖的package包/类
public Optional<Container> randomlyPickOneMatchingElement(final DefaultDockerClient dockerClient, final String key, final String value) throws DockerException, InterruptedException {
final List<Container> containers = dockerClient.listContainers();
final List<Container> collect = new ArrayList<>();
for (Container container : containers) {
final ContainerInfo containerInfo = dockerClient.inspectContainer(container.id());
boolean res;
res = containsKeyAndValue(containerInfo.config().env(), key, value);
if (res) {
collect.add(container);
}
}
final Optional<Container> ret;
if (collect.isEmpty()) {
ret = Optional.empty();
} else {
ret = Optional.of(collect.get(random.nextInt(collect.size())));
}
return ret;
}