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"]
Run in detached mode Docker can run your container in detached mode, that is in the background. To do this, we can use the --detach or -d for short. Docker will start your container the same as before but this time will “detach” from the container and return you to the terminal prompt.
you can run any command in a running container just knowing its ID (or name): docker exec -it <container_id_or_name> echo "I'm inside the container!" Note:The container needs to be running. If you're walking the boxes on the equal host then you may execute docker commands in the field.
If you need to run multiple processes, it's generally recommended to place each process in its own container. You can run multiple containers manually, or you can use an orchestration tool like Docker Compose, or Kubernetes (check out our page of awesome Kubernetes learning resources).
It's ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application. You can connect multiple containers using user-defined networks and shared volumes.
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"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With