2

I am following this tutorial to build a hello-world maven java app with jenkins in dockers: https://jenkins.io/doc/tutorials/building-a-java-app-with-maven/#fork-and-clone-the-sample-repository-on-github

This is my app (just fork form the tutorial): simple-java-maven-app

It has a small difference that I used remote repo (Github) not a local repo (or host repo) in Pipeline's option (Repository URL). I pushed Jenkinsfile to repo then build hello-world app with Pipeline.

// Jenkinsfile for Pipeline
pipeline {
    agent {
        docker {
            image 'maven:3-alpine' 
            args '-v /root/.m2:/root/.m2' 
        }
    }
    stages {
        stage('Build') { 
            steps {
                sh 'mvn -B -DskipTests clean package' 
            }
        }
    }
}

I got this error below and can not find any solution for it. I using Windows 10.

docker pull maven:3-alpine
Warning: failed to get default registry endpoint from daemon (Cannot connect to the Docker daemon. Is the docker daemon running on this host?). Using system default: https://index.docker.io/v1/
Cannot connect to the Docker daemon. Is the docker daemon running on this host?

Thanks everyone.

dungmidside
  • 508
  • 7
  • 19

1 Answers1

4

You are using Docker in Docker (DinD) which is not a recommended CI approach. You should mount your Host's Docker Daemon Socket as volume to the Jenkins container like:

docker container run -v /var/run/docker.sock:/var/run/docker.sock ...

So you can work with images & containers on your host machine rather than in the Jenkins container. Click Here to learn more.

Janshair Khan
  • 2,577
  • 4
  • 20
  • 44
  • 3
    What do you mean "not a recommended CI approach". DinD is designed for this purpose, basically. Exposing the containing host's socket to the internal docker allows docker containers to essentially take over the host's docker. The blogger's concerns are relevant only when running multiple docker CI builds in parallel on the same host. – Otheus Feb 07 '18 at 18:02
  • Also, don't forget to [ensure that the `jenkins` user can write to the `docker.socket`](https://stackoverflow.com/questions/47854463/got-permission-denied-while-trying-to-connect-to-the-docker-daemon-socket-at-uni) – Raedwald Apr 05 '19 at 16:00
  • > So you can work with images & containers on your host machine rather than in the Jenkins container. Exactly! And with -v /var/run/docker.sock you will have the same problems with concurrency and common for all your containers images, build cache, networks, etc. DinD's purpose is really isolated environments. – Dzenly Jun 02 '21 at 06:04
  • One major downside of mounting the hosts socket in - my opinion - is that you cannot mount anything from your working directory anymore while building. Buildpacks uses this, for example, in their packs to get files, like certificates into the container. – Max N. Jul 07 '23 at 15:34