@Override
    public final void before() throws Throwable {
        HostConfig.Builder hostConfigBuilder = HostConfig.builder()
                .publishAllPorts(builder.publishAllPorts())//
                .portBindings(builder.hostPortBindings())//
                .binds(builder.binds())//
                .links(links());
        if (builder.restartPolicy() != null) {
            hostConfigBuilder.restartPolicy(builder.restartPolicy().getRestartPolicy());
        }
        HostConfig hostConfig = hostConfigBuilder
                .extraHosts(builder.extraHosts())//
                .ulimits(builder.getUlimits())
                .build();
        ContainerConfig.Builder containerConfigBuilder = ContainerConfig.builder()//
                .hostConfig(hostConfig)//
                .image(imageNameWithTag)//
                .env(builder.env())//
                .networkDisabled(false)//
                .exposedPorts(builder.containerExposedPorts())
                .labels(builder.getLabels());
        if (builder.entrypoint().length > 0) {
            containerConfigBuilder.entrypoint(builder.entrypoint());
        }
        if (builder.cmd().length > 0) {
            containerConfigBuilder.cmd(builder.cmd());
        }
        ContainerConfig containerConfig = containerConfigBuilder.build();
        try {
            if (StringUtils.isNotBlank(builder.name())) {
                this.container = dockerClient.createContainer(containerConfig, builder.name());
            } else {
                this.container = dockerClient.createContainer(containerConfig);
            }

            this.containerShortId = StringUtils.left(container.id(), SHORT_ID_LEN);
            log.info("container {} created, id {}, short id {}", imageNameWithTag, container.id(), containerShortId);
            log.debug("rule before {}", containerShortId);

            dockerClient.startContainer(container.id());
            log.debug("{} started", containerShortId);

            LineListenerProxy proxyLineListener = new LineListenerProxy();
            attachLogs(dockerClient, container.id(), proxyLineListener);

            ContainerInfo containerInfo = dockerClient.inspectContainer(container.id());
            containerIp = containerInfo.networkSettings().ipAddress();
            containerPorts = containerInfo.networkSettings().ports();
            containerGateway = containerInfo.networkSettings().gateway();
            this.containerInfo = containerInfo;

            executeWaitForConditions(proxyLineListener);
            logNetworkSettings();

            isStarted = true;
        } catch (DockerRequestException e) {
            throw new IllegalStateException(e.getResponseBody(), e);
        } catch (DockerException | InterruptedException e) {
            throw new IllegalStateException(e);
        }
    }
@ Override public final void before ( ) throws Throwable { HostConfig . Builder hostConfigBuilder = HostConfig . builder ( ) . publishAllPorts ( builder . publishAllPorts ( ) ) . portBindings ( builder . hostPortBindings ( ) ) . binds ( builder . binds ( ) ) . links ( links ( ) ) ; if ( builder . restartPolicy ( ) != null ) { hostConfigBuilder . restartPolicy ( builder . restartPolicy ( ) . getRestartPolicy ( ) ) ; } HostConfig hostConfig = hostConfigBuilder . extraHosts ( builder . extraHosts ( ) ) . ulimits ( builder . getUlimits ( ) ) . build ( ) ; ContainerConfig . Builder containerConfigBuilder = ContainerConfig . builder ( ) . hostConfig ( hostConfig ) . image ( imageNameWithTag ) . env ( builder . env ( ) ) . networkDisabled ( false ) . exposedPorts ( builder . containerExposedPorts ( ) ) . labels ( builder . getLabels ( ) ) ; if ( builder . entrypoint ( ) . length > 0 ) { containerConfigBuilder . entrypoint ( builder . entrypoint ( ) ) ; } if ( builder . cmd ( ) . length > 0 ) { containerConfigBuilder . cmd ( builder . cmd ( ) ) ; } ContainerConfig containerConfig = containerConfigBuilder . build ( ) ; try { if ( StringUtils . isNotBlank ( builder . name ( ) ) ) { this . container = dockerClient . createContainer ( containerConfig , builder . name ( ) ) ; } else { this . container = dockerClient . createContainer ( containerConfig ) ; } this . containerShortId = StringUtils . left ( container . id ( ) , SHORT_ID_LEN ) ; log . info ( "container {} created, id {}, short id {}" , imageNameWithTag , container . id ( ) , containerShortId ) ; log . debug ( "rule before {}" , containerShortId ) ; dockerClient . startContainer ( container . id ( ) ) ; log . debug ( "{} started" , containerShortId ) ; LineListenerProxy proxyLineListener = new LineListenerProxy ( ) ; attachLogs ( dockerClient , container . id ( ) , proxyLineListener ) ; ContainerInfo containerInfo = dockerClient . inspectContainer ( container . id ( ) ) ; containerIp = containerInfo . networkSettings ( ) . ipAddress ( ) ; containerPorts = containerInfo . networkSettings ( ) . ports ( ) ; containerGateway = containerInfo . networkSettings ( ) . gateway ( ) ; this . containerInfo = containerInfo ; executeWaitForConditions ( proxyLineListener ) ; logNetworkSettings ( ) ; isStarted = true ; } catch ( DockerRequestException e ) { throw new IllegalStateException ( e . getResponseBody ( ) , e ) ; } catch ( DockerException | InterruptedException e ) { throw new IllegalStateException ( e ) ; } }
Create and start container.<br/>
This is {@link ExternalResource#before()} made available as public - it may be helpful in scenarios
when you want to use {@link DockerRule} and operate it manually.