Search in sources :

Example 1 with ExposedPort

use of com.github.dockerjava.api.model.ExposedPort in project camel by apache.

the class CreateContainerCmdHeaderTest method createContainerHeaderTest.

@Test
public void createContainerHeaderTest() {
    String image = "busybox";
    ExposedPort exposedPort = ExposedPort.tcp(22);
    boolean tty = true;
    String name = "cameldocker";
    String workingDir = "/opt";
    boolean disableNetwork = false;
    String domainName = "apache.org";
    String hostname = "dockerjava";
    String user = "docker";
    boolean stdInOpen = false;
    boolean stdInOnce = false;
    boolean attachStdErr = true;
    boolean attachStdOut = true;
    boolean attachStdIn = false;
    Long memoryLimit = 2048L;
    Long swapMemory = 512L;
    Integer cpuShares = 512;
    Volume volumes = new Volume("/example");
    VolumesFrom volumesFromContainer = new VolumesFrom("/etc");
    String env = "FOO=bar";
    String cmd = "whoami";
    HostConfig hostConfig = new HostConfig();
    Capability capAdd = Capability.NET_BROADCAST;
    Capability capDrop = Capability.BLOCK_SUSPEND;
    String[] entrypoint = new String[] { "sleep", "9999" };
    String portSpecs = "80";
    String dns = "8.8.8.8";
    Map<String, Object> headers = getDefaultParameters();
    headers.put(DockerConstants.DOCKER_IMAGE, image);
    headers.put(DockerConstants.DOCKER_EXPOSED_PORTS, exposedPort);
    headers.put(DockerConstants.DOCKER_TTY, tty);
    headers.put(DockerConstants.DOCKER_NAME, name);
    headers.put(DockerConstants.DOCKER_WORKING_DIR, workingDir);
    headers.put(DockerConstants.DOCKER_DISABLE_NETWORK, disableNetwork);
    headers.put(DockerConstants.DOCKER_HOSTNAME, hostname);
    headers.put(DockerConstants.DOCKER_USER, user);
    headers.put(DockerConstants.DOCKER_STD_IN_OPEN, stdInOpen);
    headers.put(DockerConstants.DOCKER_STD_IN_ONCE, stdInOnce);
    headers.put(DockerConstants.DOCKER_ATTACH_STD_IN, attachStdIn);
    headers.put(DockerConstants.DOCKER_ATTACH_STD_ERR, attachStdErr);
    headers.put(DockerConstants.DOCKER_ATTACH_STD_OUT, attachStdOut);
    headers.put(DockerConstants.DOCKER_MEMORY_LIMIT, memoryLimit);
    headers.put(DockerConstants.DOCKER_MEMORY_SWAP, swapMemory);
    headers.put(DockerConstants.DOCKER_CPU_SHARES, cpuShares);
    headers.put(DockerConstants.DOCKER_VOLUMES, volumes);
    headers.put(DockerConstants.DOCKER_VOLUMES_FROM, volumesFromContainer);
    headers.put(DockerConstants.DOCKER_ENV, env);
    headers.put(DockerConstants.DOCKER_CMD, cmd);
    headers.put(DockerConstants.DOCKER_HOST_CONFIG, hostConfig);
    headers.put(DockerConstants.DOCKER_CAP_ADD, capAdd);
    headers.put(DockerConstants.DOCKER_CAP_DROP, capDrop);
    headers.put(DockerConstants.DOCKER_ENTRYPOINT, entrypoint);
    headers.put(DockerConstants.DOCKER_PORT_SPECS, portSpecs);
    headers.put(DockerConstants.DOCKER_DNS, dns);
    headers.put(DockerConstants.DOCKER_DOMAIN_NAME, domainName);
    template.sendBodyAndHeaders("direct:in", "", headers);
    Mockito.verify(dockerClient, Mockito.times(1)).createContainerCmd(image);
    Mockito.verify(mockObject, Mockito.times(1)).withExposedPorts(Matchers.eq(exposedPort));
    Mockito.verify(mockObject, Mockito.times(1)).withTty(Matchers.eq(tty));
    Mockito.verify(mockObject, Mockito.times(1)).withName(Matchers.eq(name));
    Mockito.verify(mockObject, Mockito.times(1)).withWorkingDir(workingDir);
    Mockito.verify(mockObject, Mockito.times(1)).withNetworkDisabled(disableNetwork);
    Mockito.verify(mockObject, Mockito.times(1)).withHostName(hostname);
    Mockito.verify(mockObject, Mockito.times(1)).withUser(user);
    Mockito.verify(mockObject, Mockito.times(1)).withStdinOpen(stdInOpen);
    Mockito.verify(mockObject, Mockito.times(1)).withStdInOnce(stdInOnce);
    Mockito.verify(mockObject, Mockito.times(1)).withAttachStderr(attachStdErr);
    Mockito.verify(mockObject, Mockito.times(1)).withAttachStdin(attachStdIn);
    Mockito.verify(mockObject, Mockito.times(1)).withAttachStdout(attachStdOut);
    Mockito.verify(mockObject, Mockito.times(1)).withMemory(memoryLimit);
    Mockito.verify(mockObject, Mockito.times(1)).withMemorySwap(swapMemory);
    Mockito.verify(mockObject, Mockito.times(1)).withCpuShares(cpuShares);
    Mockito.verify(mockObject, Mockito.times(1)).withVolumes(volumes);
    Mockito.verify(mockObject, Mockito.times(1)).withVolumesFrom(volumesFromContainer);
    Mockito.verify(mockObject, Mockito.times(1)).withEnv(env);
    Mockito.verify(mockObject, Mockito.times(1)).withCmd(cmd);
    Mockito.verify(mockObject, Mockito.times(1)).withHostConfig(hostConfig);
    Mockito.verify(mockObject, Mockito.times(1)).withCapAdd(capAdd);
    Mockito.verify(mockObject, Mockito.times(1)).withCapDrop(capDrop);
    Mockito.verify(mockObject, Mockito.times(1)).withEntrypoint(entrypoint);
    Mockito.verify(mockObject, Mockito.times(1)).withPortSpecs(portSpecs);
    Mockito.verify(mockObject, Mockito.times(1)).withDns(dns);
    Mockito.verify(mockObject, Mockito.times(1)).withDomainName(domainName);
}
Also used : ExposedPort(com.github.dockerjava.api.model.ExposedPort) Capability(com.github.dockerjava.api.model.Capability) Volume(com.github.dockerjava.api.model.Volume) VolumesFrom(com.github.dockerjava.api.model.VolumesFrom) HostConfig(com.github.dockerjava.api.model.HostConfig) Test(org.junit.Test)

Example 2 with ExposedPort

use of com.github.dockerjava.api.model.ExposedPort in project dockerunit by qzagarese.

the class ConsulDescriptor method setup.

@ContainerBuilder
public CreateContainerCmd setup(CreateContainerCmd cmd) {
    List<ExposedPort> ports = new ArrayList<>(Arrays.asList(cmd.getExposedPorts()));
    ExposedPort dnsPort = ExposedPort.udp(CONSUL_DNS_PORT);
    ports.add(dnsPort);
    ExposedPort consulPort = ExposedPort.tcp(CONSUL_PORT);
    ports.add(consulPort);
    Ports bindings = cmd.getPortBindings();
    if (bindings == null) {
        bindings = new Ports();
    }
    bindings.bind(dnsPort, Binding.bindPort(8600));
    bindings.bind(consulPort, Binding.bindPort(8500));
    return cmd.withExposedPorts(ports).withPortBindings(bindings).withCmd("agent", "-dev", "-client=0.0.0.0", "-enable-script-checks");
}
Also used : ExposedPort(com.github.dockerjava.api.model.ExposedPort) ArrayList(java.util.ArrayList) Ports(com.github.dockerjava.api.model.Ports) ContainerBuilder(com.github.qzagarese.dockerunit.annotation.ContainerBuilder)

Example 3 with ExposedPort

use of com.github.dockerjava.api.model.ExposedPort in project dockerunit by qzagarese.

the class PortBindingExtensionInterpreter method build.

@Override
public CreateContainerCmd build(TestDescriptor td, CreateContainerCmd cmd, PortBinding pb) {
    List<ExposedPort> ports = new ArrayList<>(Arrays.asList(cmd.getExposedPorts()));
    ExposedPort containerPort = pb.protocol().equals(Protocol.TCP) ? ExposedPort.tcp(pb.exposedPort()) : ExposedPort.udp(pb.exposedPort());
    ports.add(containerPort);
    Ports bindings = cmd.getPortBindings();
    if (bindings == null) {
        bindings = new Ports();
    }
    bindings.bind(containerPort, pb.hostIp().isEmpty() ? Binding.bindPort(pb.hostPort()) : Binding.bindIpAndPort(pb.hostIp(), pb.hostPort()));
    return cmd.withExposedPorts(ports).withPortBindings(bindings);
}
Also used : ExposedPort(com.github.dockerjava.api.model.ExposedPort) ArrayList(java.util.ArrayList) Ports(com.github.dockerjava.api.model.Ports)

Example 4 with ExposedPort

use of com.github.dockerjava.api.model.ExposedPort in project elastest-torm by elastest.

the class DockerComposeService method setup.

public void setup() throws IOException, InterruptedException {
    // 1. Start docker-compose-ui container
    dockerComposeUiContainerName = dockerService.generateContainerName(dockerComposeUiPrefix);
    int dockerComposeBindPort = dockerService.findRandomOpenPort();
    Binding bindNoVncPort = bindPort(dockerComposeBindPort);
    ExposedPort exposedNoVncPort = tcp(dockerComposeUiPort);
    String dockerComposeServiceUrl = "http://" + dockerService.getDockerServerIp() + ":" + dockerComposeBindPort;
    log.debug("Starting docker-compose-ui container: {}", dockerComposeUiContainerName);
    List<PortBinding> portBindings = asList(new PortBinding(bindNoVncPort, exposedNoVncPort));
    Volume volume = new Volume(dockerDefaultSocket);
    List<Volume> volumes = asList(volume);
    List<Bind> binds = asList(new Bind(dockerDefaultSocket, volume));
    DockerBuilder dockerBuilder = dockerBuilder(dockerComposeUiImageId, dockerComposeUiContainerName).portBindings(portBindings).volumes(volumes).binds(binds);
    dockerService.startAndWaitContainer(dockerBuilder.build());
    // 2. Create Retrofit object to call docker-compose-ui REST API
    OkHttpClient okHttpClient = new OkHttpClient.Builder().readTimeout(dockerComposeTimeout, SECONDS).connectTimeout(dockerComposeTimeout, SECONDS).build();
    Retrofit retrofit = new Retrofit.Builder().client(okHttpClient).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create()).baseUrl(dockerComposeServiceUrl).build();
    dockerComposeApi = retrofit.create(DockerComposeApi.class);
    log.debug("docker-compose-ui up and running on URL: {}", dockerComposeServiceUrl);
    // 3.Delete default example projects
    dockerService.waitForHostIsReachable(dockerComposeServiceUrl);
    String[] defaultProjects = { "env-demo", "hello-node", "node-redis", "volumes-demo", "volumes-relative-paths" };
    removeProjects(defaultProjects);
}
Also used : PortBinding(com.github.dockerjava.api.model.PortBinding) Binding(com.github.dockerjava.api.model.Ports.Binding) DockerBuilder(io.elastest.epm.client.DockerContainer.DockerBuilder) Bind(com.github.dockerjava.api.model.Bind) OkHttpClient(okhttp3.OkHttpClient) DockerComposeApi(io.elastest.epm.client.DockerComposeApi) ExposedPort(com.github.dockerjava.api.model.ExposedPort) Retrofit(retrofit2.Retrofit) PortBinding(com.github.dockerjava.api.model.PortBinding) Volume(com.github.dockerjava.api.model.Volume)

Example 5 with ExposedPort

use of com.github.dockerjava.api.model.ExposedPort in project elastest-torm by elastest.

the class DockerUnitTest method testEmptyContainer.

@Test
@DisplayName("Try to start a container with invalid input")
void testEmptyContainer() {
    assertThrows(Exception.class, () -> {
        Binding bindPort = bindPort(0);
        ExposedPort exposedPort = tcp(0);
        List<PortBinding> portBindings = asList(new PortBinding(bindPort, exposedPort));
        dockerService.startAndWaitContainer(dockerBuilder("", "").portBindings(portBindings).build());
    });
}
Also used : PortBinding(com.github.dockerjava.api.model.PortBinding) Binding(com.github.dockerjava.api.model.Ports.Binding) ExposedPort(com.github.dockerjava.api.model.ExposedPort) PortBinding(com.github.dockerjava.api.model.PortBinding) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

ExposedPort (com.github.dockerjava.api.model.ExposedPort)12 ArrayList (java.util.ArrayList)6 PortBinding (com.github.dockerjava.api.model.PortBinding)5 Volume (com.github.dockerjava.api.model.Volume)5 Ports (com.github.dockerjava.api.model.Ports)4 InspectContainerResponse (com.github.dockerjava.api.command.InspectContainerResponse)3 Bind (com.github.dockerjava.api.model.Bind)3 HostConfig (com.github.dockerjava.api.model.HostConfig)3 Binding (com.github.dockerjava.api.model.Ports.Binding)3 VolumesFrom (com.github.dockerjava.api.model.VolumesFrom)3 IOException (java.io.IOException)3 DockerClient (com.github.dockerjava.api.DockerClient)2 CreateContainerCmd (com.github.dockerjava.api.command.CreateContainerCmd)2 Capability (com.github.dockerjava.api.model.Capability)2 DockerBuilder (io.elastest.epm.client.DockerContainer.DockerBuilder)2 Map (java.util.Map)2 DockerClientException (com.github.dockerjava.api.exception.DockerClientException)1 InternalServerErrorException (com.github.dockerjava.api.exception.InternalServerErrorException)1 NotFoundException (com.github.dockerjava.api.exception.NotFoundException)1 NotModifiedException (com.github.dockerjava.api.exception.NotModifiedException)1