Jenkins in Docker: Docker running inside a Jenkins Container.
Tags: docker, jenkins, containers
Published: 2023-04-24 16:43:44

Jenkins is a well-known continuous integration and delivery (CI/CD) platform. Jenkins is open-source and has a robust reference implementation that preserves its present software and produces plugins to improve its capability.

Amidst good community assistance, you may find difficulties with Jenkins, such as configuration file incompatibilities, which you may eliminate by running Jenkins in a container platform such as Docker. Running Jenkins with Docker, on the other hand, introduces a new issue if you intend to dockerize apps with Jenkins.

**This article will walk you through initiating Jenkins in Docker and allowing the Jenkins container to enable Docker to operate (build, run, and push images).**

_Please keep in mind that this hands-on is done on an Ubuntu computer. To advance, you must be somewhat familiar with Docker._

To begin, we will install Jenkins on our local PC but containerize it with Docker. If you may recollect, one of the merits of Jenkins is its platform neutrality, implying it can operate on any platform such as macOS, Linux, or Windows, and yet behave identically across platforms. Let’s get started straight now.

- 
Docker has to be installed on your host system (this machine is a Linux machine). If you want to join along, simply click (https://docs.docker.com/engine/install/) to install Docker on your local PC.

- 
Now we’ll launch Docker and expose port `8080` from the container, as well as bind it to port `8080` on our host system, so we can view Jenkins from this Docker container. We’ll establish another port, `50000`, and link it to the host computer; this will be the communication channel between the Jenkins master and worker nodes. We will also run the Docker container in “isolated” mode so that it may function as a daemon. Finally, we would mount disks holding Jenkins tasks, plugins, and other files.

Now, let’s execute the following instructions on our local machine:

```
docker run -p 8080:8080 -p 50000:50000 -d -v /var/run/docker.sock:/var/run/docker.sock -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts
```

- 
Now, let’s start an interactive shell on the newly formed Jenkins container so that we may install Docker inside of it. To do this, we execute the following command:

```
docker exec -it — user root <container id> bash
```

- 
Simply execute the following command once inside the Jenkins container to install Docker:

```
curl https://get.docker.com/ > dockerinstall && chmod 777 dockerinstall && ./dockerinstall
```
The above command downloads and executes the Docker rapid installation script from [https://get.docker.com/](https://get.docker.com/), which further installs Docker within the container.

- 
Exit the Jenkins container interactive shell and execute the following command to alter the permissions on “docker.sock” for increased security.

```
sudo chmod 666 /var/run/docker.sock
```
Docker has now been installed within the Jenkins container!

- 
Now, open our browser and connect to Jenkins at localhost: `8080`.As you can see in your browser, Jenkins wants to be unlocked using the password saved in the provided directory. Let’s access Jenkins by importing the password from the provided directory. We do this by using the following command:

```
docker exec -it <container id> bash
cat var/jenkins_home/secrets/initialAdminPassword
```
The password would be shown on your terminal as something like `fce7b34895cf4ba0860f8d2c8fe8487a`. Simply copy the password and paste it into the appropriate field.

- 
Simply choose “Install suggested plugins” now that you’ve been taken to the “Getting Started” page.

- 
You must now wait anxiously for each plugin to be installed. 
You’ve been sent to the page where you may create your user profile. Enter the necessary information.

**Now that you’ve successfully installed Docker within a Jenkins container, you may complete the task at hand that needs Docker.**

**Thanks for the audience!**

Please drop a few claps and leave a comment if you have any thoughts on the subject — I am always eager to learn and improve!
Cheers!