Search in sources :

Example 1 with GavLabel

use of io.fabric8.maven.docker.util.GavLabel in project docker-maven-plugin by fabric8io.

the class RunService method getContainers.

/**
 * Get all started containers.
 * The containers are returned in order of their registration.
 * If no pom label is given, then all containers are returned.
 *
 * @param gavLabel the label for which to get the containers or <code>null</code> for all containers
 * @return the containers for the given label or an empty collection
 */
public List<ContainerDescriptor> getContainers(GavLabel gavLabel) {
    Collection<ContainerShutdownDescriptor> shutdownDescriptors = tracker.getShutdownDescriptors(gavLabel);
    List<ContainerDescriptor> containers = new ArrayList<>(shutdownDescriptors.size());
    for (ContainerShutdownDescriptor descriptor : shutdownDescriptors) {
        containers.add(new ContainerDescriptor(descriptor.getContainerId(), descriptor.getImageConfiguration()));
    }
    return containers;
}
Also used : ContainerShutdownDescriptor(io.fabric8.maven.docker.service.ContainerTracker.ContainerShutdownDescriptor) ArrayList(java.util.ArrayList)

Example 2 with GavLabel

use of io.fabric8.maven.docker.util.GavLabel 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);
    }
}
Also used : RunVolumeConfiguration(io.fabric8.maven.docker.config.RunVolumeConfiguration) NetworkConfig(io.fabric8.maven.docker.config.NetworkConfig) ContainerCreateConfig(io.fabric8.maven.docker.access.ContainerCreateConfig) ContainerNetworkingConfig(io.fabric8.maven.docker.access.ContainerNetworkingConfig)

Example 3 with GavLabel

use of io.fabric8.maven.docker.util.GavLabel 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);
}
Also used : Container(io.fabric8.maven.docker.model.Container) ContainerCreateConfig(io.fabric8.maven.docker.access.ContainerCreateConfig) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration)

Example 4 with GavLabel

use of io.fabric8.maven.docker.util.GavLabel in project docker-maven-plugin by fabric8io.

the class CopyMojo method copyFromTemporaryContainers.

private void copyFromTemporaryContainers(DockerAccess dockerAccess, RunService runService, RegistryService registryService, ArchiveService archiveService, QueryService queryService, GavLabel gavLabel) throws IOException, MojoExecutionException {
    List<ImageConfiguration> imageConfigurations = getResolvedImages();
    for (ImageConfiguration imageConfiguration : imageConfigurations) {
        CopyConfiguration copyConfiguration = imageConfiguration.getCopyConfiguration();
        String imageName = imageConfiguration.getName();
        if (isEmpty(copyConfiguration)) {
            log.debug("Copy configuration is not defined for %s image, skipping coping", imageName);
            continue;
        }
        try (ContainerRemover containerRemover = new ContainerRemover(log, runService, removeVolumes)) {
            String containerId = createContainer(runService, registryService, imageConfiguration, gavLabel);
            containerRemover.setContainerId(containerId);
            log.debug("Created %s container from %s image", containerId, imageName);
            copy(dockerAccess, archiveService, containerId, imageName, copyConfiguration);
        }
    }
}
Also used : ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) CopyConfiguration(io.fabric8.maven.docker.config.CopyConfiguration)

Example 5 with GavLabel

use of io.fabric8.maven.docker.util.GavLabel in project docker-maven-plugin by fabric8io.

the class CopyMojo method copyFromStartedContainers.

private void copyFromStartedContainers(DockerAccess dockerAccess, RunService runService, ArchiveService archiveService, GavLabel gavLabel) throws IOException, MojoExecutionException {
    List<ContainerDescriptor> containerDescriptors = runService.getContainers(gavLabel);
    for (ContainerDescriptor containerDescriptor : containerDescriptors) {
        ImageConfiguration imageConfiguration = containerDescriptor.getImageConfig();
        CopyConfiguration copyConfiguration = imageConfiguration.getCopyConfiguration();
        String imageName = imageConfiguration.getName();
        if (isEmpty(copyConfiguration)) {
            log.debug("Copy configuration is not defined for %s image, skipping coping", imageName);
            continue;
        }
        String containerId = containerDescriptor.getContainerId();
        log.debug("Found %s container of %s image started by start mojo", containerId, imageName);
        copy(dockerAccess, archiveService, containerId, imageName, copyConfiguration);
    }
}
Also used : ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) CopyConfiguration(io.fabric8.maven.docker.config.CopyConfiguration) ContainerDescriptor(io.fabric8.maven.docker.service.RunService.ContainerDescriptor)

Aggregations

GavLabel (io.fabric8.maven.docker.util.GavLabel)7 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)5 Expectations (mockit.Expectations)5 ContainerCreateConfig (io.fabric8.maven.docker.access.ContainerCreateConfig)4 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)4 RunService (io.fabric8.maven.docker.service.RunService)4 Test (org.junit.Test)4 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)3 Container (io.fabric8.maven.docker.model.Container)3 Network (io.fabric8.maven.docker.model.Network)3 PortBindingException (io.fabric8.maven.docker.model.PortBindingException)3 QueryService (io.fabric8.maven.docker.service.QueryService)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 Properties (java.util.Properties)3 Gson (com.google.gson.Gson)2 PortMapping (io.fabric8.maven.docker.access.PortMapping)2 CopyConfiguration (io.fabric8.maven.docker.config.CopyConfiguration)2 NetworkConfig (io.fabric8.maven.docker.config.NetworkConfig)2 LogDispatcher (io.fabric8.maven.docker.log.LogDispatcher)2