Unable to access Docker Daemon from Jenkins - docker

I am running Jenkins inside a docker container.
I use the following command to start the container -
docker run -p 8080:8080 -p 50000:50000 -v "${PWD}:/var/jenkins_home" -v /var/run/docker.sock:/var/run/docker.sock aemdesign/jenkins
Notice -v /var/run/docker.sock:/var/run/docker.sock - I have done this so that I can access the docker daemon from within Jenkins as per this article.
I cd into the jenkins container using docker exec -it <mycontainer> bash
I then run docker ps -a but I still get docker command not found error.
I did some more research online and found out about the docker plugin for Jenkins, and configured it to connect to the docker daemon. I get the following error
Am I missing something? How do I solve this issue? Please note that I am doing this locally on a MAC machine.

-v /var/run/docker.sock:/var/run/docker.sock, this just means your container has ability to access docker daemon on the host, not mean your container will have the docker client.
You could use -v $(which docker):/usr/bin/docker to add docker client to your container, then you will find the command.
docker run -u root -p 8080:8080 -p 50000:50000 -v $(which docker):/usr/bin/docker -v "${PWD}:/var/jenkins_home" -v /var/run/docker.sock:/var/run/docker.sock aemdesign/jenkins
Another way if you want reserve jenkins user.
docker run -u jenkins:$(cut -d: -f3 < <(getent group docker)) -p 8080:8080 -p 50000:50000 -v $(which docker):/usr/bin/docker -v "${PWD}:/var/jenkins_home" -v /var/run/docker.sock:/var/run/docker.sock aemdesign/jenkins

Related

run docker in jenkins container (docker in docker)

Im trying to run docker inside jenkins container, i used this command to create jenkins container
docker run -p 8080:8080 -p 50000:50000 -d -v jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker jenkins/jenkins:latest
then this command to access jenkins container bash
docker exec -u 0 -it <container-id> bash, whenever i run docker i get this error
docker: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found (required by docker) docker: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found (required by docker)
What is creating this problem and what ways in order to solve it ?
This is not reliable anymore, because the Docker Engine is no longer distributed as (almost) static libraries.
so run docker run -p 8080:8080 -p 50000:50000 -d -v jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkins/jenkins:latest
then this command to access jenkins container bash as root user docker exec -u 0 -it <container-id> bash
Once inside the Jenkins container, simply run this command to install docker inside of the Jenkins container: curl https://get.docker.com/ > dockerinstall && chmod 777 dockerinstall && ./dockerinstall
this command gets the docker quick installation script and runs the script which then installs docker inside of the container
Exit out of the Jenkins container interactive shell, and run the following command to change permissions on “docker.sock” for added security sudo chmod 666 /var/run/docker.sock
solved by downgrading my server OS to Ubuntu 18

How do I run docker inside a jenkins container?

I am trying to run docker inside a jenkins container
I have mounted docker inside the jenkins container but I can't execute the docker commands inside the container
I have given read/write permission to jenkins user but it is still not working
docker run -p 8080:8080 -p 50000:50000 -d -v jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker jenkins/jenkins:lts
I get error message the docker does not exit

docker container fail to connect to docker daemon if use /usr/sbin/init to start the container

sudo docker run -it --name="cms_docker_without_init_with_port" --privileged=true --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v /home/cms/iris4_build:/home/cms/iris4_build -v /home/cms/jenkins:/home/cms/jenkins -v /home/cms/bin:/home/cms/bin -v /home/cms/cmn:/home/cms/cmn -v /home/cms/wls/bin:/home/cms/wls/bin -v /etc/passwd:/etc/passwd -v /etc/group:/etc/group -p 22000:22 artifact.oocl.com/fwk/cms_docker:1
I use above command to start the docker container, the cms_docker CAN connect to docker daemon in target VM.
There is contos7 inside the container, and I want to start the sshd service. I found in the docker official document, have to add /usr/sbin/init when start the container.
https://github.com/docker-library/docs/tree/master/centos#dockerfile-for-systemd-base-image
So I add the "/usr/sbin/init" when start the container. The command is below:
sudo docker run -it --name="cms_docker_without_init_with_port" --privileged=true --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v /home/cms/iris4_build:/home/cms/iris4_build -v /home/cms/jenkins:/home/cms/jenkins -v /home/cms/bin:/home/cms/bin -v /home/cms/cmn:/home/cms/cmn -v /home/cms/wls/bin:/home/cms/wls/bin -v /etc/passwd:/etc/passwd -v /etc/group:/etc/group -p 22000:22 artifact.oocl.com/fwk/cms_docker:1 /usr/sbin/init
But after I added /usr/sbin/init to start container, the cms_docker cannot connect to the docker daemon.
enter image description here
I need to start the sshd service and also need cms_docker connect to docker daemon. But now it seems there is conflict. Do you have any idea to solve this?

Can we run docker inside a docker container which is running in a virtual-box of Ubuntu 18.04?

I want to run docker inside another docker container. My main container is running in a virtualbox of OS Ubuntu 18.04 which is there on my Windows 10. On trying to run it, it is showing me as:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
How can I resolve this issue?
Yes, you can do this. Check for dind (docker in docker) on docker webpage how to achieve it: https://hub.docker.com/_/docker
Your error indicates that either dockerd in the top level container is not running or you didn't mount docker.sock on the dependent container to communicate with dockerd running on your top-level container.
I am running electric-flow in a docker container in my Ubuntu virtual-box using this docker command: docker run --name efserver --hostname=efserver -d -p 8080:8080 -p 9990:9990 -p 7800:7800 -p 7070:80 -p 443:443 -p 8443:8443 -p 8200:8200 -i -t ecdocker/eflow-ce. Inside this docker container, I want to install and run docker so that my CI/CD pipeline in electric-flow can access and use docker commands.
From your above description, ecdocker/eflow-ce is your CI/CD solution container, and you just want to use docker command in this container, then you did not need dind solution. You can just access to a container's host docker server.
Something like follows:
docker run --privileged --name efserver --hostname=efserver -d -p 8080:8080 -p 9990:9990 -p 7800:7800 -p 7070:80 -p 443:443 -p 8443:8443 -p 8200:8200 -v $(which docker):/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock -i -t ecdocker/eflow-ce
Compared to your old command:
Add --privileged
Add -v $(which docker):/usr/bin/docker, then you can use docker client in container.
Add -v /var/run/docker.sock:/var/run/docker.sock, then you can access host's docker daemon using client in container.

Link containers to a running container

I want to know if I can link Docker containers to a running container. I am running this command on a server:
docker run -d -u jenkins --name appdev-jenkins --network=host --memory="8g" -p 80:8080 -p 443:443 -p 50000:50000 -v "/opt/jenkins":/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkinsci/blueocean
I want to be able to link another Jenkins instance as an agent to the original Jenkins instance. Is this possible?
Have you tried the following?
docker run -d --link appdev-jenkins --name second-jenkins [other params]

Resources