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
}