skip to Main Content

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.

2

Answers


  1. Docker Desktop
    uses WSL 2 as the default backend
    and there’s also Jenkins for Windows. The network connectivity merely depends where you might install Jenkins. Even in a virtual network, it will be able to connect when it’s on the same host or in the same network segment.

    Comparing these should explain the concrete differences:

    Running docker info should show the hostname & port to connect to.
    On Linux this might be the docker daemon socket /var/run/docker.sock.

    Maybe try to connect with the protocol? Alike: http://host.docker.internal:2375.
    On the first screenshot, there is no port :2375 mapped -> EXPOSE 2375.

    Depending if the host is Windows or Linux, it’s either:

    • tcp://0.0.0.0:2375
    • unix:///var/run/docker.sock

    See Enable TCP port 2375 for external connection to Docker.

    Login or Signup to reply.
  2. 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
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search