Docker Community Forums

Share and learn in the Docker community.

How to use docker inside Swarm service

I am using docker swarm for my continuous integration infrastructure. I have Jenkins as one service inside the swarm. Jenkins is running on one of my worker nodes. I am using docker-swarm-plugin to enable jenkins to launch a new swarm build agent service for every job build.

Now I want to make two new jenkins jobs:
(1) a job that builds some images
(2) a job that launches services in my docker swarm, using the images built in the previous job

My questions:

  • How do I enable jenkins (which is running as service inside the swarm) to execute docker commands such as docker build?
  • How can I use the swarm managers docker deamon from my jenkins service (which is running as service inside the swarm) to launch services in the swarm (next to jenkins itself)?

I hope my question is clear. Any tips?

I just found the solution and wanted to share it with you:
In the compose file aof my jenkins swarm service, I added a bind mount:
/usr/bin/docker:/usr/bin/docker and an environment variable DOCKER_HOST=tcp://<my-swarm manager-host>.<my-domain>:2376. Then jenkins container can run docker commands with docker daemon of the swarm manager. YOu can the forward the environment variable (ENV) and the bind volume (host binds) with the docker-swarm-plugin to your build agents.