Install Docker In Jenkins Container

/ Comments off
Install docker in jenkins container pack
  1. We have to use the Docker daemon of the host in order to run docker commands inside the Jenkins which is already running as a container in the docker. This is something we say running Docker in Docker! Lets see what we are getting from the dockerfile. Temporarily switched to the root user to install Docker; run a Docker install script.
  2. Apr 08, 2018 In Jenkins, all the commands in the stages of your pipeline are executed on the agent that you specify. This agent can be a Docker container. So, if one of your commands, for example, in the Build stage, is a Docker command (for example, for building an image), then you have the case that you need to run a Docker command within a Docker container.
  3. Docker images can be built as part of the pipeline. Create another container named build by using the alpine image. The Docker socket from the host also needs to be shared with the agent containers by creating a host path volume. Volume configuration is under Manage Jenkins Configure Jenkins Cloud. A new stage can then be added to the pipeline.
  4. To use Docker containers for the Jenkins build agents, you need to do a few things to your Jenkins configuration: Build a new Docker image that can act as a Jenkins build agent, and is capable of building Docker images (of course) Push the new image to a Docker registry. Turn off the default Jenkins build agents.

Step 1: Launch Jenkins Docker Container. The Dockerfile I have used to push the customized Jenkins image looks like this. All I am doing here is to install Docker and Docker Compose.

Jenkins:-

Jenkins is a Continuous Integration (CI) server or tool which is written in java. It provides Continuous Integration services for software development, which can be started via command line or web application server

Advantages of Jenkins:-

· Jenkins is an open-source tool with much support from its community.

· Installation is easier.

· It has more than 1000 plug-in to make the work easier.

· It is easy to create new Jenkins plugin if one is not available.

· It is a tool which is written in Java. Hence it can be portable to almost all major platforms.

Install Docker In Jenkins Container

Prerequisites:-

· A system with Jenkins installed and configured.

· Docker installed and docker group added to Jenkins.

· Machine with sudo access user Jenkins.

· Java installed and JAVA_HOME configured.

How to start a docker container and use it in a Jenkins job execution:-

Here I am going to use Jenkins pipeline to achieve the above scenario.

Here you can see a basic Jenkins pipeline syntax

In the above syntax we are using an agent as any available, in this case, Jenkins uses the default agent which is Jenkins machine itself.

For small scenarios, it's fine to use default agent, but if you need the specific environments such as node, mvn etc, and then you need to install all these applications in Jenkins machine which is a messy solution.

To avoid this we can use the docker containers as the agents to run the job-specific tasks and once the tasks are done the container will be destroyed thereby avoiding the need to install all the applications in the Jenkins machine.

Now let’s jump into the demo, here I am going to create Jenkins pipeline job as shown below.

Now navigate to pipeline option as shown below and enter the following pipeline declarative code.

Install docker in jenkins container code

In the above, I am using a MySQL container for DB operations,

The main advantage of this system is, we don’t need to manage DB and it can be used to store data from Jenkins job only during execution, this way can be used to store logs or records while CI-CD process and the data is retained in the volumes.

Jenkins

So, in this case, we only need to schedule a similar job and attach the same volume to retrieve the data from the database.

Install docker in jenkins container list

Install Docker In Jenkins Container List

Now let’s run the job and see the result in the container agent.

Here you can see the respective volume and environments are created.

Here you can see the command executed successfully in the container.

similarly we can execute multiple commands in the container environment and destroy the container once the usage is completed.

Install Docker Client In Jenkins Container