0Pricing
Docker & DevOps Fundamentals · Lesson

Automated Deployment with Jenkins

Create a CI/CD pipeline in Jenkins to deploy your Docker containers to a target environment.

Automated Deployment with Jenkins is a free Docker & DevOps Fundamentals lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Docker & DevOps Fundamentals learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Automated Deployment Intro

Welcome to the final lesson on CI/CD with Jenkins and Docker! Today, we'll focus on automating the deployment of your Docker containers.

Automated deployment is the process of releasing software to production or staging environments without manual intervention. It's a critical part of Continuous Delivery and Deployment, ensuring speed, consistency, and reliability.

Automated Deployment with Jenkins — illustration 1

Why Automate Deployment?

Manually deploying applications can be slow, error-prone, and inconsistent. Automation addresses these challenges:

  • Speed: Deploy new features or fixes rapidly.
  • Reliability: Reduce human error with standardized processes.
  • Consistency: Ensure every deployment follows the same steps.
  • Efficiency: Free up developers to focus on coding, not deployment tasks.

Deployment Pipeline Stages

A typical Jenkins pipeline for Docker deployment involves several key stages:

  • Build: Build the Docker image (covered in previous lessons).
  • Test: Run automated tests against the image.
  • Push: Push the image to a Docker Registry.
  • Deploy: Pull the image from the registry and run it on a target server.
  • Verify: Confirm the new deployment is healthy.

We'll focus on the 'Deploy' and 'Verify' stages today.

Target Environment Setup

Before Jenkins can deploy, your target server needs to be ready. This typically means:

  • Docker Installed: The target server must have Docker Engine installed.
  • SSH Access: Jenkins needs secure Shell (SSH) access to the target server.
  • User Permissions: The SSH user needs permissions to run Docker commands.

We usually manage SSH credentials securely within Jenkins.

Jenkins Credentials for SSH

To connect to a remote server, Jenkins uses SSH credentials. These are typically SSH username/private key pairs stored securely in Jenkins' credential manager.

When defining a pipeline, you can tell Jenkins to use these credentials for specific stages, allowing it to execute commands on your target server.

Basic Deployment Script Example

Here's a simple shell script that could be executed on a target server to update a Docker container. It stops the old one, removes it, pulls the new image, and runs it.

APP_NAME and IMAGE_NAME would be passed as environment variables.

#!/bin/bash

# Stop and remove existing container
docker stop ${APP_NAME} || true
docker rm ${APP_NAME} || true

# Pull the new Docker image
docker pull ${IMAGE_NAME}

# Run the new container
docker run -d --name ${APP_NAME} -p 80:80 ${IMAGE_NAME}

echo "Deployment of ${APP_NAME} complete!"

Integrating with Jenkins Pipeline

In a Jenkinsfile, you can use the sshPublisher plugin or a simple sh step with SSH agent forwarding to execute commands on a remote host. Here's how a deploy stage might look using a basic sh step, assuming SSH is configured.

agent { label 'remote-deploy-host' } means this stage runs on the target server.

stage('Deploy to Production') {
  agent { label 'remote-deploy-host' } 
  steps {
    withEnv([
      "APP_NAME=my-webapp",
      "IMAGE_NAME=myregistry/my-webapp:latest"
    ]) {
      sh '''
        docker stop ${APP_NAME} || true
        docker rm ${APP_NAME} || true
        docker pull ${IMAGE_NAME}
        docker run -d --name ${APP_NAME} -p 80:80 ${IMAGE_NAME}
        echo "Deployment successful!"
      '''
    }
  }
}

Achieving Rolling Updates

For zero-downtime deployments, a simple stop-and-start isn't enough. You'd typically use a strategy like:

  • Rolling Update: Gradually replace old containers with new ones, one by one, ensuring service availability.
  • Blue/Green Deployment: Deploy the new version (Green) alongside the old (Blue), then switch traffic.

While Jenkins can orchestrate these, tools like Docker Swarm or Kubernetes offer native support for more advanced strategies.

Deployment Verification

After deploying, it's crucial to verify the application is running correctly. This can involve:

  • Health Checks: Simple HTTP GET requests to the application's health endpoint.
  • Log Monitoring: Check container logs for errors.
  • Integration Tests: Run a quick set of tests against the newly deployed version.

Your Jenkins pipeline can include a 'Verify' stage to automate these checks.

Verify Your Knowledge

Consider the following Jenkinsfile snippet for a deployment stage. What is the primary purpose of docker stop ${APP_NAME} || true?

Recap: Automated Deployment

Great job! You've learned how to approach automated deployment with Jenkins and Docker.

  • Automated deployment ensures fast, reliable, and consistent releases.
  • Target environments need Docker and SSH access.
  • Jenkins uses secure credentials to connect to remote servers.
  • Deployment scripts typically stop old containers, pull new images, and start new ones.
  • Verification steps are crucial to confirm successful deployment.

This wraps up our CI/CD with Jenkins & Docker course. Keep practicing to master these powerful tools!

Frequently asked questions

Is the “Automated Deployment with Jenkins” lesson free?

Yes — the full text of “Automated Deployment with Jenkins” is free to read here on the web, and the Docker & DevOps Fundamentals course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Docker & DevOps Fundamentals course, upgrade to CoddyKit PRO.

What will I learn in “Automated Deployment with Jenkins”?

Create a CI/CD pipeline in Jenkins to deploy your Docker containers to a target environment. You practise Docker & DevOps Fundamentals with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Docker & DevOps Fundamentals?

No prior experience is required. Docker & DevOps Fundamentals on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Automated Deployment with Jenkins” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Docker & DevOps Fundamentals lesson?

Yes. Every Docker & DevOps Fundamentals lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Jenkins
  2. Building Docker Images with Jenkins
  3. Automated Deployment with Jenkins
  4. Jenkins Pipeline as Code with Jenkinsfile
← Back to Docker & DevOps Fundamentals