0

I need to execute docker inspect from a Jenkins pipeline to get the health status of a container. I tried the following in my pipeline script:

def containerHealth = sh(
    script: "docker inspect --format='{{json .State.Health.Status}}' db",
    returnStdout: true
).trim()

In my docker-compose file I mounted docker.sock as the following:

jenkins:
    container_name: jenkins-master
    ports:
        - '8080:8080'
        - '50000:50000'
    image: jenkins-master
    restart: always
    volumes:
        - /var/run/docker.sock:/var/run/docker.sock

However it doesn't work and I get this output message: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? But I can execute docker --version from the pipeline properly.

Does anyone have an idea how can I get through this?

  • Can you place both `docker inspect` and `docker --version` next to each other in a small pipeline and check if only the inspect fails. Please add the log to the question for more info. – Ram Jan 11 '22 at 07:43
  • When I place ```docker --version``` and ```docker inspect``` next to each other the console output is ```Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?``` I think it is because I cannot execute docker commands like docker inspect or docker ps from a slave container, I do not know. – Erred Occuror Jan 12 '22 at 09:37
  • This means you are not able to run even docker —version but you mentioned that you could in your question. What I understand from your setup is, you are running the Jenkins as a docker container and would like to spin a docker from the Jenkins pipeline as well. Am I right? – Ram Jan 12 '22 at 09:40
  • Yes I am running jenkins as a container. I use 2 other salve containers in where pipeline code is executed. I am able to run ```docker --version```, but ```docker ps``` or ```docker inspect``` don't work. I would like these commands to work inside my slave containres. Sorry if I was not clear – Erred Occuror Jan 13 '22 at 10:12
  • Could you add logs ? Please place ‘docker —version’ first and ‘docker inspect’ as a second line in the same pipeline. I would like to understand if the first command is executed as a shell command or a dsl command from docker plug-in in the Jenkins. – Ram Jan 13 '22 at 11:03
  • The logs is the following: (sorry it is not very readable in the comment section) ```[Pipeline] stage [Pipeline] { (Test: docker version) [Pipeline] sh + docker --version Docker version 20.10.8, build 3967b7d [Pipeline] sh + docker inspect '--format={{json .State.Health.Status}}' sq1 Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?``` – Erred Occuror Jan 14 '22 at 16:03

0 Answers0