Introduction
The application Docker makes it easier to manage application processes in containers. You can run your applications in resource-isolated processes with containers. Containers are similar to virtual machines, but they are more mobile, use fewer resources, and rely more on the host operating system.
Jenkins is an automation server that is free to use. It facilitates continuous integration and delivery by automating the building, testing, and deployment phases of software development. It is a server-based system that runs in Apache Tomcat and other servlet containers.
Prerequisites
- Up and running Docker instance.
- Basic knowledge of Jenkins and docker understanding.
Step 1: Create Custom Dockerfile
We need to build a custom Jenkins docker images with docker engine, So that’s why we need to use the following steps.
To create a directory.
mkdir custom_jenkins && cd custom_jenkins
To create a Dockerfile.
nano Dockerfile
Paste the following command.
FROM jenkins/jenkins:lts
USER root
RUN apt-get update -qq \
&& apt-get install -qqy apt-transport-https ca-certificates curl gnupg2 software-properties-common
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
RUN add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/debian \
$(lsb_release -cs) \
stable"
RUN apt-get update -qq \
&& apt-get -y install docker-ce
RUN usermod -aG docker jenkins
Step 2: Build Custom Jenkins Image
We are ready to build the custom jenkins docker image with docker engine by following given command.
To build custom jenkins docker image with named “jenkinsdocker”
sudo docker build -t jenkinsdocker:latest .
Step 3: Deploy Jenkins Docker Container
Once the docker image got build So then we can start uisng custom jenkins docker container that will perform the docker based tasks.
Jenkins can be configured right away by running the image, however if you do that, you won’t be able to:
Save Jenkins settings and information. Dockerize an application with the Jenkins container when the container is stopped, restarted, or removed since the custom Jenkins image YOU does not contain the Docker daemon, which is necessary to generate and push images.
archival Jenkins data
enabling the Jenkins container’s use of the Docker daemon /var/run/docker.sock
Jenkins volume configuration to store Jenkins’s data, you need to create an explicit volume for Jenkins on your host machine. To do that, add this argument -v jenkins_home:/var/jenkins_home
when running the custom Jenkins image.
To accomplish this, link the Jenkins container’s Docker CLI to the host machine’s Docker daemon using given command.
docker run -it -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkinsdocker:latest
Step 4: Accessing Jenkins
Open the browser with localhost:8080 and setup the Admin user creds and suggested plugins.
Now Jenkins asking for the initialAdminPassword, You need to execute the following command to get password.
sudo docker logs <TYPE_Jenkins_Container_ID>
You should get out put like this.
In the Customize Jenkins web page you need to select an option to install suggested plugin or select plugin, Choose as per your requirment.
In my case, I am selecting suggested plug-in’s, You should get out-put like this.
After that you will be asked to create your admin account with jenkins like this.
Update your Jenkins URL, If you want to access from network So then you need to replace localhost to jenkins’s machine IP address.
After this you should get start using Jenkins message on your screen.
and then you should get Jenkins’s dashboard like this.
Step 5: Build and Push Docker Images
To test docker in Jenkins container, We need to execute the docker command like docker info, If able to docker information with Jenkins So thats mean docker installation done inside Jenkins container.
Follow the given steps.
- Create a free styple project with any name like Check_Docker_Version
- Choose Execute Shell in Build Step section
- Put the
docker info
- Run the Check_Docker_Version Job
If everything going good so then you should get out put like this.
tarted by user Aftab
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/Check_Docker_version
[Check_Docker_version] $ /bin/sh -xe /tmp/jenkins6418660913341057972.sh
+ docker info
Client:
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.10.2
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.15.1
Path: /usr/libexec/docker/cli-plugins/docker-compose
scan: Docker Scan (Docker Inc.)
Version: v0.23.0
Path: /usr/libexec/docker/cli-plugins/docker-scan
Server:
Containers: 1
Running: 1
Paused: 0
Stopped: 0
Images: 105
Server Version: 20.10.18
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
runc version: v1.1.4-0-g5fd4c4d
init version: de40ad0
Security Options:
apparmor
seccomp
Profile: default
cgroupns
Kernel Version: 5.15.0-48-generic
Operating System: Ubuntu 22.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.605GiB
Name: VivoBook
ID: Q5TP:JEPW:UZCN:IGXE:LUR3:QGGZ:Z4EG:5HTX:NO7U:75KK:DFTV:7IRO
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Finished: SUCCESS
In order build docker images, You can use the given link.
Conclusion
We have successfully Installed Docker inside in Jenkins’s Docker Container, If you still have questions, please post them in the comments section below.
How can I find out more about it?
By following our facebook page.
May I have information on the topic of your article?
You’ve been great to me. Thank you!