skip to Main Content

I’m attempting to run a CI/CD pipeline using GitLab CI with Kubernetes runners. I’m using a docker:dind base image to build and deploy my Java application with Maven. Here is the Dockerfile I am using:

FROM docker:dind

RUN apk update && apk add --no-cache openjdk21 maven git

ENV MAVEN_HOME=/usr/share/java/maven-3.9.8
ENV PATH=$MAVEN_HOME/bin:$PATH

CMD ["dockerd-entrypoint.sh"]

However, when I run the pipeline, I encounter the following log error:

Could not find a valid Docker environment.

I am using Kubernetes runners to execute my CI/CD jobs, and it seems like my configuration is unable to find or start a Docker environment correctly.

I tried using different dind versions, but i think the problem is related to the Kubernetes runners, because the docker wont be in one single machine (as it was before)

2

Answers


  1. I think. It is because Docker is not more a part of kubernetes installation. Just google about it. For example. https://kodekloud.com/blog/kubernetes-removed-docker-what-happens-now/

    Most likely you have another CRI containerD. Just take another base image. some alphine or ubuntu.

    Login or Signup to reply.
  2. Did you tried this below approach

    In your GitLab CI configuration, you need to enable DinD explicitly. Add the DOCKER_TLS_CERTDIR environment variable and set the services configuration in your .gitlab-ci.yml file to use the docker:dind image.

    example: .gitlab-ci.yml configuration:

        image: docker:latest
    
    services:
      - docker:dind
    
    variables:
      DOCKER_TLS_CERTDIR: "/certs"
      DOCKER_HOST: "tcp://docker:2376"
      DOCKER_TLS_VERIFY: "1"
      DOCKER_CERT_PATH: "/certs/client"
    
    before_script:
      - apk add --no-cache openjdk21 maven git
    
    build:
      stage: build
      script:
        - docker info  # Check if Docker is running
        - mvn clean install  # Build your Java app with Maven
    

    Since DinD needs privileged mode to run ensure your Kubernetes runner configuration allows privileged containers. This is typically set in your config.toml on the GitLab Runner side:

    [[runners]]
      ...
      privileged = true
      ...
    

    In some cases Kubernetes DNS or networking issues can prevent the docker service from being accessible. To address this use the following in your .gitlab-ci.yml:

        services:
      - name: docker:dind
        command: ["--host=tcp://0.0.0.0:2375"]
    

    If DinD continues to cause issues an alternative approach is to use Docker socket binding which allows Docker commands to run on the host machine Docker daemon without requiring a separate Docker service. In this approach map the Docker socket from the host to your container:

    variables:
      DOCKER_HOST: "unix:///var/run/docker.sock"
    
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search