You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

构建集成Testcontainers的Dockerfile时遇Docker环境检测失败求助

问题:Testcontainers无法找到Docker环境导致构建失败

问题描述

我对Docker了解不多,操作可能有误,但所有步骤均遵循官方文档。本地构建一切正常,但尝试构建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环境下也出现相同错误,请求帮助解决。


内容的提问来源于stack exchange,提问作者Grodasan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 05:33:23