use of io.fabric8.maven.docker.access.ContainerCreateConfig in project docker-maven-plugin by fabric8io.
the class RunService method createContainerConfig.
// visible for testing
ContainerCreateConfig createContainerConfig(String imageName, RunImageConfiguration runConfig, PortMapping mappedPorts, GavLabel gavLabel, Properties mavenProps, File baseDir) throws DockerAccessException {
try {
ContainerCreateConfig config = new ContainerCreateConfig(imageName).hostname(runConfig.getHostname()).domainname(runConfig.getDomainname()).user(runConfig.getUser()).workingDir(runConfig.getWorkingDir()).entrypoint(runConfig.getEntrypoint()).exposedPorts(mappedPorts.getContainerPorts()).environment(runConfig.getEnvPropertyFile(), runConfig.getEnv(), mavenProps).labels(mergeLabels(runConfig.getLabels(), gavLabel)).command(runConfig.getCmd()).hostConfig(createContainerHostConfig(runConfig, mappedPorts, baseDir));
RunVolumeConfiguration volumeConfig = runConfig.getVolumeConfiguration();
if (volumeConfig != null) {
resolveRelativeVolumeBindings(baseDir, volumeConfig);
config.binds(volumeConfig.getBind());
}
NetworkConfig networkConfig = runConfig.getNetworkingConfig();
if (networkConfig.isCustomNetwork() && networkConfig.hasAliases()) {
ContainerNetworkingConfig networkingConfig = new ContainerNetworkingConfig().aliases(networkConfig);
config.networkingConfig(networkingConfig);
}
return config;
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("Failed to create contained configuration for [%s]", imageName), e);
}
}
use of io.fabric8.maven.docker.access.ContainerCreateConfig in project docker-maven-plugin by fabric8io.
the class RunService method createContainer.
/**
* Create a container with the given image configuration.
*
* @param imageConfig image configuration holding the run information and the image name
* @param portMapping container port mapping
* @param gavLabel label to tag the started container with
* @param properties properties to fill in with dynamically assigned ports
* @param defaultContainerNamePattern pattern to use for naming containers. Can be null in which case a default pattern is used
* @param buildTimestamp date which should be used as the timestamp when calculating container names
* @return the container id
*
* @throws DockerAccessException if access to the docker backend fails
*/
public String createContainer(ImageConfiguration imageConfig, PortMapping portMapping, GavLabel gavLabel, Properties properties, File baseDir, String defaultContainerNamePattern, Date buildTimestamp) throws DockerAccessException {
RunImageConfiguration runConfig = imageConfig.getRunConfiguration();
String imageName = imageConfig.getName();
Collection<Container> existingContainers = queryService.getContainersForImage(imageName, true);
String containerName = ContainerNamingUtil.formatContainerName(imageConfig, defaultContainerNamePattern, buildTimestamp, existingContainers);
ContainerCreateConfig config = createContainerConfig(imageName, runConfig, portMapping, gavLabel, properties, baseDir);
return docker.createContainer(config, containerName);
}
use of io.fabric8.maven.docker.access.ContainerCreateConfig in project docker-maven-plugin by fabric8io.
the class DockerAccessWinIT method testCreateContainer.
private void testCreateContainer() throws DockerAccessException {
PortMapping portMapping = new PortMapping(Arrays.asList(new String[] { PORT + ":" + PORT }), new Properties());
ContainerHostConfig hostConfig = new ContainerHostConfig().portBindings(portMapping);
ContainerCreateConfig createConfig = new ContainerCreateConfig(IMAGE).command(new Arguments("ping google.com")).hostConfig(hostConfig);
containerId = dockerClient.createContainer(createConfig, CONTAINER_NAME);
assertNotNull(containerId);
String name = dockerClient.getContainer(containerId).getName();
assertEquals(CONTAINER_NAME, name);
}
use of io.fabric8.maven.docker.access.ContainerCreateConfig in project docker-maven-plugin by fabric8io.
the class DockerAccessIT method testCreateContainer.
private void testCreateContainer() throws DockerAccessException {
PortMapping portMapping = new PortMapping(Arrays.asList(new String[] { PORT + ":" + PORT }), new Properties());
ContainerHostConfig hostConfig = new ContainerHostConfig().portBindings(portMapping);
ContainerCreateConfig createConfig = new ContainerCreateConfig(IMAGE).command(new Arguments("ping google.com")).hostConfig(hostConfig);
containerId = dockerClient.createContainer(createConfig, CONTAINER_NAME);
assertNotNull(containerId);
String name = dockerClient.getContainer(containerId).getName();
assertEquals(CONTAINER_NAME, name);
}
use of io.fabric8.maven.docker.access.ContainerCreateConfig in project docker-maven-plugin by fabric8io.
the class RunServiceTest method failAfterRetryingIfInsufficientPortBindingInformation.
@Test(expected = PortBindingException.class)
public void failAfterRetryingIfInsufficientPortBindingInformation(@Mocked Container container, @Mocked ImageConfiguration imageConfiguration, @Mocked PortMapping portMapping) throws DockerAccessException {
new Expectations() {
{
docker.createContainer(withAny(new ContainerCreateConfig("img")), anyString);
result = "containerId";
portMapping.needsPropertiesUpdate();
result = true;
queryService.getMandatoryContainer("containerId");
result = container;
times = 20;
container.isRunning();
result = true;
container.getPortBindings();
result = new PortBindingException("5432/tcp", new Gson().fromJson("{\"5432/tcp\": []}", JsonObject.class));
}
};
runService.createAndStartContainer(imageConfiguration, portMapping, new GavLabel("Im:A:Test"), properties, getBaseDirectory(), "blah", new Date());
}
Aggregations