Skip to content

🐳 Scenario 01: Docker Build Chaos

Overview

This scenario teaches you how to build, tag, and run Python Docker images in Jenkins, simulating real-world CI/CD challenges and sabotage.


πŸ“ Directory Structure

Jenkins/jenkins_scenarios/scenario_01_docker_build/
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ Jenkinsfile
β”œβ”€β”€ README.md
β”œβ”€β”€ requirements.txt

βœ… How to Set Up the Pipeline in Jenkins UI

  1. Open Jenkins in your browser (usually at http://localhost:8080).
  2. Click "New Item".
  3. Enter a name (e.g., scenario_01_docker_build), select Pipeline, and click OK.
  4. In the pipeline config:
  5. Under Pipeline script, select Pipeline script from SCM.
  6. Set SCM to Git and enter your repository URL.
  7. Set Script Path to Jenkins/jenkins_scenarios/scenario_01_docker_build/Jenkinsfile.
  8. Click Save.

βœ… How to Run the Pipeline

  1. Click "Build with Parameters".
  2. Set the APP_VERSION parameter (1–5) to choose which app version to build.
  3. Click Build.
  4. Watch the console output for build, run, and test steps.
  5. Check for success or failure messages.

βœ… What the Pipeline Does

  • Cleans up any containers running on port 3000
  • Builds the Docker image for the selected app version
  • Runs the container and exposes port 3000
  • Tests the HTTP response from the app
  • Cleans up the container after the test

πŸ§ͺ Chaos Testing Scenarios

βœ… Scenario 1: Docker Build Failures

// Simulate Docker build failures
stage('Build Docker Image') {
    steps {
        script {
            // Randomly fail builds to test resilience
            if (Math.random() < 0.1) {
                error "Simulated Docker build failure"
            }

            sh "docker build -t chaos-app:${APP_VERSION} ."
        }
    }
}

βœ… Scenario 2: Container Startup Delays

// Simulate slow container startup
stage('Run Container') {
    steps {
        script {
            sh "docker run -d -p 3000:3000 --name chaos-app chaos-app:${APP_VERSION}"

            // Add artificial delay
            sleep 10

            // Test if app responds
            sh "curl -f http://localhost:3000/health || exit 1"
        }
    }
}

βœ… Troubleshooting

  • Docker build fails:
  • Ensure Docker is running and the Docker socket is mounted in Jenkins.
  • Check for typos in the APP_VERSION parameter.
  • App does not respond:
  • Check the container logs in Jenkins output.
  • Make sure the app version exists in the repo.
  • Permission errors:
  • Make sure Jenkins has permission to run Docker commands (mount /var/run/docker.sock).

βœ… Useful Commands

  • See running containers: bash docker ps
  • Check logs for a container: bash docker logs <container_id>
  • Remove a container: bash docker rm -f <container_id>

πŸ“Š Monitoring & Reporting

βœ… Pipeline Metrics

  • Build success rate
  • Average build time
  • Docker image size trends
  • Container startup time

βœ… Chaos Metrics

  • Number of simulated failures
  • Recovery time from failures
  • System resilience score

Next: Scenario 02: Testcontainers | Scenario 03: HTML Reports | Scenario 04: Manage Secrets | Scenario 05: Deploy to EKS


This scenario helps you master Docker builds in Jenkins and prepares you for more advanced CI/CD chaos! πŸ”₯