Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"]
like image 748
Tobias Hermann Avatar asked May 07 '18 09:05

Tobias Hermann


People also ask

How do I run a Docker container in background mode?

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.

How do you go inside a container if the container is running in the background?

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.

Can we run multiple process in a single container should we with reasons?

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).

Can I run two processes in Docker container?

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.


1 Answers

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"]
like image 121
Tobias Hermann Avatar answered Sep 30 '22 17:09

Tobias Hermann