Search in sources :

Example 1 with PortMapping

use of io.fabric8.maven.docker.access.PortMapping in project docker-maven-plugin by fabric8io.

the class WatchService method defaultContainerRestartTask.

private Task<ImageWatcher> defaultContainerRestartTask() {
    return new Task<ImageWatcher>() {

        @Override
        public void execute(ImageWatcher watcher) throws Exception {
            // Stop old one
            ImageConfiguration imageConfig = watcher.getImageConfiguration();
            PortMapping mappedPorts = runService.createPortMapping(imageConfig.getRunConfiguration(), watcher.getWatchContext().getMojoParameters().getProject().getProperties());
            String id = watcher.getContainerId();
            String optionalPreStop = getPreStopCommand(imageConfig);
            if (optionalPreStop != null) {
                runService.execInContainer(id, optionalPreStop, watcher.getImageConfiguration());
            }
            runService.stopPreviouslyStartedContainer(id, false, false);
            // Start new one
            watcher.setContainerId(runService.createAndStartContainer(imageConfig, mappedPorts, watcher.getWatchContext().getPomLabel(), watcher.getWatchContext().getMojoParameters().getProject().getProperties(), watcher.getWatchContext().getMojoParameters().getProject().getBasedir()));
        }
    };
}
Also used : Task(io.fabric8.maven.docker.util.Task) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) WatchImageConfiguration(io.fabric8.maven.docker.config.WatchImageConfiguration) PortMapping(io.fabric8.maven.docker.access.PortMapping)

Example 2 with PortMapping

use of io.fabric8.maven.docker.access.PortMapping in project docker-maven-plugin by fabric8io.

the class RunService method createContainerHostConfig.

ContainerHostConfig createContainerHostConfig(RunImageConfiguration runConfig, PortMapping mappedPorts, File baseDir) throws DockerAccessException {
    RestartPolicy restartPolicy = runConfig.getRestartPolicy();
    List<String> links = findContainerIdsForLinks(runConfig.getLinks(), runConfig.getNetworkingConfig().isCustomNetwork());
    ContainerHostConfig config = new ContainerHostConfig().extraHosts(runConfig.getExtraHosts()).links(links).portBindings(mappedPorts).privileged(runConfig.getPrivileged()).shmSize(runConfig.getShmSize()).dns(runConfig.getDns()).dnsSearch(runConfig.getDnsSearch()).capAdd(runConfig.getCapAdd()).capDrop(runConfig.getCapDrop()).sysctls(runConfig.getSysctls()).securityOpts(runConfig.getSecurityOpts()).memory(runConfig.getMemory()).memorySwap(runConfig.getMemorySwap()).restartPolicy(restartPolicy.getName(), restartPolicy.getRetry()).logConfig(runConfig.getLogConfiguration()).tmpfs(runConfig.getTmpfs()).ulimits(runConfig.getUlimits()).isolation(runConfig.getIsolation()).cpuShares(runConfig.getCpuShares()).cpus(runConfig.getCpus()).cpuSet(runConfig.getCpuSet()).readonlyRootfs(runConfig.getReadOnly()).autoRemove(runConfig.getAutoRemove());
    addVolumeConfig(config, runConfig, baseDir);
    addNetworkingConfig(config, runConfig);
    return config;
}
Also used : RestartPolicy(io.fabric8.maven.docker.config.RestartPolicy) ContainerHostConfig(io.fabric8.maven.docker.access.ContainerHostConfig)

Example 3 with PortMapping

use of io.fabric8.maven.docker.access.PortMapping 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 4 with PortMapping

use of io.fabric8.maven.docker.access.PortMapping 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 5 with PortMapping

use of io.fabric8.maven.docker.access.PortMapping 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);
}
Also used : Arguments(io.fabric8.maven.docker.config.Arguments) ContainerCreateConfig(io.fabric8.maven.docker.access.ContainerCreateConfig) PortMapping(io.fabric8.maven.docker.access.PortMapping) Properties(java.util.Properties) ContainerHostConfig(io.fabric8.maven.docker.access.ContainerHostConfig)

Aggregations

PortMapping (io.fabric8.maven.docker.access.PortMapping)8 ContainerCreateConfig (io.fabric8.maven.docker.access.ContainerCreateConfig)7 Properties (java.util.Properties)5 ContainerHostConfig (io.fabric8.maven.docker.access.ContainerHostConfig)4 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)4 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)4 GavLabel (io.fabric8.maven.docker.util.GavLabel)4 Date (java.util.Date)4 Arguments (io.fabric8.maven.docker.config.Arguments)3 PortBindingException (io.fabric8.maven.docker.model.PortBindingException)3 Gson (com.google.gson.Gson)2 ContainerNetworkingConfig (io.fabric8.maven.docker.access.ContainerNetworkingConfig)2 NetworkConfig (io.fabric8.maven.docker.config.NetworkConfig)2 RestartPolicy (io.fabric8.maven.docker.config.RestartPolicy)2 RunVolumeConfiguration (io.fabric8.maven.docker.config.RunVolumeConfiguration)2 LogOutputSpecFactory (io.fabric8.maven.docker.log.LogOutputSpecFactory)2 Container (io.fabric8.maven.docker.model.Container)2 ContainerDetails (io.fabric8.maven.docker.model.ContainerDetails)2 RunService (io.fabric8.maven.docker.service.RunService)2 File (java.io.File)2