我想在docker build
期间运行应用的集成测试。这些测试需要Redis服务器可用。
如何在集成测试步骤中运行redis-server
并使其在后台运行,即gradle build
?
以下是我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"]
答案 0 :(得分:5)
正如halfer中的his comment所述,这不是一个好习惯。
然而,为了完整性,我想分享原始问题的解决方案:
RUN nohup bash -c "redis-server &" && sleep 4 && /opt/gradle/gradle-4.6/bin/gradle build --info
仅对此单层运行redis-server
。 sleep 4
只是为了给redis足够的时间启动。
所以Dockerfile
看起来如下:
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"]