0Pricing
Docker & DevOps Fundamentals · Lesson

Building Docker Images with Jenkins

Configure Jenkins jobs to automatically build and push Docker images to a registry upon code changes.

Building Docker Images with Jenkins is a free Docker & DevOps Fundamentals lesson on CoddyKit — lesson 2 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.

Automating Docker Builds

Manually building Docker images can be slow, inconsistent, and prone to errors. This is especially true in a team environment or when you have frequent code changes.

Automation is key to reliable and efficient software delivery. In this lesson, we'll learn how to integrate Jenkins with Docker to automate image creation and pushing.

Building Docker Images with Jenkins — illustration 1

Jenkins & Docker Prerequisites

For Jenkins to build Docker images, it needs to interact with a Docker daemon. This means the Docker client must be installed on the machine or agent where your Jenkins job will run.

You'll also need:

  • A running Jenkins server (from Lesson 1)
  • Access to a Docker registry (e.g., Docker Hub)
  • A sample application and its Dockerfile

Installing Docker on Jenkins Host

To allow Jenkins to execute Docker commands, ensure Docker is installed and configured on the Jenkins server or agent. The Jenkins user often needs permissions to interact with the Docker daemon.

Here's how you might set it up on a Linux system:

sudo apt-get update
sudo apt-get install -y docker.io
sudo usermod -aG docker jenkins
sudo systemctl restart docker
sudo systemctl restart jenkins

Securing Registry Credentials

To push images to a Docker registry, Jenkins needs credentials. It's crucial to store these securely, not directly in your Jenkinsfile or job configuration.

Jenkins's built-in Credentials feature is perfect for this:

  • Go to Manage Jenkins > Manage Credentials.
  • Add a new Username with password credential for your Docker registry.
  • Give it a descriptive ID (e.g., dockerhub-credentials) to reference later.

Sample Flask Application

Let's use a very simple Python Flask application. We'll put this in a file named app.py. It will serve a basic 'Hello' message.

We'll also need a requirements.txt file to specify our Python dependencies.

# app.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello from CoddyKit!'

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)

# requirements.txt
Flask==2.0.2

Creating the Dockerfile

Next, we'll define our Docker image using a Dockerfile. This file will instruct Docker how to build our application image.

Place this Dockerfile in the same directory as your app.py and requirements.txt.

# Dockerfile
FROM python:3.9-slim-buster
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app.py .
EXPOSE 5000
CMD ["python", "app.py"]

Jenkins Job: Build Step

Now, let's configure a Jenkins Freestyle job. In the Build Steps section, add an Execute shell step. This command will build your Docker image.

Remember to replace yourusername with your Docker Hub username or registry path.

# In Jenkins 'Execute shell' build step:
docker build -t yourusername/coddykit-app:latest .

Jenkins Job: Login & Push Step

After building the image, we need to push it to our Docker registry. This typically requires logging in. We'll use the credentials stored earlier.

Add another Execute shell step. Jenkins will inject the credentials as environment variables (e.g., DOCKER_USER, DOCKER_PASS) if configured correctly.

# In Jenkins 'Execute shell' build step:
echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
docker push yourusername/coddykit-app:latest

Automating Build Triggers

The power of CI/CD comes from automation. Jenkins can automatically trigger these build and push jobs based on various events:

  • SCM Polling: Jenkins periodically checks your Git repository for new commits.
  • Webhooks: Your Git provider (GitHub, GitLab, Bitbucket) sends a notification to Jenkins whenever code is pushed.
  • Upstream Projects: Triggered upon the successful completion of another Jenkins job.

Testing the Jenkins Job

After configuring the build steps and triggers, save your Jenkins job. You can manually trigger a build to test it. Watch the console output for successful Docker image building and pushing.

A successful run means your CI pipeline can now automatically produce and store new Docker images whenever your code changes!

Quick Check

You've set up a Jenkins job to build and push a Docker image. Your Git repository has new code changes.

Recap: Build & Push

Great job! In this lesson, you learned how to:

  • Prepare your Jenkins environment for Docker interaction.
  • Securely manage Docker registry credentials in Jenkins.
  • Configure a Jenkins job to build a Docker image using a Dockerfile.
  • Push the newly built image to a Docker registry.
  • Understand different ways to trigger automated builds.

Automating these steps is a core part of Continuous Integration and sets the stage for Continuous Delivery!

Frequently asked questions

Is the “Building Docker Images with Jenkins” lesson free?

Yes — the full text of “Building Docker Images 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 “Building Docker Images with Jenkins”?

Configure Jenkins jobs to automatically build and push Docker images to a registry upon code changes. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Building Docker Images 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