DEV Community

Cover image for Making docker available in Jenkins without a plugin. Part 1
tundek
tundek

Posted on

Making docker available in Jenkins without a plugin. Part 1

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Dokcer commands

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

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
Enter fullscreen mode Exit fullscreen mode

Docker

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 .
Enter fullscreen mode Exit fullscreen mode

Docker

Docker

Dockerfile output

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
Repo1
Repo2

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

Build success

And the image will be pushed to the private repo

Private repo build

Top comments (0)