I am building a java project inside a dockerfile using the maven package. At the same time, integration tests written using Testcontainers are launched. But the problem is that these tests require the creation of docker containers to deploy databases inside them. That's why I'm mounting docker.sock in docker-compose. I need to save the integration tests while optimizing the dockerfile somehow. Because at the moment the mvn package is launched every time the container is started, which is very expensive.
dockerfile:
FROM maven:3.8.1-openjdk-8-slim
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
ENV POSTGRES_PASSWORD=5e-Jo87_hhNf*9
ENV POSTGRES_USERNAME=postgres
ENV POSTGRES_HOST=postgres
ENV CASSANDRA_HOST=cassandra
EXPOSE 8080
COPY . .
ENTRYPOINT ["sh", "-c", "mvn clean package && java -jar /app/target/TechnicalTask_Boot-1.0-SNAPSHOT.jar"]
docker-compose.yaml:
services:
app:
build: ./TechnicalTask_Boot
volumes:
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
postgres:
condition: service_healthy
cassandra:
condition: service_healthy
ports:
- 8080:8080
postgres:
build: ./databases/Postgres
volumes:
- postgresdb:/var/lib/postgresql
healthcheck:
test:
- CMD-SHELL
- pg_isready -U postgres
interval: 1s
timeout: 3s
retries: 10
cassandra:
build: ./databases/Cassandra
volumes:
- cassandradb:/var/lib/cassandra
healthcheck:
test:
- CMD-SHELL
- cqlsh -e 'SELECT release_version FROM system.local;' || exit 1
interval: 1s
timeout: 3s
retries: 30
volumes:
postgresdb:
cassandradb: