So you want to start testing your code? Good choice. Testing is a great way to make sure nothing breaks in deployment, and Jenkins makes it easy to automate this process. Attention!: You can read the original article here https://carltheperson.com/posts/jenkins-in-docker-and-tests-inside-own-container/ The code formatting is much better there In this tutorial you will learn how to set up Jenkins, so it runs inside a Docker container, and test your projects inside their own container. This is useful if you want to test your application in its own custom environment, isolated from Jenkins. Having your Jenkins setup containerized makes it portable and quick to set up. Whether you are running Mac, Windows or Linux running Jenkins inside Docker is easy and straightforward. Why run Jenkins in a Docker container? This is definitely not always necessary but can be an advantage if you want to specify your exact testing situation. Why run my project in a Docker container within Jenkins? If your project is meant to be deployed using Docker it is important that your testing environment completely matches your production environment. Setting up Jenkins Here we are going to need Docker to be installed inside our Jenkins container, so we create a Dockerfile that builds on top of the official Jenkins image. Put this in a file called Dockerfile_jenkins_setup jenkins/jenkins:lts root jenkins FROM USER RUN apt-get update && \ apt-get -y install apt-transport-https \ ca-certificates \ curl \ gnupg2 \ software-properties-common && \ curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; )/gpg > /tmp/dkey; apt-key add /tmp/dkey && \ add-apt-repository \ && \ apt-get update && \ apt-get -y install docker-ce echo " " $ID "deb [arch=amd64] https://download.docker.com/linux/ \ \ stable" $(. /etc/os-release; echo "$ID") $(lsb_release -cs) RUN apt-get install -y docker-ce RUN usermod -a -G docker jenkins USER You could also pull the image from the image source Building the image: docker build -f DockerfileJenkinsSetup -t gustavoapolinario/jenkins-docker . Running the container: docker run -d -p 8080:8080 \ -v /var/run/docker.sock:/var/run/docker.sock \ -v /var/jenkins_home:/var/jenkins_home \ --name Jenkins_Docker gustavoapolinario/jenkins-docker docker run -d -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock -v /var/jenkins_home:/var/jenkins_home --name Jenkins_Docker gustavoapolinario/jenkins-docker # Optional # One line: Now visit localhost:8080 Jenkins will ask you for a password. You can retrieve it by running: docker Jenkins_Docker cat /var/jenkins_home/secrets/initialAdminPassword exec Just a few more things to set up Jenkins: Click: Install suggested plugins Fill in your information The app For this project I have created a very simple Node app. You can find the full source code at: I have also written a simple test using Mocha and Chai. The test just tests if the app returns with status 200. I test my app using https://github.com/carlriis/Jenkins-Docker-Example.git npm test express = ( ); app = express(); port = process.env.PORT || ; app.get( , (req, res) => { res.status( ); res.send( ); }); app.listen(port, () => { .log( ); }); .exports = app; const require "express" const const 3000 '/' 200 "Cool beans" console `App is up and listening on port ` ${port} module Let’s pretend that this app is going to be deployed on a server in a Docker container. So, I create a Dockerfile based on the Node image. Note that I am setting the app directory to an environment variable. node:latest PROJECTDIR /nodeApp FROM ENV WORKDIR $PROJECTDIR COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD [ , ] "npm" "start" The pipeline We are now going to create the pipeline that will build our Docker image and run our tests inside. On the Jenkins homepage, Click: New Item Pick Pipeline Select: Pipeline script from SCM Put in your git repository. Add the path to your Jenkinsfile Now we should be good to go. When we ask Jenkins to build our app, it will clone our repository and execute our Jenkinsfile. Here is the Jenkinsfile I am using. It will build an image from the files in the Git repository using our Dockerfile. It will then enter the image and extract the environment variable that we defined earlier. Then it copies everything from that directory into our workspace. This is not always necessary, and we could just run the tests directly with the files from the repository, but I like doing it because the project directory might look different depending on what you specified in your Dockerfile. Our pipeline will then go inside the new directory and run the test. At the end it will remove the Docker image, so you don't get a bunch of images from old builds taking up space. PROJECTDIR pipeline { environment { registry = } agent any stages { stage( ) { steps { script { dockerImage = docker.build registry + { dockerImage.inside() { def PROJECTDIR = sh(script: , returnStdout: ).trim() sh dir( ) { sh } } } { sh } } } } } } // This registry is important for removing the image after the tests "yourname/nodeapp" "Test" // Building the Docker image ": " $BUILD_NUMBER try // Extracting the PROJECTDIR environment variable from inside the container 'echo \$PROJECTDIR' true // Copying the project into our workspace "cp -r ' ' ' '" $PROJECTDIR $WORKSPACE // Running the tests inside the new directory " " $WORKSPACE $PROJECTDIR "npm test" finally // Removing the docker image "docker rmi : " $registry $BUILD_NUMBER Building We can now try building our app (just select your item and click build) and it should fail or succeed depending on if the tests we’ve written pass. If something unexpected happens you can click on the build and look at the console output. This should give you a clue as to what happened. If your build fails saying something like “ ”, then the GID of the Docker group in the container does not match the one on your machine (some problems with permissions). What worked for me on Ubuntu was: Got permission denied while trying to connect to the Docker daemon socket docker --user root Jenkins_Docker groupmod -g `cut -d: -f3 < <(getent group docker)` docker docker restart Jenkins_Docker exec Conclusion You should now be able to set up Jenkins in a Docker container and create a pipeline that builds an image and runs tests inside it. Hopefully you will get into the habit of continuous integration, and I am sure it will improve your workflow.