如何通过Docker启动Jenkins CLI?已构建含相关组件的镜像
Hey there! Let's break down how to get your Jenkins CLI up and running with your Docker image, plus fix a few small issues in your Dockerfile first—since it looks like it cuts off before completing Jenkins installation.
First off, your original script has a couple syntax typos and is missing the actual Jenkins installation steps. Here's a polished, complete version that includes all your desired tools plus Jenkins:
FROM ubuntu:14.04 # Update system and install base dependencies RUN apt update && \ apt upgrade -y && \ apt install -y default-jdk curl wget git maven nano unzip && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* # Set accurate Java environment variables for Ubuntu 14.04 ENV JAVA_HOME /usr/lib/jvm/java-7-openjdk-amd64 ENV PATH $JAVA_HOME/bin:$PATH # Install Jenkins (missing from your original script) RUN wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | apt-key add - && \ echo "deb http://pkg.jenkins.io/debian-stable binary/" >> /etc/apt/sources.list.d/jenkins.list && \ apt update && \ apt install -y jenkins && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* # Expose standard Jenkins ports (web UI + agent communication) EXPOSE 8080 50000 # Start Jenkins when the container launches CMD ["java", "-jar", "/usr/share/jenkins/jenkins.war"]
Quick fixes explained:
- Replaced the typo
$$with&&in your original RUN command - Added proper cleanup commands to shrink the final image size
- Completed the Jenkins installation workflow (critical for your use case)
- Set a precise
JAVA_HOMEpath that matches Ubuntu 14.04's default JDK location
Once you build your image (docker build -t my-custom-jenkins .), launch the container with port mappings to access Jenkins:
docker run -d -p 8080:8080 -p 50000:50000 --name jenkins-container my-custom-jenkins
You have two straightforward ways to use Jenkins CLI:
1. Run CLI Inside the Container
First, open a shell in your running Jenkins container:
docker exec -it jenkins-container /bin/bash
Inside the container, grab the initial admin password (required for first-time access):
cat /var/lib/jenkins/secrets/initialAdminPassword
Locate the Jenkins CLI jar (it's bundled with Jenkins) and log in:
java -jar /var/lib/jenkins/war/WEB-INF/jenkins-cli.jar -s http://localhost:8080/ login # Paste the initial password when prompted
Now you can pass parameters to CLI commands. For example, to trigger a parameterized job:
java -jar /var/lib/jenkins/war/WEB-INF/jenkins-cli.jar -s http://localhost:8080/ build "MyParameterizedJob" -p ENV=production -p BUILD_NUMBER=123
2. Run CLI From Your Host Machine
If you prefer using CLI directly from your host, first download the CLI jar from Jenkins' web UI:
curl http://localhost:8080/jnlpJars/jenkins-cli.jar -o jenkins-cli.jar
Grab the initial password from the container (no need to enter the shell):
docker exec jenkins-container cat /var/lib/jenkins/secrets/initialAdminPassword
Log in and run parameterized commands from your host:
java -jar jenkins-cli.jar -s http://localhost:8080/ login # Enter the initial password when prompted # Example: Trigger the same parameterized job java -jar jenkins-cli.jar -s http://localhost:8080/ build "MyParameterizedJob" -p ENV=staging -p BUILD_NUMBER=124
To avoid entering passwords every time, generate an API token in Jenkins: Go to Your Profile > Configure > API Token > Generate Token. Then use this token in CLI commands with the -auth flag:
java -jar jenkins-cli.jar -s http://localhost:8080/ -auth admin:your-api-token build "MyParameterizedJob" -p ENV=dev
内容的提问来源于stack exchange,提问作者Hithesh

