Running a background process in container during one step in docker build

Tobias Hermann picture Tobias Hermann · May 7, 2018 · Viewed 9.8k times · Source

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"]

Answer

Tobias Hermann picture Tobias Hermann · May 7, 2018

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"]