简体   繁体   中英

Deploying Jenkins with docker in docker swarm

So I am trying to run jenkins inside my docker swarm and I want it to be able to execute docker commands to build new docker images.

When I am running it locally with the docker-compose script:

version: '2'
services:
  jenkins:
    build: ./jenkins
    image: munhunger/jenkins
    container_name: "jenkins"
    ports:
      - "81:8080"
    environment:
      - minio_url=<URL>
      - minio_access=<TOKEN>
      - minio_secret=<SECRET>
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /root/.jenkins/workspace:/root/.jenkins/workspace
      - /var/lib/jenkins:/var/lib/jenkins
      - /var/lib/docker:/var/lib/docker

It works exactly as I want it to and it can build the following jenkinsfile:

pipeline {
    agent any
    stages {
        stage('build war') {
            agent {
                docker { image 'gradle:latest' }
            }
            steps {
                sh 'gradle war -b oven/build.gradle'
            }
        }
        stage('build dockerimage') {
            steps {
                script {
                    dir('oven') {
                        def image = docker.build("munhunger/highly-oven")

                        docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-credentials') {
                            image.push("${env.BUILD_NUMBER}")
                            image.push("latest")
                        }
                    }
                }
            }
        }
    }
}

The problem is that it doesn't seem possible to deploy it to my docker swarm. I am just constantly getting invalid mount config for type "bind": bind source path does not exist . This is when using Portainer to deploy it. Have I misconfigured something or what is going on?

EDIT: I tested deploying the following compose file using docker stack deploy -c jenkins.yml jenkins

version: '3'
services:
  jenkins:
    image: munhunger/jenkins
    container_name: "jenkins"
    ports:
      - "81:8080"
    environment:
      - minio_url=<URL>
      - minio_access=<ACCESS>
      - minio_secret=<SECRET>
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /root/.jenkins/workspace:/root/.jenkins/workspace
      - /var/lib/jenkins:/var/lib/jenkins
      - /var/lib/docker:/var/lib/docker

But I am still getting invalid mount config for type "bind": bind source path does not exist

There's no reason to use bind-mounts or the docker socket to use Jenkins inside a docker container. It's fine to use named volumes to store your important (persistant) config data, but you can build images with the docker command without needing to use the host docker socket.

See https://hub.docker.com/r/jenkins/jenkins/

and https://hub.docker.com/_/docker/ (docker in docker)

and http://vfarcic.github.io/jenkins-swarm/#/cover

You need to use, docker compose version: '3'or greater to deploy services to swarm. https://docs.docker.com/engine/swarm/stack-deploy/

Use docker stack deploy to deploy the compose file - https://docs.docker.com/engine/swarm/stack-deploy/

Compose ref - https://docs.docker.com/compose/compose-file/

FYI - docker compose v3 doesn't support build , you need to feed images to the swarm in order to deploy it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM