spring 如何使用Testcontainers构建Dockerfile?

luaexgnf  于 2023-11-16  发布在  Spring
关注(0)|答案(1)|浏览(111)

我不太了解Docker,也许我做错了什么?但一切似乎都遵循了以下指令:https://java.testcontainers.org/supported_docker_environment/continuous_integration/dind_patterns/
在本地构建时,一切都很顺利。但是当我尝试构建Docker镜像时,弹出一个错误:

2023-10-21T06:35:22.415+0000 [DEBUG] [TestEventLogger] 
2023-10-21T06:35:22.416+0000 [DEBUG] [TestEventLogger]       .   ____          _            __ _ _
2023-10-21T06:35:22.416+0000 [DEBUG] [TestEventLogger]      /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
2023-10-21T06:35:22.416+0000 [DEBUG] [TestEventLogger]     ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2023-10-21T06:35:22.417+0000 [DEBUG] [TestEventLogger]      \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
2023-10-21T06:35:22.417+0000 [DEBUG] [TestEventLogger]       '  |____| .__|_| |_|_| |_\__, | / / / /
2023-10-21T06:35:22.418+0000 [DEBUG] [TestEventLogger]      =========|_|==============|___/=/_/_/_/
2023-10-21T06:35:22.419+0000 [DEBUG] [TestEventLogger]      :: Spring Boot ::                (v3.1.4)
2023-10-21T06:35:22.420+0000 [DEBUG] [TestEventLogger] 
2023-10-21T06:35:22.691+0000 [DEBUG] [TestEventLogger]     2023-10-21T06:35:22.690Z  INFO 231 --- [ers-lifecycle-0] .t.d.DockerMachineClientProviderStrategy : docker-machine executable was not found on PATH ([/opt/java/openjdk/bin, /usr/local/sbin, /usr/local/bin, /usr/sbin, /usr/bin, /sbin, /bin])
2023-10-21T06:35:22.691+0000 [DEBUG] [TestEventLogger]     2023-10-21T06:35:22.691Z ERROR 231 --- [ers-lifecycle-0] o.t.d.DockerClientProviderStrategy       : Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
2023-10-21T06:35:22.692+0000 [DEBUG] [TestEventLogger]          UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (Could not find unix domain socket). Root cause NoSuchFileException (/var/run/docker.sock)
2023-10-21T06:35:22.692+0000 [DEBUG] [TestEventLogger]          DockerDesktopClientProviderStrategy: failed with exception NullPointerException (Cannot invoke "java.nio.file.Path.toString()" because the return value of "org.testcontainers.dockerclient.DockerDesktopClientProviderStrategy.getSocketPath()" is null)As no valid configuration was found, execution cannot continue.
2023-10-21T06:35:22.692+0000 [DEBUG] [TestEventLogger]     See https://java.testcontainers.org/on_failure.html for more details.
2023-10-21T06:35:22.777+0000 [DEBUG] [TestEventLogger]     2023-10-21T06:35:22.773Z ERROR 231 --- [    Test worker] o.s.boot.SpringApplication               : Application run failed
2023-10-21T06:35:22.777+0000 [DEBUG] [TestEventLogger] 
2023-10-21T06:35:22.777+0000 [DEBUG] [TestEventLogger]     java.util.concurrent.CompletionException: java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration

字符串
我的Dockerfile:

ARG BUILD_HOME=/inquiry-service-example
ARG DEFAULT_APP_NAME=inquiry-service-0.0.1-SNAPSHOT-boot

FROM gradle:jdk17 as build-image

ARG BUILD_HOME
ARG DEFAULT_APP_NAME
ENV APP_HOME=$BUILD_HOME
WORKDIR $APP_HOME

COPY build.gradle $APP_HOME
COPY settings.gradle $APP_HOME
COPY src $APP_HOME/src
COPY config $APP_HOME/config

ENV ESTCONTAINERS_HOST_OVERRIDE=host.docker.internal
STOPSIGNAL SIGKILL
VOLUME /var/run/docker.sock:/var/run/docker.sock

RUN gradle build --debug

FROM openjdk:17

ARG BUILD_HOME
ARG DEFAULT_APP_NAME
ENV APP_HOME=$BUILD_HOME
ENV APP_NAME=$DEFAULT_APP_NAME

COPY --from=build-image $APP_HOME/build/libs/$APP_NAME.jar app.jar

EXPOSE 8080 8443

ENTRYPOINT java -jar app.jar


我的测试类:

package com.iprody.inquiryservice;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.lifecycle.Startables;

@SpringBootTest(
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
        properties = {

        }
)
public abstract class AbstractIntegrationTest {

    private static final int POSTGRES_SQL_PORT = 5432;
    private static final PostgreSQLContainer<?> POSTGRES_SQL_CONTAINER;

    static {
        final String root = "root";
        POSTGRES_SQL_CONTAINER = new PostgreSQLContainer<>("postgres:alpine")
                .withExposedPorts(POSTGRES_SQL_PORT)
                .withPassword(root)
                .withUsername(root)
                .withReuse(true);
    }

    @DynamicPropertySource
    static void postgresProperties(DynamicPropertyRegistry registry) {
        Startables.deepStart(POSTGRES_SQL_CONTAINER).join();

        registry.add("spring.datasource.url", POSTGRES_SQL_CONTAINER::getJdbcUrl);
        registry.add("spring.datasource.username", POSTGRES_SQL_CONTAINER::getUsername);
        registry.add("spring.datasource.password", POSTGRES_SQL_CONTAINER::getPassword);
    }
}


我以为Windows有问题,但在Linux上也是同样的错误。

6jjcrrmo

6jjcrrmo1#

我发现你的帖子,因为我有类似的问题“找不到一个有效的Docker环境”.我有完全相同的信息,你与docker-machine executable was not found等.在你的dockerfile,有一个错字.而不是ESTCONTAINERS_HOST_OVERRIDE你应该有TESTCONTAINERS_HOST_OVERRIDE.我有正确的变量名,但它仍然不工作.

相关问题