简体   繁体   中英

How to build docker image in Jenkins running into docker?

I just tried build my test image for Jenkins course and got the issue

+ docker build -t nginx_lamp_app .
/var/jenkins_home/jobs/docker-test/workspace@tmp/durable-d84b5e6a/script.sh: 2: /var/jenkins_home/jobs/docker-test/workspace@tmp/durable-d84b5e6a/script.sh: docker: not found
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

But I've already configured docker socket in docker-compose file for Jenkins, like this

version: "2"
services:
  jenkins:
    image: "jenkins/jenkins:lts"
    ports:
      - "8080:8080"
    restart: "always"
    volumes:
      - "/var/jenkins_home:/var/jenkins_home"
      - "/var/run/docker.sock:/var/run/docker.sock"

But, when I attach to container I see also "docker: not found" when I type command "docker"... And I've changed permissions to socket like 777 What's can be wrong?

Thanks!

You are trying to achieve a Docker-in-Docker kind of thing. Mounting just the docker socket will not make it working as you expect. You need to install docker binary into it as well. You can do this by either extending your jenkins image/Dockerfile or create( docker commit ) a new image after installing docker binary into it & use that image for your CI/CD. Try to integrate below RUN statement with the extended Dockerfile or the container to be committed(should work on ubuntu docker image) -

RUN  apt-get update && \
     apt-get -y install apt-transport-https \
     ca-certificates \
     curl \
     gnupg2 \
     software-properties-common && \
     curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && \
     add-apt-repository \
     "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
     $(lsb_release -cs) \
     stable" && \
     apt-get update && \
     apt-get -y install docker-ce

Ref - https://github.com/jpetazzo/dind

PS - It isn't really recommended ( http://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/ )

Adding to that, you shouldn't mount host docker binary inside the container -

⚠️ Former versions of this post advised to bind-mount the docker binary from the host to the container. This is not reliable anymore, because the Docker Engine is no longer distributed as (almost) static libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM