0

I have a VM with Linux CentOS 7, and inside this VM, there is an active Docker daemon, running a container of a Jenkins instance (LTS). This instance is responsible of running a pipeline described in a Jenkinsfile, in which the last stage is the docker build of a Dockerfile. I attach to this post a photo of my current architecture to be as clear as possible.

My actual architecture: a VM with Linux CentOS 7, and inside this VM, there is an active Docker daemon, running a container of a Jenkins instance (LTS).

My task is: successfully execute all the pipeline's stage, in particular the last one, the build of the Dockerfile.

But, I ran into a problem: inside the container I can't launch the docker daemon, so, how can I launch the build of the Dockerfile within its docker container?

A colleague suggested me to configure a Jenkins Agent on the VM, outside of the docker daemon, but is it possible? If yes, how?

Or maybe it's better to opt for other solutions?

pppery
  • 3,731
  • 22
  • 33
  • 46
Lorenzo
  • 85
  • 1
  • 9
  • 1
    you need to mount docker.sock and install docker client in jenkins container – Def Soudani May 01 '22 at 16:08
  • @DefSoudani Thanks, I'll try, but what if in any way, I wanted to use a Jenkins Agent? I would have to install docker.sock and a docker client on the Agent container, right? – Lorenzo May 01 '22 at 16:14
  • 1
    While Stack Overflow does permit certain questions about Docker, we require that they (like all questions asked here) be specifically related to programming. This question does not appear to be specifically related to programming, but daemon- and jenkins-configuration, which makes it off-topic here. You might be able to ask questions like this one on [sf] or [DevOps](https://devops.stackexchange.com/). – Turing85 May 01 '22 at 16:21
  • 1
    @Turing85 Oh, good to know, I didn't know there was a forum dedicated to DevOps, next time I'll post my doubts related to Docker and Jenkins there, thanks! – Lorenzo May 01 '22 at 18:26
  • 1
    I’m voting to close this question because it's not a programming question. – pppery May 02 '22 at 01:49

1 Answers1

1

First of all, you can build docker image within the Jenkins container and if you're using declarative Jenkinsfile, then install docker pipeline plugin. See how to exploit and docs in the following links.

https://plugins.jenkins.io/docker-workflow/

https://docs.cloudbees.com/docs/admin-resources/latest/plugins/docker-workflow

In order to build your Dockerfile, see the code snippet below

stage("Build") {
        steps {
            script {
                myImage = docker.build("team/my-image:latest") // build the Dockerfile
            }
        }
    }

    // you can execute your Docker container and run some specific command you desire
    stage("Run Docker Container") {
        steps {
            script {
                sh "docker network create --driver bridge prod_bridge || true"
                myImage.withRun("--network-alias prod_bridge_alias --net prod_bridge --name my-container") {c ->
                    sh """
                        docker logs my-container
                    """
                }
            }
            sh "docker network rm prod_bridge"
            sh "docker rmi team/my-image:latest"
        }
    }

However, somewhat you might be faced some issues such as out of resources ( space/ cpu/ memory), or any other technical issues related to "docker in docker", which is not recommended, which are explained in Jerome's post.

Also see the question on Stackoverflow Is it ok to run docker from inside docker?

You can consider installing Jenkins and all required plugin in the Jenkins instance on your VM, as your colleague recommended.

It's up to you!

Haeyoon J.
  • 181
  • 1
  • 2
  • 12
  • Thanks, you're right, it can work, but probably I will run into other problems related to system resources and "docker in docker" related problems, which I would avoid. At this point I think I will put both the Jenkins server and the Jenkisn agent at VM level. – Lorenzo May 01 '22 at 18:24