-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using docker build plugin and getting "Cannot connect to the Docker daemon." #196
Comments
You need to have a docker daemon to connect to. As per https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/, a convenient way of doing such is bind-mounting
|
right, you can't just run a docker daemon in a docker container |
Thanks for the link and the additional info @dweomer. That came in handy. I managed to get something working that is a variation on what you suggested and I'd be curious to get your, or anyone else's opinions on best practices.
So, from this I concluded I have to at least install docker into my image. Note: Since I'm now installing docker into the image I had to map the group ID to a different name in the run.sh script below.
So now I have a Dockerfile that looks like this: And I have a run.sh script that looks like this: DOCKER_SOCKET=/var/run/docker.sock if [ -S ${DOCKER_SOCKET} ]; then exec sudo -E -H -u jenkins bash -c /usr/local/bin/jenkins.sh` |
Bind-mounting the Docker executable is only feasible when it is a static (aka standalone) executable. Older articles mention doing this because it was common practice that relied on the fact that the docker executable for the longest time was statically compiled. Installing docker via the
This installs the statically linked Docker executable and does not attempt to install support packages like apparmor or aufs which will happen, depending on your base container, when using the |
@dweomer: Good tip, thanks! I updated my image accordingly and its still all working, so I think this is the way for me to go. |
Thx, super helpful! Here's how I got this to work w/o the curl to get.docker.com. ./docker-compose.yml
./jenkins/Dockerfile
./jenkins/entrypoint.sh
|
thanks @bkcummins and @CameronGo I managed to stick the contents of entrypoint in cmd rather than the file. In my deployment environment this is easier to manage than having another external file.
|
Unfortunately this does not work in docker-machine since you can't (afaik) mount /var/run/docker.sock from your Docker Machine VM to the container running in the VM. |
Thanks @CameronGo and @bkcummins. I tried what you suggested but the if statement fails because the
The
any ideas what I might be doing wrong please?:) |
Here's my docker-compose.yml:
and here is my Dockerfile:
and here is the run.sh script that is running at launch:
If you put all 3 of those files into a folder and run docker-compose up then you should get a jenkins instance up and running that let's you run docker build inside of it provided the docker host also has docker installed and running. |
Use the entrypoint to create an unprivileged user, then use `su-exec` to assume that user's uid and the gid of the Docker daemon socket. jenkinsci/docker#196 (comment) Install the su-exec package. https://github.com/ncopa/su-exec
--env DOCKER_HOST=unix:///var/run/docker.sock \
|
I'm not sure the best strategy for this, but the path I took was to create a custom image from the public jenkins image, and I have installed docker into my custom image, adding the jenkins user to the docker group. This is what my Dockerfile looks like:
FROM jenkins
USER root
RUN curl -sSL https://get.docker.com/ | sh &&
usermod -aG docker jenkins
USER jenkins
Then I launch the container using this command:
docker run -d --name=jenkins -p 8080:8080 -p 50000:50000 -v /data/jenkins:/var/jenkins_home jenkinscustom
As soon as I attempt a build using the plugin; however, I get the following error immediately after the docker build command is issued:
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
Obviously the docker daemon isn't running since it is installed as a service. I'm not sure this is even the right approach to take. Is there any guidance for the best way to use the Docker Build plugins when running jenkins inside a Docker container?
The text was updated successfully, but these errors were encountered: