在docker build中的一个步骤中在容器中运行后台进程

时间:2018-05-07 09:43:16

标签: ubuntu docker dockerfile background-process docker-build

我想在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"]

1 个答案:

答案 0 :(得分:5)

正如halfer中的his comment所述,这不是一个好习惯。

然而,为了完整性,我想分享原始问题的解决方案:

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

仅对此单层运行redis-serversleep 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"]