Step‑by‑Step Guide to Installing and Configuring Jenkins with Java and Docker on Ubuntu
This tutorial walks through installing Java (JRE/JDK), setting JAVA_HOME, installing Jenkins via apt or Docker, configuring Jenkins service and Nginx reverse proxy, handling common timezone and permission errors, and resetting the admin password, providing complete commands and configuration snippets for a functional CI/CD setup on Ubuntu.
What is Jenkins?
Jenkins is an open‑source, extensible web‑based platform for continuous integration, delivery and deployment of software projects.
1. Install Java
Update the package index and install the default JRE or a specific OpenJDK version.
sudo apt update
java -version # verify installationIf Java is missing, install it with:
sudo apt install default-jre # JRE only
sudo apt install default-jdk # JDK (includes JRE)Verify the JDK installation: javac -version For a specific version such as OpenJDK 8 (long‑term support), run:
sudo apt install openjdk-8-jdk java -version2. Install Jenkins (APT method)
Add the Jenkins repository key and source, then install the package:
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkinsThe installation creates a systemd service, a dedicated jenkins user, and default configuration files.
Service script: /etc/init.d/jenkins Log file: /var/log/jenkins/jenkins.log Configuration: /etc/default/jenkins (e.g., JENKINS_HOME, HTTP_PORT)
Control the service with:
sudo systemctl status jenkins.service # view status
sudo systemctl restart jenkins.service # restart3. Configure Java environment for Jenkins
Locate the Java binary, e.g. /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java.
Edit /etc/environment and add:
JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java"Reload the file and verify:
source /etc/environment
echo $JAVA_HOME4. Adjust Jenkins configuration
Edit /etc/default/jenkins to set a custom home directory, e.g. JENKINS_HOME=/home/www/jenkins, and change the listening port, e.g. HTTP_PORT=8081.
Give the Jenkins user appropriate permissions on the Docker‑based Jenkins directory:
sudo chmod -R 777 /home/www/jenkins
sudo gpasswd -a jenkins www # add to www group5. Set up Nginx as a reverse proxy (Docker container)
server {
listen 443 ssl http2;
server_name jenkins.tinywan.com;
ssl_certificate /etc/letsencrypt/jenkins.tinywan.com/full_chain.pem;
ssl_certificate_key /etc/letsencrypt/jenkins.tinywan.com/private.key;
location / {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect http:// https://;
proxy_pass http://172.17.0.1:8081;
proxy_read_timeout 90;
proxy_http_version 1.1;
proxy_request_buffering off;
proxy_buffering off;
add_header 'X-SSH-Endpoint' 'p2p.herbeauty.top:50000' always;
}
}6. Docker‑based Jenkins installation
Run a basic Jenkins container:
docker run -d -p 8084:8080 --name tinywan-jenkins --user root -v /host_mnt/d/Jenkins2:/var/jenkins_home jenkinsFor a stable LTS version:
docker run -d --name lnmp-jenkins --user root -p 8080:8080 -p 50000:50000 jenkins/jenkins:2.165Blue Ocean (recommended) with Nginx networking:
docker run -d --name lnmp-jenkins \
--network dnmp_backend --network-alias jenkins \
--user root -p 8081:8080 \
-v $HOME/jenkins:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$HOME":/home jenkinsci/blueoceanEnter the container:
docker exec -it tinywan-jenkins bash7. Common issues and fixes
Timezone
Copy host timezone files into the container:
docker cp /etc/localtime lnmp-jenkins:/etc/
docker cp /etc/timezone lnmp-jenkins:/etc/Permission problems
Add the Jenkins user to the Docker group and restart:
sudo gpasswd -a jenkins docker
sudo systemctl restart jenkins.serviceDocker socket permission error
Ensure the Jenkins user has access to /var/run/docker.sock or run the container with --user root.
Admin password forgotten
Remove the security section from /var/lib/jenkins/config.xml (delete <useSecurity>true</useSecurity> and related blocks).
Restart Jenkins.
Navigate to “Manage Jenkins → Configure Global Security”, enable security, select “Jenkins’ own user database”, and save.
After saving, “Manage Users” appears; create a new admin user and set a password.
8. Automated deployment script example
Test environment (pull latest code, stash local changes):
# cd to project directory
cd /home/www/dnmp/www/jl-pay-develop
# stash local modifications
git stash
# reset and pull latest test branch
git reset --hard origin/test
git pull origin test
# re‑apply stashed changes
git stash popProduction environment:
cd /home/www/lnmp/www/juhepay
git reset --hard origin/master
git pull origin masterSSH key deployment (fix host key change error):
# Remove old host key
ssh-keygen -f "/root/.ssh/known_hosts" -R 47.112.97.230References
Installing Java on Ubuntu 18.04
https://jenkins.io/zh/doc/book/installing/
PHP continuous integration with Jenkins
SpringBoot + Docker + Git + Jenkins CI/CD example
Docker + Jenkins automatic build and deployment
Changing timezone in Alpine‑based Docker containers
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
