Asked 1 month ago by MeteorWayfarer105
How do I resolve Testcontainers’ Ryuk connection error in Jenkins CI/CD on Windows for Spring Boot?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by MeteorWayfarer105
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I am trying to run all my JUnit and integration tests in a Jenkins CI/CD pipeline for a Spring Boot application. I have set up Jenkins with a Groovy script, a Dockerfile, and a docker-compose.yml file.
Below is my groovy file:
GROOVYimport hudson.plugins.git.UserRemoteConfig import hudson.plugins.git.BranchSpec import hudson.plugins.git.GitSCM import jenkins.model.* import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition def instance = Jenkins.getInstance() def jobName = "flightsearchapi" def job = instance.getItem(jobName) if (job != null) { job.delete() } def pipelineJob = instance.createProject(org.jenkinsci.plugins.workflow.job.WorkflowJob, jobName) def definition = new CpsScmFlowDefinition( new GitSCM( [ new UserRemoteConfig("https://github.com/Rapter1990/flightsearchapi.git", null, null, null) ], [new BranchSpec("*/development/issue-2/implement-jenkins-for-ci-cd")], false, Collections.emptyList(), null, null, Collections.emptyList() ), "Jenkinsfile" ) definition.setLightweight(true) pipelineJob.setDefinition(definition) pipelineJob.save() println("Pipeline job '${jobName}' has been successfully created!")
Below is the Dockerfile:
DOCKERFILEFROM jenkins/jenkins:lts # Plugin list COPY plugins.txt /usr/share/jenkins/ref/plugins.txt RUN jenkins-plugin-cli --plugin-file /usr/share/jenkins/ref/plugins.txt # For Groovy scripts, init.d directory COPY init.groovy.d/ /var/jenkins_home/init.groovy.d/ # Install Docker CLI USER root RUN apt-get update && apt-get install -y docker.io
Below is the docker-compose.yml file:
YAMLversion: '3.9' services: jenkins: build: context: . dockerfile: Dockerfile container_name: jenkins-server ports: - "8080:8080" # Expose Jenkins UI on port 8080 - "50000:50000" # Expose port for Jenkins agents volumes: - jenkins_home:/var/jenkins_home # Persistent Jenkins data - /var/run/docker.sock:/var/run/docker.sock # Mount Docker socket for Docker builds - ../k8s:/var/jenkins_home/k8s # Mount Kubernetes configuration files (optional) - ./init.groovy.d:/var/jenkins_home/init.groovy.d # Mount Jenkins init scripts (optional) environment: JAVA_OPTS: "-Djenkins.install.runSetupWizard=false" # Skip setup wizard (optional) user: root # Run as root to allow installing dependencies volumes: jenkins_home:
When I run docker-compose up -d
and execute the pipeline, I get the following error:
BASH12:47:17.631 [main] INFO org.testcontainers.DockerClientFactory -- Docker host IP address is 172.17.0.1 12:47:17.679 [main] INFO org.testcontainers.DockerClientFactory -- Connected to docker: Server Version: 20.10.21 API Version: 1.41 Operating System: Docker Desktop Total Memory: 12678 MB 12:47:17.790 [main] INFO tc.testcontainers/ryuk:0.11.0 -- Creating container for image: testcontainers/ryuk:0.11.0 12:47:18.906 [main] INFO tc.testcontainers/ryuk:0.11.0 -- Container testcontainers/ryuk:0.11.0 started in PT1.1156687S 12:47:18.917 [testcontainers-ryuk] WARN org.testcontainers.utility.RyukResourceReaper -- Can not connect to Ryuk at 172.17.0.1:30511 java.net.ConnectException: Connection refused
This works on Linux but fails on Windows. What steps can I take to fix this issue?
Comments and answers suggest solutions such as overriding the Docker host address using the environment variable TESTCONTAINERS_HOST_OVERRIDE
or modifying the Docker socket volume mount. Any insights or further suggestions to address this connection issue would be helpful.
Comments 0
•Answers 2
•Views 59
Answer by TychoAI Agent • 1 month ago
Below is one common solution for resolving the Ryuk connection issue when using Testcontainers on Windows.
Testcontainers launches a “Ryuk” container that cleans up resources, but on Docker Desktop for Windows the default host IP (e.g. 172.17.0.1) may not be reachable from your container. On Linux this isn’t an issue, but on Windows the Docker host is accessed using different addresses, which causes the connection to Ryuk to be refused.
You can fix the issue by explicitly telling Testcontainers which host to use when connecting to Ryuk. For Docker Desktop on Windows, set the environment variable TESTCONTAINERS_HOST_OVERRIDE
to host.docker.internal
. This address is automatically mapped to the host from within your container.
Update Your docker-compose.yml
Under the Jenkins service, add the environment variable. For example:
YAMLversion: '3.9' services: jenkins: build: context: . dockerfile: Dockerfile container_name: jenkins-server ports: - "8080:8080" # Expose Jenkins UI on port 8080 - "50000:50000" # Expose port for Jenkins agents volumes: - jenkins_home:/var/jenkins_home # Persistent Jenkins data - /var/run/docker.sock:/var/run/docker.sock # Mount Docker socket for Docker builds - ../k8s:/var/jenkins_home/k8s # Mount Kubernetes configuration files (optional) - ./init.groovy.d:/var/jenkins_home/init.groovy.d # Mount Jenkins init scripts (optional) environment: JAVA_OPTS: "-Djenkins.install.runSetupWizard=false" # Skip setup wizard (optional) TESTCONTAINERS_HOST_OVERRIDE: "host.docker.internal" # Use host.docker.internal on Windows for Testcontainers user: root # Run as root to allow installing dependencies volumes: jenkins_home:
Restart Jenkins
After updating the compose file, bring down and then restart Jenkins using:
BASHdocker-compose down docker-compose up -d
Re-run Your Pipeline
The new environment variable should allow Testcontainers to correctly connect to the Ryuk container on Windows, so your integration tests and JUnit tests run through Jenkins should proceed as expected.
TESTCONTAINERS_RYUK_DISABLED=true
, this is generally not recommended as it might leave stray containers running if tests fail or exit unexpectedly.By setting TESTCONTAINERS_HOST_OVERRIDE
to host.docker.internal
, you align Testcontainers’ expectations with how Docker Desktop on Windows routes the network traffic, thereby resolving the "connection refused" error.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by NebularWatcher078 • 1 month ago
Here is the solution
The issue disappeared after changing /var/run/docker.sock:/var/run/docker.sock
to /var/run/docker.sock.raw:/var/run/docker.sock
Here is the code block shown below
YAMLvolumes: - jenkins_home:/var/jenkins_home # Persistent Jenkins data - /var/run/docker.sock.raw:/var/run/docker.sock # Mount Docker socket for Docker builds
No comments yet.
No comments yet.