14

I'd like to run integration tests of an app during docker build. These tests require a Redis server being available.

How can I run redis-server and keep it running in the background during the integration-test step, i.e. gradle build?

Here is the essence of my Dockerfile:

FROM ubuntu:16.04

# apt-get install stuff
# ...
# install gradle
# build and install redis

WORKDIR /app
ADD . /app

# TODO: start redis-server

# run unit tests / integration tests of app
RUN /opt/gradle/gradle-4.6/bin/gradle build --info

# TODO: stop redis-server

# build app
RUN ./gradlew assemble

# start app with
# docker run
CMD ["java", "-jar", "my_app.jar"]
halfer
  • 19,824
  • 17
  • 99
  • 186
Tobias Hermann
  • 9,936
  • 6
  • 61
  • 134
  • 5
    I wouldn't do it this way. I would produce the image and then take the image through a CI process, in which it will only be pushed to your image registry if it passes the tests. That will allow you to spin up a separate Redis image, rather than installing Redis in an image where it doesn't belong. – halfer May 07 '18 at 09:59

1 Answers1

19

As halfer states in his comment, this is not good practice.

However for completeness I want to share a solution to the original question nevertheless:

RUN nohup bash -c "redis-server &" && sleep 4 && /opt/gradle/gradle-4.6/bin/gradle build --info

This runs redis-server only for this single layer. The sleep 4 is just there to give redis enough time start up.

So the Dockerfile then looks as follows:

FROM ubuntu:16.04

# apt-get install stuff
# ...
# install gradle
# build and install redis

WORKDIR /app
ADD . /app

# run unit tests / integration tests of app
RUN nohup bash -c "redis-server &" && sleep 4 && /opt/gradle/gradle-4.6/bin/gradle build --info

# TODO: uninstall redis

# build app
RUN ./gradlew assemble

# start app with
# docker run
CMD ["java", "-jar", "my_app.jar"]
Tobias Hermann
  • 9,936
  • 6
  • 61
  • 134