Search in sources :

Example 1 with DockerBuilder

use of io.github.bonigarcia.wdm.docker.DockerContainer.DockerBuilder in project webdrivermanager by bonigarcia.

the class DockerService method startBrowserContainer.

public DockerContainer startBrowserContainer(String dockerImage, String cacheKey, String browserVersion, boolean androidEnabled) {
    dockerImage = getPrefixedDockerImage(dockerImage);
    // pull image
    pullImageIfNecessary(cacheKey, dockerImage, browserVersion);
    // exposed ports
    List<String> exposedPorts = new ArrayList<>();
    String dockerBrowserPort = String.valueOf(config.getDockerBrowserPort());
    exposedPorts.add(dockerBrowserPort);
    // shmSize
    long shmSize = config.getDockerMemSizeBytes(config.getDockerShmSize());
    // mounts
    List<Mount> mounts = new ArrayList<>();
    Mount tmpfsMount = new Mount().withTmpfsOptions(new TmpfsOptions().withSizeBytes(config.getDockerMemSizeBytes(config.getDockerTmpfsSize()))).withTarget(config.getDockerTmpfsMount());
    mounts.add(tmpfsMount);
    // binds
    List<String> binds = new ArrayList<>();
    String dockerVolumes = config.getDockerVolumes();
    if (!isNullOrEmpty(dockerVolumes)) {
        List<String> volumeList = Arrays.asList(dockerVolumes.split(","));
        log.trace("Using custom volumes: {}", volumeList);
        binds.addAll(volumeList);
    }
    // envs
    List<String> envs = new ArrayList<>();
    envs.add("TZ=" + config.getDockerTimezone());
    envs.add("LANG=" + config.getDockerLang());
    envs.add("SCREEN_RESOLUTION=" + config.getDockerScreenResolution());
    envs.addAll(config.getDockerEnvVariables());
    String dockerVncPort = String.valueOf(config.getDockerVncPort());
    if (config.isDockerEnabledVnc()) {
        envs.add("ENABLE_VNC=true");
        exposedPorts.add(dockerVncPort);
    }
    if (androidEnabled) {
        envs.add("QTWEBENGINE_DISABLE_SANDBOX=1");
    }
    if (isChromeAllowedOrigins(dockerImage, browserVersion)) {
        envs.add("DRIVER_ARGS=--whitelisted-ips= --allowed-origins=*");
    }
    // network
    String network = config.getDockerNetwork();
    // extra hosts
    List<String> extraHosts = config.getDockerExtraHosts();
    // builder
    DockerBuilder dockerBuilder = DockerContainer.dockerBuilder(dockerImage).exposedPorts(exposedPorts).network(network).mounts(mounts).binds(binds).shmSize(shmSize).envs(envs).extraHosts(extraHosts).sysadmin();
    if (androidEnabled) {
        dockerBuilder = dockerBuilder.privileged();
    }
    DockerContainer browserContainer = dockerBuilder.build();
    String containerId = startContainer(browserContainer);
    browserContainer.setContainerId(containerId);
    String browserHost = getHost(containerId, network);
    String browserPort = getBindPort(containerId, dockerBrowserPort + "/tcp");
    String browserUrlFormat = "http://%s:%s/";
    if (dockerImage.contains("firefox") || androidEnabled) {
        browserUrlFormat += "wd/hub";
    }
    String browserUrl = format(browserUrlFormat, browserHost, browserPort);
    browserContainer.setContainerUrl(browserUrl);
    String gateway = getGateway(containerId, network);
    browserContainer.setGateway(gateway);
    String address = getAddress(containerId, network);
    browserContainer.setAddress(address);
    log.trace("Browser remote URL {}", browserUrl);
    if (config.isDockerEnabledVnc()) {
        String vncPort = getBindPort(containerId, dockerVncPort + "/tcp");
        browserContainer.setVncPort(vncPort);
        String vncAddress = format("vnc://%s:%s/", getDefaultHost(), vncPort);
        log.debug("VNC server URL: {}", vncAddress);
        browserContainer.setVncAddress(vncAddress);
    }
    return browserContainer;
}
Also used : DockerBuilder(io.github.bonigarcia.wdm.docker.DockerContainer.DockerBuilder) ArrayList(java.util.ArrayList) Mount(com.github.dockerjava.api.model.Mount) TmpfsOptions(com.github.dockerjava.api.model.TmpfsOptions)

Aggregations

Mount (com.github.dockerjava.api.model.Mount)1 TmpfsOptions (com.github.dockerjava.api.model.TmpfsOptions)1 DockerBuilder (io.github.bonigarcia.wdm.docker.DockerContainer.DockerBuilder)1 ArrayList (java.util.ArrayList)1