A Jenkins pipeline job defines and automates a series of steps in the software delivery process. It enables scripting and organizing entire build, test, and deployment workflows. Jenkins pipelines facilitate seamless integration of continuous integration and continuous delivery (CI/CD) practices into software development by allowing teams to define, visualize, and execute complex processes as code.
- Jenkins server running and accessible
- GitHub repository with source code
- Basic understanding of Docker concepts
- Administrative access to Jenkins instance
- Navigate to Jenkins dashboard
- Click "New Item" from the left sidebar
- Enter job name: "My pipeline job"
- Select "Pipeline" as job type
- Click "OK"
Set up automatic triggering when code changes occur:
- In job configuration, scroll to "Build Triggers" section
- Select "GitHub hook trigger for GITScm polling"
- Save configuration
Note: Ensure GitHub webhook is already configured to point to Jenkins server endpoint.
Jenkins supports two pipeline syntax types:
- Declarative Syntax: Structured, domain-specific language (recommended for beginners)
- Scripted Syntax: More flexible, suitable for complex requirements
pipeline {
agent any
stages {
stage('Connect To Github') {
steps {
checkout scmGit(branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/username/repository.git']])
}
}
stage('Build Docker Image') {
steps {
script {
sh 'docker build -t dockerfile .'
}
}
}
stage('Run Docker Container') {
steps {
script {
sh 'docker run -itd -p 8081:80 dockerfile'
}
}
}
}
}agent anySpecifies pipeline can run on any available Jenkins agent (master or node).
Contains sequential stages representing different phases of the delivery process.
stage('Connect To Github') {
steps {
checkout scmGit(branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'repository-url']])
}
}- Checks out source code from specified GitHub repository
- Uses main branch by default
- Downloads repository contents to Jenkins workspace
stage('Build Docker Image') {
steps {
script {
sh 'docker build -t dockerfile .'
}
}
}- Creates Docker image using Dockerfile in repository root
- Tags image as 'dockerfile'
- Executes shell command within Jenkins environment
stage('Run Docker Container') {
steps {
script {
sh 'docker run -itd -p 8081:80 dockerfile'
}
}
}- Runs container in detached mode (-d flag)
- Maps host port 8081 to container port 80
- Uses previously built Docker image
- In pipeline job configuration, click "Pipeline Syntax"
- Select "checkout: Check out from version control" from dropdown
- Enter repository URL and branch information
- Click "Generate Pipeline Script"
- Copy generated script and replace in pipeline configuration
Before running Docker commands in Jenkins pipeline, install Docker on the Jenkins server instance.
- Create installation script file:
nano docker-install.sh- Add Docker installation commands:
sudo apt-get update -y
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update -y
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
sudo systemctl enable docker
sudo systemctl start docker- Make script executable and run:
chmod +x docker-install.sh
./docker-install.sh- Add Jenkins user to docker group:
sudo usermod -aG docker jenkins
sudo systemctl restart jenkinsCreate Dockerfile in repository root:
# Use official NGINX base image
FROM nginx:latest
# Set working directory
WORKDIR /usr/share/nginx/html/
# Copy local HTML file to NGINX directory
COPY index.html /usr/share/nginx/html/
# Expose port 80
EXPOSE 80Create index.html:
<!DOCTYPE html>
<html>
<head>
<title>Pipeline Success</title>
</head>
<body>
<h1>Congratulations!</h1>
<p>Successfully run first pipeline code.</p>
</body>
</html>- Commit and push Dockerfile and index.html to repository
- Pipeline automatically triggers via webhook
- Monitor execution in Jenkins dashboard
- Configure security group/firewall rules for port 8081
- Access application via browser:
http://jenkins-server-ip:8081
Problem: Jenkins cannot execute Docker commands Solution:
sudo usermod -aG docker jenkins
sudo systemctl restart jenkinsProblem: Container fails to start due to port conflict Solution:
# Find and stop conflicting container
docker ps
docker stop <container-id>
# Or use different port mapping
docker run -itd -p 8082:80 dockerfileProblem: Docker build fails with "cannot find Dockerfile" Solution:
- Ensure Dockerfile exists in repository root
- Check file naming (case-sensitive)
- Verify checkout stage completed successfully
Problem: Pipeline cannot access repository Solution:
- Verify repository URL is correct and accessible
- Check GitHub webhook configuration
- Ensure proper branch name (main vs master)
- Configure Jenkins credentials if repository is private
Problem: Jenkins still cannot find Docker commands Solution:
# Restart Jenkins service
sudo systemctl restart jenkins
# Verify Docker installation
docker --version
# Check Jenkins user can access Docker
sudo -u jenkins docker psProblem: Container exits after starting Solution:
- Check container logs:
docker logs <container-id> - Ensure base image runs correctly
- Verify application starts properly in container
Problem: Pipeline fails to parse Solution:
- Use Pipeline Syntax generator for complex steps
- Validate Groovy syntax
- Check proper indentation and brackets
- Test pipeline script in smaller segments
- Always test Dockerfile locally before committing
- Use specific image tags instead of 'latest' for production
- Include error handling in pipeline scripts
- Monitor Jenkins logs for detailed error information
- Clean up unused Docker images and containers regularly
- Use meaningful stage and job names
- Document pipeline changes and configurations








