Making docker available in Jenkins without a plugin. Part 1
Tags: docker, devops, jenkins, linux
Published: 2024-02-24 17:10:42

In this article, you will be able to run all your favourite docker commands in the same container that has your jenkins running..

If you already have Jenkins running in a container, we will stop the container and restart it with the following commands.

### Step 1: Check the status of the running container

`docker ps`

![Docker ps command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fj00xghgab6n313s080m.png)

run the command `docker stop 8cb594e942c2` to stop the container. Once the container stops, we can now re-run our jenkins app with this command

```
docker run -p 8080:8080 -p 50000:50000 -d -v jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker jenkins:jenkins:lts
```

Yeah, i know :), the command seems ambiguous, complex to understand and confusing, let's break it down.

* docker run: This is the command to create and start a new Docker container.

* -p 8080:8080: This option maps port 8080 of the host to port 8080 of the container. Jenkins typically runs its web interface on port 8080, so this mapping allows you to access the Jenkins web interface through your host machine's port 8080.

* -p 50000:50000: This maps port 50000 of the host to port 50000 of the container. Jenkins uses this port for agent connections, allowing build agents to communicate with the Jenkins server.

* -d: This option runs the container in detached mode, meaning the container runs in the background and does not block the terminal session.

* -v jenkins_home:/var/jenkins_home: This mounts a volume named jenkins_home to /var/jenkins_home inside the container. /var/jenkins_home is the default directory Jenkins uses to store its configuration and data. By mounting a volume, you ensure that this data persists across container restarts and recreations, maintaining your Jenkins state.

* -v /var/run/docker.sock:/var/run/docker.sock: This volume mount is particularly interesting. It mounts the Docker socket from the host into the container. This allows the Jenkins container to communicate with the Docker daemon of the host machine, effectively allowing Jenkins to launch other Docker containers. This is often used for running Jenkins jobs inside separate Docker containers.

* -v $(which docker):/usr/bin/docker: This mounts the Docker binary from the host into the container at /usr/bin/docker. This allows Jenkins running inside the container to execute Docker commands directly, assuming Docker is installed on the host at the path returned by $(which docker).

* jenkins:jenkins:lts: This specifies the Docker image to use. It seems like there's a repetition in the image name; typically, it would be something like jenkins/jenkins:lts where jenkins/jenkins is the Docker Hub repository for Jenkins images, and lts specifies that you want to use the Long-Term Support version of Jenkins.

#### Let's login into the Jenkins container

```
docker exec -it 8cb594e942c2 /bin/bash
```


![Dokcer commands](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0jx5woti4csu0i8v74t.png)

If we try to execute the command `docker pull redis` on the container, we will get an error which needs to be corrected so that we can successfully run docker commands on the container.


![Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06vthxdwgsgmlb245dny.png)

Exit from the shell and login as root

`docker exec -it -u 0 8cb594e942c2 /bin/bash`

follow the above command with the following

```
chmod 666 /var/run/docker.sock
ls -l /var/run/docker.sock
```

![Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nfnsdzrou9x8vo1v3aa3.png)

With the above, you are now able to execute docker commands on the jenkins execute shell.

### Step 2: Build a docker image for our App

To build the docker image, we will now go into Jenkins and add the following command on the Execute shell part using the Dockerfile to build the image

```
docker build -t java-maven-app:1.0 .
```


![Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22qpkqzbcevfzsj36thv.png)

![Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nptcgg211rvcte1kcvhg.png)

![Dockerfile output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nbd1ffj3qmsvdahrj536.png)

### Step 3: Pushing the image to Artifactory (Docker Repository)

We will now be pushing the image we have created into a private repository (docker repo) in this case.

Some of the configurations we need to do on Jenkins include the following


![Repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d969ok835r7jesuydg5k.png)
![Repo1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2aimbk827yiritu7ez0i.png)
![Repo2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/we0rdx1dcaqhaqraxuxt.png)

After a successful build on Jenkins we will have an output like so with a SUCCESS message

![Build success](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jb3bdir4nauvml2eygj.png)

And the image will be pushed to the private repo


![Private repo build](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1pny9pmjwltkk7zj392h.png)