Search in sources :

Example 1 with CreateContainerCmd

use of com.github.dockerjava.api.command.CreateContainerCmd in project elastest-torm by elastest.

the class DockerService method startAndWaitContainer.

public void startAndWaitContainer(DockerContainer dockerContainer) throws InterruptedException {
    String containerName = dockerContainer.getContainerName();
    String imageId = dockerContainer.getImageId();
    if (!isRunningContainer(containerName)) {
        pullImageIfNecessary(imageId);
        try (CreateContainerCmd createContainer = dockerClient.createContainerCmd(imageId).withName(containerName)) {
            Optional<String> network = dockerContainer.getNetwork();
            if (network.isPresent()) {
                log.trace("Using network: {}", network.get());
                createContainer.withNetworkMode(network.get());
            }
            Optional<List<PortBinding>> portBindings = dockerContainer.getPortBindings();
            if (portBindings.isPresent()) {
                log.trace("Using port binding: {}", portBindings.get());
                createContainer.withPortBindings(portBindings.get());
            }
            Optional<List<Volume>> volumes = dockerContainer.getVolumes();
            if (volumes.isPresent()) {
                log.trace("Using volumes: {}", volumes.get());
                createContainer.withVolumes(volumes.get());
            }
            Optional<List<Bind>> binds = dockerContainer.getBinds();
            if (binds.isPresent()) {
                log.trace("Using binds: {}", binds.get());
                createContainer.withBinds(binds.get());
            }
            Optional<List<String>> envs = dockerContainer.getEnvs();
            if (envs.isPresent()) {
                log.trace("Using envs: {}", envs.get());
                createContainer.withEnv(envs.get());
            }
            createContainer.exec();
            dockerClient.startContainerCmd(containerName).exec();
            waitForContainer(containerName);
        }
    } else {
        log.warn("Container {} already running", containerName);
    }
}
Also used : CreateContainerCmd(com.github.dockerjava.api.command.CreateContainerCmd) List(java.util.List)

Example 2 with CreateContainerCmd

use of com.github.dockerjava.api.command.CreateContainerCmd in project elastest-torm by elastest.

the class DockerService2 method createContainer.

public CreateContainerResponse createContainer(DockerExecution dockerExec, String type) throws TJobStoppedException {
    TJobExecution tJobExec = dockerExec.gettJobexec();
    TJob tJob = tJobExec.getTjob();
    SutSpecification sut = tJob.getSut();
    String image = "";
    String commands = null;
    List<Parameter> parametersList = new ArrayList<Parameter>();
    String prefix = "";
    String suffix = "";
    String containerName = "";
    int logPort = 5000;
    String sutHost = null;
    String sutPath = null;
    if ("sut".equals(type.toLowerCase())) {
        parametersList = sut.getParameters();
        commands = sut.getCommands();
        image = sut.getSpecification();
        prefix = "sut_";
        if (sut.isSutInNewContainer()) {
            suffix = sut.getSutInContainerAuxLabel();
        }
        containerName = getSutName(dockerExec);
        sutPath = filesService.buildFilesPath(tJobExec, ElastestConstants.SUT_FOLDER);
        filesService.createExecFilesFolder(sutPath);
    } else if ("tjob".equals(type.toLowerCase())) {
        parametersList = tJobExec.getParameters();
        commands = tJob.getCommands();
        image = tJob.getImageName();
        prefix = "test_";
        containerName = getTestName(dockerExec);
        if (dockerExec.isWithSut()) {
            sutHost = dockerExec.getSutExec().getIp();
        }
    }
    // Environment variables (optional)
    ArrayList<String> envList = new ArrayList<>();
    String envVar;
    // Get TJob Exec Env Vars
    for (Map.Entry<String, String> entry : dockerExec.gettJobexec().getEnvVars().entrySet()) {
        envVar = entry.getKey() + "=" + entry.getValue();
        envList.add(envVar);
    }
    // Get Parameters and insert into Env Vars
    for (Parameter parameter : parametersList) {
        envVar = parameter.getName() + "=" + parameter.getValue();
        envList.add(envVar);
    }
    if (sutHost != null) {
        envList.add("ET_SUT_HOST=" + dockerExec.getSutExec().getIp());
    }
    // Commands (optional)
    ArrayList<String> cmdList = new ArrayList<>();
    ArrayList<String> entrypointList = new ArrayList<>();
    if (commands != null && !commands.isEmpty()) {
        cmdList.add("-c");
        if (sut != null) {
            if (sut.isSutInNewContainer()) {
                commands = sutPath != null ? ("cd " + sutPath + ";" + commands) : commands;
            }
        } else {
            commands = "export ET_SUT_HOST=$(" + this.getThisContainerIpCmd + ") || echo;" + commands;
        }
        cmdList.add(commands);
        entrypointList.add("/bin/sh");
    }
    // Load Log Config
    LogConfig logConfig = null;
    if (tJob.isSelectedService("ems")) {
        try {
            logConfig = getEMSLogConfig(type, prefix, suffix, dockerExec);
        } catch (Exception e) {
            logger.error("", e);
        }
    } else {
        logConfig = getDefaultLogConfig(logPort, prefix, suffix, dockerExec);
    }
    // Pull Image
    this.pullETExecImage(image, type);
    // Create docker sock volume
    Volume dockerSockVolume = new Volume(dockerSock);
    CreateContainerCmd containerCmd = dockerExec.getDockerClient().createContainerCmd(image).withEnv(envList).withLogConfig(logConfig).withName(containerName).withCmd(cmdList).withEntrypoint(entrypointList).withNetworkMode(dockerExec.getNetwork());
    Volume sharedDataVolume = null;
    if (dockerExec.gettJobexec().getTjob().getSut() != null && dockerExec.gettJobexec().getTjob().getSut().isSutInNewContainer()) {
        sharedDataVolume = new Volume(sharedFolder);
    }
    if (sharedDataVolume != null) {
        containerCmd = containerCmd.withVolumes(dockerSockVolume, sharedDataVolume).withBinds(new Bind(dockerSock, dockerSockVolume), new Bind(sharedFolder, sharedDataVolume));
    } else {
        containerCmd = containerCmd.withVolumes(dockerSockVolume).withBinds(new Bind(dockerSock, dockerSockVolume));
    }
    // Create Container
    return containerCmd.exec();
}
Also used : Bind(com.github.dockerjava.api.model.Bind) TJobExecution(io.elastest.etm.model.TJobExecution) ArrayList(java.util.ArrayList) SutSpecification(io.elastest.etm.model.SutSpecification) TJob(io.elastest.etm.model.TJob) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotModifiedException(com.github.dockerjava.api.exception.NotModifiedException) DockerClientException(com.github.dockerjava.api.exception.DockerClientException) InternalServerErrorException(com.github.dockerjava.api.exception.InternalServerErrorException) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) CreateContainerCmd(com.github.dockerjava.api.command.CreateContainerCmd) Volume(com.github.dockerjava.api.model.Volume) Parameter(io.elastest.etm.model.Parameter) Map(java.util.Map) HashMap(java.util.HashMap) LogConfig(com.github.dockerjava.api.model.LogConfig)

Example 3 with CreateContainerCmd

use of com.github.dockerjava.api.command.CreateContainerCmd in project testcontainers-java by testcontainers.

the class GenericContainer method applyConfiguration.

private void applyConfiguration(CreateContainerCmd createCommand) {
    // Set up exposed ports (where there are no host port bindings defined)
    ExposedPort[] portArray = exposedPorts.stream().map(ExposedPort::new).toArray(ExposedPort[]::new);
    createCommand.withExposedPorts(portArray);
    // Set up exposed ports that need host port bindings
    PortBinding[] portBindingArray = portBindings.stream().map(PortBinding::parse).toArray(PortBinding[]::new);
    createCommand.withPortBindings(portBindingArray);
    if (commandParts != null) {
        createCommand.withCmd(commandParts);
    }
    String[] envArray = env.entrySet().stream().map(it -> it.getKey() + "=" + it.getValue()).toArray(String[]::new);
    createCommand.withEnv(envArray);
    Bind[] bindsArray = binds.stream().toArray(Bind[]::new);
    createCommand.withBinds(bindsArray);
    VolumesFrom[] volumesFromsArray = volumesFroms.stream().toArray(VolumesFrom[]::new);
    createCommand.withVolumesFrom(volumesFromsArray);
    Set<Link> allLinks = new HashSet<>();
    Set<String> allLinkedContainerNetworks = new HashSet<>();
    for (Map.Entry<String, LinkableContainer> linkEntries : linkedContainers.entrySet()) {
        String alias = linkEntries.getKey();
        LinkableContainer linkableContainer = linkEntries.getValue();
        Set<Link> links = findLinksFromThisContainer(alias, linkableContainer);
        allLinks.addAll(links);
        if (allLinks.size() == 0) {
            throw new ContainerLaunchException("Aborting attempt to link to container " + linkableContainer.getContainerName() + " as it is not running");
        }
        Set<String> linkedContainerNetworks = findAllNetworksForLinkedContainers(linkableContainer);
        allLinkedContainerNetworks.addAll(linkedContainerNetworks);
    }
    createCommand.withLinks(allLinks.toArray(new Link[allLinks.size()]));
    allLinkedContainerNetworks.remove("bridge");
    if (allLinkedContainerNetworks.size() > 1) {
        logger().warn("Container needs to be on more than one custom network to link to other " + "containers - this is not currently supported. Required networks are: {}", allLinkedContainerNetworks);
    }
    Optional<String> networkForLinks = allLinkedContainerNetworks.stream().findFirst();
    if (networkForLinks.isPresent()) {
        logger().debug("Associating container with network: {}", networkForLinks.get());
        createCommand.withNetworkMode(networkForLinks.get());
    }
    createCommand.withPublishAllPorts(true);
    String[] extraHostsArray = extraHosts.stream().toArray(String[]::new);
    createCommand.withExtraHosts(extraHostsArray);
    if (network != null) {
        createCommand.withNetworkMode(network.getId());
        createCommand.withAliases(this.networkAliases);
    } else if (networkMode != null) {
        createCommand.withNetworkMode(networkMode);
    }
    if (workingDirectory != null) {
        createCommand.withWorkingDir(workingDirectory);
    }
    if (privilegedMode) {
        createCommand.withPrivileged(privilegedMode);
    }
    createContainerCmdModifiers.forEach(hook -> hook.accept(createCommand));
    Map<String, String> labels = createCommand.getLabels();
    labels = new HashMap<>(labels != null ? labels : Collections.emptyMap());
    labels.putAll(DockerClientFactory.DEFAULT_LABELS);
    createCommand.withLabels(labels);
}
Also used : Arrays(java.util.Arrays) MinimumDurationRunningStartupCheckStrategy(org.testcontainers.containers.startupcheck.MinimumDurationRunningStartupCheckStrategy) VolumesFrom(com.github.dockerjava.api.model.VolumesFrom) Link(com.github.dockerjava.api.model.Link) DockerClient(com.github.dockerjava.api.DockerClient) IsRunningStartupCheckStrategy(org.testcontainers.containers.startupcheck.IsRunningStartupCheckStrategy) Future(java.util.concurrent.Future) Wait(org.testcontainers.containers.wait.Wait) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) STDOUT(org.testcontainers.containers.output.OutputFrame.OutputType.STDOUT) Duration(java.time.Duration) Map(java.util.Map) ExposedPort(com.github.dockerjava.api.model.ExposedPort) PortBinding(com.github.dockerjava.api.model.PortBinding) WaitStrategyTarget(org.testcontainers.containers.wait.strategy.WaitStrategyTarget) Path(java.nio.file.Path) NonNull(lombok.NonNull) StartupCheckStrategy(org.testcontainers.containers.startupcheck.StartupCheckStrategy) Set(java.util.Set) Description(org.junit.runner.Description) EqualsAndHashCode(lombok.EqualsAndHashCode) InspectContainerResponse(com.github.dockerjava.api.command.InspectContainerResponse) Collectors(java.util.stream.Collectors) WaitStrategy(org.testcontainers.containers.wait.WaitStrategy) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Stream(java.util.stream.Stream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Optional(java.util.Optional) DockerClientFactory(org.testcontainers.DockerClientFactory) NotNull(org.jetbrains.annotations.NotNull) Info(com.github.dockerjava.api.model.Info) Setter(lombok.Setter) Bind(com.github.dockerjava.api.model.Bind) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) CreateContainerCmd(com.github.dockerjava.api.command.CreateContainerCmd) AccessLevel(lombok.AccessLevel) RemoteDockerImage(org.testcontainers.images.RemoteDockerImage) Slf4jLogConsumer(org.testcontainers.containers.output.Slf4jLogConsumer) Charset(java.nio.charset.Charset) FrameConsumerResultCallback(org.testcontainers.containers.output.FrameConsumerResultCallback) CommandLine.runShellCommand(org.testcontainers.utility.CommandLine.runShellCommand) org.testcontainers.utility(org.testcontainers.utility) LinkedHashSet(java.util.LinkedHashSet) Profiler(org.slf4j.profiler.Profiler) Logger(org.slf4j.Logger) IOUtils(org.apache.commons.compress.utils.IOUtils) FileOutputStream(java.io.FileOutputStream) RateLimiterBuilder(org.rnorth.ducttape.ratelimits.RateLimiterBuilder) IOException(java.io.IOException) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Unreliables(org.rnorth.ducttape.unreliables.Unreliables) RateLimiter(org.rnorth.ducttape.ratelimits.RateLimiter) LinkableContainer(org.testcontainers.containers.traits.LinkableContainer) Data(lombok.Data) Volume(com.github.dockerjava.api.model.Volume) OutputFrame(org.testcontainers.containers.output.OutputFrame) Collections(java.util.Collections) STDERR(org.testcontainers.containers.output.OutputFrame.OutputType.STDERR) LinkableContainer(org.testcontainers.containers.traits.LinkableContainer) Bind(com.github.dockerjava.api.model.Bind) VolumesFrom(com.github.dockerjava.api.model.VolumesFrom) ExposedPort(com.github.dockerjava.api.model.ExposedPort) PortBinding(com.github.dockerjava.api.model.PortBinding) Map(java.util.Map) HashMap(java.util.HashMap) Link(com.github.dockerjava.api.model.Link) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with CreateContainerCmd

use of com.github.dockerjava.api.command.CreateContainerCmd in project beam by apache.

the class BaseClickHouseTest method setup.

@BeforeClass
public static void setup() throws IOException, InterruptedException {
    // network sharing doesn't work with ClassRule
    network = Network.newNetwork();
    zookeeper = new GenericContainer<>(DockerImageName.parse("zookeeper").withTag(ZOOKEEPER_VERSION)).withStartupAttempts(10).withExposedPorts(2181).withNetwork(network).withNetworkAliases("zookeeper");
    // so far zookeeper container always starts successfully, so no extra retries
    zookeeper.start();
    clickHouse = (ClickHouseContainer) new ClickHouseContainer(CLICKHOUSE_IMAGE).withStartupAttempts(10).withCreateContainerCmdModifier(// type inference for `(CreateContainerCmd) -> cmd.` doesn't work
    cmd -> ((CreateContainerCmd) cmd).withMemory(256 * 1024 * 1024L).withMemorySwap(4L * 1024 * 1024 * 1024L)).withNetwork(network).withClasspathResourceMapping("config.d/zookeeper_default.xml", "/etc/clickhouse-server/config.d/zookeeper_default.xml", BindMode.READ_ONLY);
    BackOff backOff = FluentBackoff.DEFAULT.withMaxRetries(3).withInitialBackoff(Duration.standardSeconds(15)).backoff();
    // try to start clickhouse-server a couple of times, see BEAM-6639
    while (true) {
        try {
            Unreliables.retryUntilSuccess(10, () -> {
                DockerClientFactory.instance().checkAndPullImage(DockerClientFactory.instance().client(), CLICKHOUSE_IMAGE);
                return null;
            });
            clickHouse.start();
            break;
        } catch (Exception e) {
            if (!BackOffUtils.next(Sleeper.DEFAULT, backOff)) {
                throw e;
            } else {
                List<Image> images = DockerClientFactory.instance().client().listImagesCmd().withShowAll(true).exec();
                String listImagesOutput = "listImagesCmd:\n" + Joiner.on('\n').join(images) + "\n";
                LOG.warn("failed to start clickhouse-server\n\n" + listImagesOutput, e);
            }
        }
    }
}
Also used : CreateContainerCmd(com.github.dockerjava.api.command.CreateContainerCmd) List(java.util.List) ClickHouseContainer(org.testcontainers.containers.ClickHouseContainer) BackOff(org.apache.beam.sdk.util.BackOff) SQLException(java.sql.SQLException) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 5 with CreateContainerCmd

use of com.github.dockerjava.api.command.CreateContainerCmd in project hazelcast by hazelcast.

the class MySqlCdcNetworkIntegrationTest method initMySql.

private MySQLContainer<?> initMySql(Network network, Integer fixedExposedPort) {
    MySQLContainer<?> mysql = namedTestContainer(new MySQLContainer<>(AbstractMySqlCdcIntegrationTest.DOCKER_IMAGE).withUsername("mysqluser").withPassword("mysqlpw"));
    if (fixedExposedPort != null) {
        Consumer<CreateContainerCmd> cmd = e -> e.withPortBindings(new PortBinding(Ports.Binding.bindPort(fixedExposedPort), new ExposedPort(MYSQL_PORT)));
        mysql = mysql.withCreateContainerCmdModifier(cmd);
    }
    if (network != null) {
        mysql = mysql.withNetwork(network);
    }
    mysql.start();
    return mysql;
}
Also used : CreateContainerCmd(com.github.dockerjava.api.command.CreateContainerCmd) Arrays(java.util.Arrays) Ports(com.github.dockerjava.api.model.Ports) Connection(java.sql.Connection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) RetryStrategies(com.hazelcast.jet.retry.RetryStrategies) TimeoutException(java.util.concurrent.TimeoutException) HazelcastSerialParametersRunnerFactory(com.hazelcast.test.HazelcastSerialParametersRunnerFactory) StreamSource(com.hazelcast.jet.pipeline.StreamSource) Network(org.testcontainers.containers.Network) JetException(com.hazelcast.jet.JetException) CreateContainerCmd(com.github.dockerjava.api.command.CreateContainerCmd) SQLException(java.sql.SQLException) Util.entry(com.hazelcast.jet.Util.entry) After(org.junit.After) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ExposedPort(com.github.dockerjava.api.model.ExposedPort) Assume(org.junit.Assume) Assert.fail(org.junit.Assert.fail) PortBinding(com.github.dockerjava.api.model.PortBinding) ChangeRecord(com.hazelcast.jet.cdc.ChangeRecord) AbstractCdcIntegrationTest(com.hazelcast.jet.cdc.AbstractCdcIntegrationTest) Job(com.hazelcast.jet.Job) Before(org.junit.Before) UseParametersRunnerFactory(org.junit.runners.Parameterized.UseParametersRunnerFactory) HazelcastInstance(com.hazelcast.core.HazelcastInstance) NightlyTest(com.hazelcast.test.annotation.NightlyTest) HazelcastParametrizedRunner(com.hazelcast.test.HazelcastParametrizedRunner) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Parameter(org.junit.runners.Parameterized.Parameter) Collection(java.util.Collection) Sinks(com.hazelcast.jet.pipeline.Sinks) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Category(org.junit.experimental.categories.Category) MySQLContainer(org.testcontainers.containers.MySQLContainer) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) ToxiproxyContainer(org.testcontainers.containers.ToxiproxyContainer) Statement(java.sql.Statement) RUNNING(com.hazelcast.jet.core.JobStatus.RUNNING) RetryStrategy(com.hazelcast.jet.retry.RetryStrategy) MYSQL_PORT(org.testcontainers.containers.MySQLContainer.MYSQL_PORT) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) ExposedPort(com.github.dockerjava.api.model.ExposedPort) PortBinding(com.github.dockerjava.api.model.PortBinding)

Aggregations

CreateContainerCmd (com.github.dockerjava.api.command.CreateContainerCmd)13 IOException (java.io.IOException)5 ExposedPort (com.github.dockerjava.api.model.ExposedPort)4 ArrayList (java.util.ArrayList)4 NotFoundException (com.github.dockerjava.api.exception.NotFoundException)3 Bind (com.github.dockerjava.api.model.Bind)3 PortBinding (com.github.dockerjava.api.model.PortBinding)3 Volume (com.github.dockerjava.api.model.Volume)3 List (java.util.List)3 DockerClient (com.github.dockerjava.api.DockerClient)2 InternalServerErrorException (com.github.dockerjava.api.exception.InternalServerErrorException)2 Capability (com.github.dockerjava.api.model.Capability)2 Ports (com.github.dockerjava.api.model.Ports)2 VolumesFrom (com.github.dockerjava.api.model.VolumesFrom)2 ContainerException (com.github.qzagarese.dockerunit.exception.ContainerException)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 JetException (com.hazelcast.jet.JetException)2 Job (com.hazelcast.jet.Job)2 Util.entry (com.hazelcast.jet.Util.entry)2 AbstractCdcIntegrationTest (com.hazelcast.jet.cdc.AbstractCdcIntegrationTest)2