How to run Spring Boot tests in a docker container?

Hi there,

I’m quite new to docker so this question might involve some basic docker concept that I still don’t get.
I have created 2 containers:

  • Spring Boot Application (that communicates with…)
  • MySql Instance

This is my Dockerfile:

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
COPY build/libs/spring-boot-docker-demo-0.1.0.jar /app.jar
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev././urandom -Dspring.profiles.active=container -jar /app.jar" ]

The application works great when I spin it up using this docker-compose.yml file:

version: '2'
services:
  dockerapp:
    build: .
    environment:
      - DATABASE_HOST=db
      - DATABASE_PORT=3306
      - DATABASE_NAME=test_db
      - DATABASE_USER=root
      - DATABASE_PASSWORD=root
    ports:
      - "8081:8081"

  db:
    image: mysql:5.7
    environment:
       - MYSQL_ROOT_PASSWORD=root
       - MYSQL_DATABASE=college_app_db

What I’m trying to accomplish is to execute spring boot tests and have them use the mysql instance in that separate container.

This was my first approach:

docker run -d --name test_mysql -p "3306:3306" --env="MYSQL_ROOT_PASSWORD=xxx" --env="MYSQL_DATABASE=test_db" mysql:5.7
./gradlew test -PactiveSpringProfile=remoteMysqlDocker

This only works on my host machine. Instead of running gradlew directly I somehow need to spin up another container that contains my app and ONLY starts the tests. This app container would use the hostname “test_mysql” to access the mysql database.

I’m not sure whether it might be better to create a docker image for my app that contains some sort of
os (e.g. debian) where I then could start a bash script to start the tests or start the application.

I hope my explanation is not confusing.

Thanks a lot!