2

I try to create docker image and run a container but maven build fails due failing the tests with testcontainers. Also should say that Im a windows user, but there is a Ubuntu-22.04 over Windows 10. Docker successfully finds WSL2 in settings

P.S. tests are passed if I run mvn clean package/clean install or manually start them

When I run docker build -t *someName* OR docker-compose up --build app I faced with this (most useful as I think) stacktrace after successful dependencies downloading:

#0 78.13 12:55:52.042 [main] INFO org.testcontainers.utility.ImageNameSubstitutor - Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImag eNameSubstitutor') #0 78.18 12:55:52.093 [main] DEBUG org.testcontainers.dockerclient.RootlessDockerClientProviderStrategy - $XDG_RUNTIME_DIR is not set. #0 78.18 12:55:52.094 [main] DEBUG org.testcontainers.dockerclient.RootlessDockerClientProviderStrategy - '/root/.docker/run' does not exist. #0 78.24 12:55:52.151 [main] DEBUG org.testcontainers.dockerclient.RootlessDockerClientProviderStrategy - '/run/user/0' does not exist. #0 78.24 12:55:52.151 [main] DEBUG org.testcontainers.dockerclient.DockerClientProviderStrategy - Trying out strategy: UnixSocketClientProviderStrategy #0 78.24 12:55:52.154 [main] DEBUG org.testcontainers.dockerclient.DockerClientProviderStrategy - UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (Could not find unix domain socket). Root caus e NoSuchFileException (/var/run/docker.sock) #0 78.25 12:55:52.157 [main] INFO org.testcontainers.dockerclient.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]) #0 78.25 12:55:52.158 [main] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy - Could not find a valid Docker environment. Please check configuration. Attempted configurations were: #0 78.25 UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (Could not find unix domain socket). Root cause NoSuchFileException (/var/run/docker.sock)As no valid configuration was found, e xecution cannot continue. #0 78.25 See https://www.testcontainers.org/on_failure.html for more details.

===Dockerfile===

FROM maven:3.8.5 AS maven

WORKDIR /usr/src/app
COPY . /usr/src/app

RUN mvn clean package

FROM openjdk:17-jdk-slim

ARG JAR_FILE=practise.jar

WORKDIR /opt/app

COPY --from=maven /usr/src/app/target/${JAR_FILE} /opt/app/

ENTRYPOINT \["java","-jar","practise.jar"\]
EXPOSE 8080

===docker-compose.yml===

version: '3.1'
    services:
        app:
            build: .
                image: 'practise'
            ports:
                "8080:8080"
            links:
                postgresdb
            environment:
         
  SPRING_DATASOURCE_URL=jdbc:postgresql://postgresdb:5432/practise
               SPRING_DATASOURCE_USERNAME=root
               SPRING_DATASOURCE_PASSWORD=root
               SPRING_JPA_HIBERNATE_DDL_AUTO=update

         postgresdb:
             image: 'postgres:13.1-alpine'
             ports:
                 "5432:5432"
             expose:
                 5432
             environment:
                 POSTGRES_PASSWORD=root
                 POSTGRES_USER=root
                 POSTGRES_DB=practise

There is a declaration of test class:

@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@Testcontainers
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class CompanyControllerTest {
@Container
public static final PostgreSQLContainer<?> container =
    new PostgreSQLContainer<>("postgres")
        .withUsername("root")
        .withPassword("root");

@DynamicPropertySource
static void properties(DynamicPropertyRegistry registry) {
    registry.add("hibernate.connection.url", container::getJdbcUrl);
    registry.add("hibernate.connection.username", container::getUsername);
    registry.add("hibernate.connection.password", container::getPassword);
}

// tests
}
AAL
  • 21
  • 2
  • 1
    If `mvn package` runs the JUnit tests, and the JUnit tests depend on Testcontainers, this can't work: inside the `docker build` environment you can't have access to the Docker socket. Can you avoid running tests as part of the build sequence? Instead of using Testcontainers, can you [use an in-memory database](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.testing.spring-boot-applications.autoconfigured-spring-data-jpa)? (I'd do both.) – David Maze Jan 25 '23 at 14:11
  • @DavidMaze thanks for your answer, ill skip tests in maven build until will find a right solution – AAL Jan 26 '23 at 08:23
  • Hi, @DavidMaze! I chose a modification of your solution and it is the following: I deleted Dockerfile and run my app only via docker-compose. And in configuration of docker-compose-file I deleted `image`-field. Now there aren't any execution of tests via docker. Thanks for the answer x2) – AAL Jan 27 '23 at 07:22
  • You need access to a Docker daemon during the test execution. One convenient way for this is using Testcontainers Cloud, as outlined in this SO answer: https://stackoverflow.com/a/75158846/1396362 – Kevin Wittek Jan 27 '23 at 12:06

0 Answers0