You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows10环境下如何通过Dockerfile构建含预定义任务的Jenkins主节点镜像

Hey there! I totally get how frustrating it is to spend two days digging through docs and only get partial results when building a custom Jenkins master image. Let's walk through how to bake in all the pieces you need—plugins, users, pre-configured jobs, agent nodes, and general settings—so you can get a fully functional Jenkins instance right out of the box.

Building a Fully Configured Jenkins Master Docker Image

1. Start with a Solid Base Image

First, use the official Jenkins LTS image as your foundation—it's well-maintained and avoids compatibility headaches. For Windows 10, make sure your Docker is using the WSL 2 backend (it's faster and handles Linux containers better than the old Hyper-V setup).

In your Dockerfile, start with:

FROM jenkins/jenkins:lts
USER root

We switch to root first to handle setup tasks that need elevated permissions, then switch back later.

2. Pre-Install Required Plugins

Jenkins has a built-in script install-plugins.sh that makes installing plugins a breeze. Create a plugins.txt file in your build directory with all the plugins you need (include versions if you want consistency):

git:latest
maven-plugin:latest
docker-plugin:latest
credentials-binding:latest
ssh-slaves:latest
# Add any other plugins you use regularly

Then add this to your Dockerfile:

COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
RUN jenkins-plugin-cli -f /usr/share/jenkins/ref/plugins.txt

3. Create a Pre-Defined Admin User

Hardcoding users directly in config.xml can break across Jenkins versions, so use a Groovy init script instead—it's the supported way to bootstrap users. Create a file init.groovy.d/create-admin-user.groovy:

import jenkins.model.Jenkins
import hudson.security.User
import hudson.security.HudsonPrivateSecurityRealm

def jenkins = Jenkins.getInstance()
def realm = new HudsonPrivateSecurityRealm(false)
def username = "admin"
def password = "your-secure-password"

// Create user if they don't exist
if (!User.exists(username)) {
    realm.createAccount(username, password)
    jenkins.setSecurityRealm(realm)
    jenkins.save()
}

Copy this into your image with:

COPY init.groovy.d/ /usr/share/jenkins/ref/init.groovy.d/

4. Apply General Jenkins Configuration

For global settings like tool paths (JDK, Maven), proxy settings, or build retention policies, you can either:

  • Export the config.xml from a working Jenkins instance and copy it into /usr/share/jenkins/ref/
  • Use another Groovy script to set these values programmatically (more maintainable long-term)

Example Groovy script for setting JDK and Maven paths (init.groovy.d/tool-config.groovy):

import jenkins.model.Jenkins
import hudson.tools.JDKInstallation
import hudson.tools.MavenInstallation

def jenkins = Jenkins.getInstance()

// Configure JDK
def jdkInstall = new JDKInstallation("OpenJDK11", "/usr/local/openjdk-11", [])
jenkins.getDescriptorByType(hudson.tools.JDKInstallation.DescriptorImpl.class).setInstallations(jdkInstall)

// Configure Maven
def mavenInstall = new MavenInstallation("Maven3", "/usr/share/maven", [])
jenkins.getDescriptorByType(hudson.tools.MavenInstallation.DescriptorImpl.class).setInstallations(mavenInstall)

jenkins.save()

5. Add Pre-Defined Jobs

If you have existing jobs you want to include, simply copy their job directories into the image. First, export the job from your current Jenkins instance (zip up the jobs/YourJobName folder), then unzip it in your build directory. Add this to your Dockerfile:

COPY jobs/ /var/jenkins_home/jobs/
RUN chown -R jenkins:jenkins /var/jenkins_home/jobs

The chown step is crucial—Jenkins runs as the jenkins user, so it needs ownership of these files to modify them later.

6. Configure Agent Nodes

To pre-configure agent nodes (either static SSH agents or Docker cloud agents), use another Groovy script. Example for a static SSH agent (init.groovy.d/configure-agent.groovy):

import jenkins.model.Jenkins
import hudson.slaves.DumbSlave
import hudson.slaves.SSHLauncher
import hudson.plugins.sshslaves.verifiers.NonVerifyingKeyVerificationStrategy

def jenkins = Jenkins.getInstance()

// Create a static SSH agent
def agentName = "my-static-agent"
def agentDescription = "Pre-configured static SSH agent"
def agentRemoteFS = "/home/jenkins"
def agentLabels = "build-node"
def agentLauncher = new SSHLauncher("agent-hostname", 22, "jenkins-agent-key", "", "", "", "", new NonVerifyingKeyVerificationStrategy())

def agent = new DumbSlave(agentName, agentDescription, agentRemoteFS, "1", Node.Mode.NORMAL, agentLabels, agentLauncher, new hudson.slaves.RetentionStrategy.Always(), [])

// Add agent if it doesn't exist
if (!jenkins.getNode(agentName)) {
    jenkins.addNode(agent)
    jenkins.save()
}

Adjust the hostname, credentials, and settings to match your agent setup.

7. Final Dockerfile Cleanup

Switch back to the jenkins user before finishing, and set the working directory:

USER jenkins
WORKDIR /var/jenkins_home

8. Build and Test Your Image

On Windows 10, open PowerShell or WSL 2 terminal, navigate to your build directory, and run:

docker build -t my-custom-jenkins .
docker run -p 8080:8080 -p 50000:50000 my-custom-jenkins

Wait a minute for Jenkins to start, then visit http://localhost:8080—you should be able to log in with your pre-defined admin user, see all your plugins, pre-configured jobs, and agent nodes ready to go.

Quick Windows 10 Tips

  • If you run into file permission issues when copying files, make sure your build directory is in the WSL 2 filesystem (e.g., /home/your-user/jenkins-build) instead of a Windows folder—this avoids cross-filesystem permission quirks.
  • Double-check that Docker Desktop is set to use WSL 2 (Settings > Resources > WSL Integration).

内容的提问来源于stack exchange,提问作者aoki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:41:49