tutorial on deploying docker images with jenkins open source

Understanding the Basics of Docker and Jenkins Docker Overview Docker is an open-source containerization platform that enables developers to package applications into containers. These containers are lightweight, portable, and ensure consistency across different environments. In

Written by: David Choi

Published on: October 21, 2025

Understanding the Basics of Docker and Jenkins

Docker Overview
Docker is an open-source containerization platform that enables developers to package applications into containers. These containers are lightweight, portable, and ensure consistency across different environments. In tech stacks today, deploying applications using Docker ensures that they can run anywhere, be it a developer’s local machine or a production server.

Jenkins Overview
Jenkins is an open-source automation server in which the central build and continuous integration process take place. Jenkins supports building, deploying, and automating any project by integrating with various version control systems and tools. The combination of Jenkins and Docker facilitates robust CI/CD (Continuous Integration/Continuous Deployment) pipelines.

Pre-requisites for the Deployment

  1. Docker Installed: Ensure that Docker is installed on your system. You can download it from the official Docker website.

  2. Jenkins Installed: Install Jenkins on your server. You can either host it locally using Java or use Docker itself to run Jenkins. The following commands can help you get started with a Docker-based installation:

    docker pull jenkins/jenkins:lts
    docker run -d -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts
  3. Git for Version Control: You will need Git installed to pull your application code.

  4. Basic understanding of Jenkins and Docker concepts.

Setting Up Jenkins with Docker

  1. Access Jenkins: Open your web browser and go to http://localhost:8080. The initial setup wizard will prompt you to enter your admin password, which can be found in the logs. Use this command to retrieve it:

    docker exec jenkins_container_name cat /var/jenkins_home/secrets/initialAdminPassword
  2. Install Suggested Plugins: Upon logging in for the first time, Jenkins will guide you through the installation of suggested plugins. This setup will help you integrate the necessary tools.

  3. Create First Admin User: After the plugins are installed, create your first admin user. Fill in the required details and click “Save and Continue”.

  4. Configure URL: Click on “Save and Finish” after configuring the Jenkins URL.

  5. Set Up Its Own Slave: Configure Jenkins to use Docker as a cloud agent (slave) where builds can occur inside Docker containers.

Integrating Docker with Jenkins

  1. Install Docker Plugin: Navigate to Manage Jenkins > Manage Plugins. Search for the “Docker” plugin, install it, and restart Jenkins.

  2. Configure Docker Host: Go to Manage Jenkins > Configure System. Find the “Docker” section. Here, you will need to configure the Docker host details. If Jenkins runs on a machine that also runs Docker, you might just need to set up the Docker socket. Typically, it will look like this:

    unix:///var/run/docker.sock

    Alternatively, if working with remote Docker, configure accordingly.

  3. Add Credentials: If you need to push images to a Docker registry (e.g. Docker Hub), you’ll need to set up credentials. Proceed to Manage Jenkins > Manage Credentials and add Docker Hub credentials.

Creating a Jenkins Pipeline

  1. Create a New Job: Click on New Item, select Pipeline, and provide a name for your project.

  2. Define the Pipeline Script: Scroll down to the Pipeline section:

    pipeline {
        agent any
        stages {
            stage('Clone Repository') {
                steps {
                    git 'https://github.com/YOUR_USERNAME/YOUR_REPOSITORY.git'
                }
            }
            stage('Build Docker Image') {
                steps {
                    script {
                        def image = docker.build("myapp:${env.BUILD_ID}")
                    }
                }
            }
            stage('Run Tests') {
                steps {
                    script {
                        docker.image("myapp:${env.BUILD_ID}").inside {
                            sh 'npm test'  // or any command for running tests
                        }
                    }
                }
            }
            stage('Push Docker Image') {
                steps {
                    script {
                        docker.withRegistry('https://index.docker.io/v1/', 'dockerhub-credentials') {
                            image.push()  // Push the image to Docker Hub
                        }
                    }
                }
            }
        }
    }

    This pipeline script does the following:

    • Clones your repository.
    • Builds the Docker image with a unique tag based on the Jenkins build ID.
    • Runs tests in the Docker image.
    • Pushes the image to Docker Hub.
  3. Save the Pipeline: Click on Save.

Triggering the Pipeline

  1. Build the Pipeline: Click on Build Now to execute the pipeline. Watch the progress in the Jenkins console output.

  2. Monitor Builds: As the build runs, you can view real-time logs, which can help in debugging if something doesn’t go as planned.

Advanced Configuration

  1. Environment Variables: You can add environment variables in the pipeline by modifying the pipeline block. For instance:

    environment {
        NODE_ENV = 'production'
        APP_VERSION = env.BUILD_ID
    }
  2. Post Actions: You can add post actions for notifications, cleanup, or archiving artifacts. Example:

    post {
        success {
            echo 'Build succeeded!'
        }
        failure {
            echo 'Build failed!'
        }
    }
  3. Integrating Testing Frameworks: You may integrate various testing frameworks or static code analysis tools in the appropriate stages to ensure code quality.

Troubleshooting Common Issues

  • Jenkins is not running Docker commands: Ensure that the Jenkins user has the right permissions to access the Docker socket. This can typically be resolved by adding the Jenkins user to the Docker group:

     sudo usermod -aG docker jenkins
  • Failed Docker Build: Check the logs in the Jenkins console output to diagnose issues specific to commands run during the build.

  • Image not found: Ensure that the image name in your pipeline script matches the tags you are pushing to the Docker registry.

Best Practices for Jenkins and Docker Deployment

  1. Use Multistage Dockerfiles: This helps in reducing the size of the final Docker image by eliminating unnecessary files and dependencies.

  2. Automated Rollbacks: Implement strategies for rolling back deployments automatically upon failure, allowing for minimal downtime.

  3. Keep Images Lightweight: Use minimal base images to enhance the portability and speed of deployments.

  4. Regular Updates: Regularly update Jenkins, Docker, and plugins to leverage new features and security fixes.

  5. Use Docker Compose for Multi-Container Applications: If your application has multiple services, consider using Docker Compose, allowing you to define and run multi-container applications efficiently.

Summary of Key Points

  • Ensure Docker and Jenkins are correctly installed.
  • Configure Jenkins to use Docker as a containerized build agent.
  • Develop a Jenkins pipeline script that automates the Docker image build, test, and deployment process.
  • Monitor and troubleshoot your pipeline using Jenkins logs.
  • Implement best practices to enhance the CI/CD process efficiency.

Leave a Comment

Previous

detailed review of onlyoffice vs collabora office features

Next

which open source monitoring tool replaces prometheus and grafana