How can I arrange to start and re-launch two interdependent docker containers?

I am using docker container as data volume just for my Jenkins CI server. Thus, in order to start the Jenkins service, I need to run two sequential commands:

docker run --name=jenkins_data -v /path/to/volume/:/var/jenkins_home busybox
docker run -d --name=jenkins_server -p 8081:8080 --volumes-from jenkins_data --restart="always" jenkins

      

How can I copy these two containers so that they start automatically? How to make sure the container is jenkins_data

running before starting jenkins_server

?

I tried to set --restart="always"

for the container jenkins_data

, but since it exits right after the first command docker run

, it starts up again every few seconds or so.

Basically, I would like to treat both containers as a service that starts automatically when the server boots.

+3


source to share


1 answer


First, you are not using a data-only container. Since you are actually mounting the host volumes, your data container doesn't really buy anything: you could easily replace it with --volumes-from

a second container using a command line option -v /path/to/volume:/var/jenkins_home

.

Second, you do not need to "run" the data container to reference it in --volumes-from

. Consider:

docker run --name mydata -v /data busybox true

      

This container exits immediately (because we are just starting true

). But now I can do this:



docker run --volumes-from mydata -it busybox sh

      

And I can see the volume /data

from the container mydata

:

/ # df -P |grep data
/dev/mapper/tank-docker  10190136    357972   9815780   4% /data

      

This means you don't have to worry about running multiple containers. As long as the data container exists, you can reference it in the --volumes-from

Jenkins container.

+4


source







All Articles