com.github.dockerjava.api.exception.InternalServerErrorException Java Examples

The following examples show how to use com.github.dockerjava.api.exception.InternalServerErrorException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DockerClientFactory.java    From testcontainers-java with MIT License 6 votes vote down vote up
private <T> T runInsideDocker(DockerClient client, Consumer<CreateContainerCmd> createContainerCmdConsumer, BiFunction<DockerClient, String, T> block) {
    checkAndPullImage(client, TINY_IMAGE);
    CreateContainerCmd createContainerCmd = client.createContainerCmd(TINY_IMAGE)
            .withLabels(DEFAULT_LABELS);
    createContainerCmdConsumer.accept(createContainerCmd);
    String id = createContainerCmd.exec().getId();

    try {
        client.startContainerCmd(id).exec();
        return block.apply(client, id);
    } finally {
        try {
            client.removeContainerCmd(id).withRemoveVolumes(true).withForce(true).exec();
        } catch (NotFoundException | InternalServerErrorException e) {
            log.debug("Swallowed exception while removing container", e);
        }
    }
}
 
Example #2
Source File: ResponseStatusExceptionFilter.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
    int status = responseContext.getStatus();
    switch (status) {
        case 200:
        case 201:
        case 204:
            return;
        case 304:
            throw new NotModifiedException(getBodyAsMessage(responseContext));
        case 400:
            throw new BadRequestException(getBodyAsMessage(responseContext));
        case 401:
            throw new UnauthorizedException(getBodyAsMessage(responseContext));
        case 404:
            throw new NotFoundException(getBodyAsMessage(responseContext));
        case 406:
            throw new NotAcceptableException(getBodyAsMessage(responseContext));
        case 409:
            throw new ConflictException(getBodyAsMessage(responseContext));
        case 500:
            throw new InternalServerErrorException(getBodyAsMessage(responseContext));
        default:
            throw new DockerException(getBodyAsMessage(responseContext), status);
    }
}
 
Example #3
Source File: StartContainerCmdIT.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Test(expected = InternalServerErrorException.class)
public void startContainerWithConflictingPortBindings() throws DockerException {

    ExposedPort tcp22 = ExposedPort.tcp(22);
    ExposedPort tcp23 = ExposedPort.tcp(23);

    Ports portBindings = new Ports();
    portBindings.bind(tcp22, Binding.bindPort(11022));
    portBindings.bind(tcp23, Binding.bindPort(11022));

    CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("true")
            .withExposedPorts(tcp22, tcp23).withHostConfig(newHostConfig()
                    .withPortBindings(portBindings))
            .exec();

    LOG.info("Created container {}", container.toString());

    assertThat(container.getId(), not(is(emptyString())));

    dockerRule.getClient().startContainerCmd(container.getId()).exec();
}
 
Example #4
Source File: DockerfileFixture.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Override
public void close() throws Exception {

    if (containerId != null) {
        LOGGER.info("removing container {}", containerId);
        try {
            client.removeContainerCmd(containerId).withForce(true) // stop too
                    .exec();
        } catch (NotFoundException | InternalServerErrorException ignored) {
            LOGGER.info("ignoring {}", ignored.getMessage());
        }
        containerId = null;
    }

    if (repository != null) {
        LOGGER.info("removing repository {}", repository);
        try {
            client.removeImageCmd(repository).withForce(true).exec();
        } catch (DockerException e) {
            LOGGER.info("ignoring {}", e.getMessage());
        }
        repository = null;
    }
}
 
Example #5
Source File: MesosCluster.java    From minimesos with Apache License 2.0 5 votes vote down vote up
public void waitForState(final Predicate<State> predicate) {
    await("Mesos master startup" + clusterConfig.getTimeout()).atMost(clusterConfig.getTimeout(), TimeUnit.SECONDS).until(() -> {
        try {
            assertTrue(predicate.test(State.fromJSON(getMaster().getStateInfoJSON().toString())));
        } catch (InternalServerErrorException | JsonParseException | UnirestException | JsonMappingException e) { //NOSONAR
            throw new AssertionError("Mesos master did not start after " + clusterConfig.getTimeout(), e);
        }
    });
}
 
Example #6
Source File: UnpauseCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test(expected = InternalServerErrorException.class)
public void unpauseRunningContainer() {

    CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999").exec();
    LOG.info("Created container: {}", container.toString());
    assertThat(container.getId(), not(is(emptyString())));

    ContainerUtils.startContainer(dockerRule.getClient(), container);

    dockerRule.getClient().unpauseContainerCmd(container.getId()).exec();
}
 
Example #7
Source File: UnpauseCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test(expected = InternalServerErrorException.class)
public void unpauseCreatedContainer() {

    CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999").exec();
    LOG.info("Created container: {}", container.toString());
    assertThat(container.getId(), not(is(emptyString())));

    dockerRule.getClient().unpauseContainerCmd(container.getId()).exec();
}
 
Example #8
Source File: UnpauseCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test(expected = InternalServerErrorException.class)
public void unpauseUnpausedContainer() {

    CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999").exec();
    LOG.info("Created container: {}", container.toString());
    assertThat(container.getId(), not(is(emptyString())));

    ContainerUtils.startContainer(dockerRule.getClient(), container);

    ContainerUtils.pauseContainer(dockerRule.getClient(), container);

    dockerRule.getClient().unpauseContainerCmd(container.getId()).exec();
    dockerRule.getClient().unpauseContainerCmd(container.getId()).exec();
}
 
Example #9
Source File: CreateContainerCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test
public void createContainerFromPrivateRegistryWithNoAuth() throws Exception {
    AuthConfig authConfig = REGISTRY.getAuthConfig();

    String imgName = REGISTRY.createPrivateImage("create-container-with-no-auth");

    if (TestUtils.isSwarm(dockerRule.getClient())) {
        exception.expect(instanceOf(InternalServerErrorException.class));
    } else {
        exception.expect(instanceOf(NotFoundException.class));
    }

    dockerRule.getClient().createContainerCmd(imgName)
            .exec();
}
 
Example #10
Source File: DefaultInvocationBuilder.java    From docker-java with Apache License 2.0 5 votes vote down vote up
protected DockerHttpClient.Response execute(DockerHttpClient.Request request) {
    try {
        DockerHttpClient.Response response = dockerHttpClient.execute(request);
        int statusCode = response.getStatusCode();
        if (statusCode < 200 || statusCode > 299) {
            try {
                String body = IOUtils.toString(response.getBody(), StandardCharsets.UTF_8);
                switch (statusCode) {
                    case 304:
                        throw new NotModifiedException(body);
                    case 400:
                        throw new BadRequestException(body);
                    case 401:
                        throw new UnauthorizedException(body);
                    case 404:
                        throw new NotFoundException(body);
                    case 406:
                        throw new NotAcceptableException(body);
                    case 409:
                        throw new ConflictException(body);
                    case 500:
                        throw new InternalServerErrorException(body);
                    default:
                        throw new DockerException(body, statusCode);
                }
            } finally {
                response.close();
            }
        } else {
            return response;
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #11
Source File: HttpResponseHandler.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
protected void channelRead0(final ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    if (msg instanceof HttpResponse) {

        response = (HttpResponse) msg;

        resultCallback.onStart(() -> ctx.channel().close());

    } else if (msg instanceof HttpContent) {

        HttpContent content = (HttpContent) msg;

        ByteBuf byteBuf = content.content();

        switch (response.status().code()) {
            case 200:
            case 201:
            case 204:
                ctx.fireChannelRead(byteBuf);
                break;
            default:
                errorBody.writeBytes(byteBuf);
        }

        if (content instanceof LastHttpContent) {
            try {

                switch (response.status().code()) {
                    case 101:
                    case 200:
                    case 201:
                    case 204:
                        break;
                    case 301:
                    case 302:
                        if (response.headers().contains(HttpHeaderNames.LOCATION)) {
                            String location = response.headers().get(HttpHeaderNames.LOCATION);
                            HttpRequest redirected = requestProvider.getHttpRequest(location);

                            ctx.channel().writeAndFlush(redirected);
                        }
                        break;
                    case 304:
                        throw new NotModifiedException(getBodyAsMessage(errorBody));
                    case 400:
                        throw new BadRequestException(getBodyAsMessage(errorBody));
                    case 401:
                        throw new UnauthorizedException(getBodyAsMessage(errorBody));
                    case 404:
                        throw new NotFoundException(getBodyAsMessage(errorBody));
                    case 406:
                        throw new NotAcceptableException(getBodyAsMessage(errorBody));
                    case 409:
                        throw new ConflictException(getBodyAsMessage(errorBody));
                    case 500:
                        throw new InternalServerErrorException(getBodyAsMessage(errorBody));
                    default:
                        throw new DockerException(getBodyAsMessage(errorBody), response.status().code());
                }
            } catch (Throwable e) {
                resultCallback.onError(e);
            } finally {
                resultCallback.onComplete();
            }
        }
    }
}
 
Example #12
Source File: UnpauseCmdIT.java    From docker-java with Apache License 2.0 3 votes vote down vote up
@Test(expected = InternalServerErrorException.class)
public void unpauseStoppedContainer() {

    CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999").exec();
    LOG.info("Created container: {}", container.toString());
    assertThat(container.getId(), not(is(emptyString())));

    ContainerUtils.startContainer(dockerRule.getClient(), container);

    ContainerUtils.stopContainer(dockerRule.getClient(), container);

    dockerRule.getClient().unpauseContainerCmd(container.getId()).exec();
}