Search code examples
windowsdockerjenkinswindows-subsystem-for-linux

Jenkins connect to the remote Docker Engine, build image failed


My problem is very similar to the situation here(DOCKER_HOST environment variable on windows), but a bit more complex.

I am learning CI/CD and my environment consists of Docker 1.47.0 on Windows 11 with WSL2 as the Docker engine, and a Spring Boot application using Maven 3.9.8 as the dependency manager. I am also using the fabric8io Docker Maven Plugin(https://github.com/fabric8io/docker-maven-plugin).

In my lesson, I need to create a Jenkins pipeline task that first retrieves my project from Git, then builds it. Since I am using the Docker plugin, I can also create a Docker image. Finally, I want to run the image in a container using a .sh script.

Btw both the Git service and Jenkins are running on my local Docker service. git and jenkins service

I have enabled "Expose daemon on tcp://localhost:2375 without TLS" in Docker Desktop setting and set the value in pom.xml to "tcp://localhost:2375". docker desktop setting pom.xml details

I can successfully build my project and create a Docker image in my local environment. run in IDEA

However, when I try to run the task in Jenkins, specifically when building the Docker image, I encounter the following error: Cannot create docker access object

I have tried many different values for 'dockerHost' label, but all failed.enter image description here if set to host.docker.internal:2375 host.docker.internal:2375 if dont set without dockerHost value if set to http://127.0.0.1:2375 http://127.0.0.1:2375 ...

My question is: If I am using WSL2 to run Docker containers (with Git and Jenkins services in my case), how can I enable Jenkins to connect to the Docker service and build images successfully? In my lesson, they are using virtualBox to create a Linux environment and run Docker on that, which is different from my env.


Solution

  • It's a pretty standard setup for someone on Windows to run Jenkins locally. You are missing two pieces:

    1. Run your jenkins container as root. Obviously you are not going to do that with your actual production setup, or even with any sandbox, but user permissions can be figured out later once your networking understanding is solid
    2. Mount your docker socket inside the jenkins container with --volume=/var/run/docker.sock:/var/run/docker.sock. Now jenkins should be able to connect to it using standard unix socket notation unix:///var/run/docker.sock