0
0
Jenkinsdevops~5 mins

Installing with Docker in Jenkins - Step-by-Step CLI Walkthrough

Choose your learning style9 modes available
Introduction
Installing Jenkins with Docker lets you run Jenkins easily without manual setup. Docker packages Jenkins and its environment so you can start it quickly on any machine.
When you want to try Jenkins without installing it directly on your computer.
When you need to run Jenkins on different machines with the same setup.
When you want to isolate Jenkins from other software to avoid conflicts.
When you want a quick way to start and stop Jenkins without complex installation.
When you want to use Jenkins in a containerized environment for easy updates.
Commands
This command downloads the official Jenkins image with the latest stable version from Docker Hub.
Terminal
docker pull jenkins/jenkins:lts
Expected OutputExpected
lts: Pulling from jenkins/jenkins Digest: sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Status: Downloaded newer image for jenkins/jenkins:lts docker.io/jenkins/jenkins:lts
This command starts a Jenkins container in the background, mapping ports so you can access Jenkins on your computer's port 8080.
Terminal
docker run -d -p 8080:8080 -p 50000:50000 --name my-jenkins jenkins/jenkins:lts
Expected OutputExpected
a1b2c3d4e5f678901234567890abcdef1234567890abcdef1234567890abcdef
-d - Run container in detached mode (in the background)
-p 8080:8080 - Map host port 8080 to container port 8080 for web access
-p 50000:50000 - Map host port 50000 to container port 50000 for Jenkins agents
--name my-jenkins - Assign a name to the container for easy reference
This command shows the startup logs of the Jenkins container, including the initial admin password needed to unlock Jenkins.
Terminal
docker logs my-jenkins
Expected OutputExpected
Running from: /usr/share/jenkins/jenkins.war 2024-06-01 12:00:00.000+0000 [id=1] INFO org.eclipse.jetty.server.Server#main: Started @12345ms ************************************************************* Jenkins initial setup is required. An admin user has been created and a password generated. Please use the following password to proceed to installation: 1234567890abcdef1234567890abcdef *************************************************************
This command stops the running Jenkins container safely.
Terminal
docker stop my-jenkins
Expected OutputExpected
my-jenkins
This command starts the Jenkins container again after it was stopped.
Terminal
docker start my-jenkins
Expected OutputExpected
my-jenkins
Key Concept

If you remember nothing else from this pattern, remember: Docker lets you run Jenkins quickly and consistently by packaging it in a container.

Common Mistakes
Not mapping ports with -p flag when running the container
Without port mapping, you cannot access Jenkins from your browser on your computer.
Always use -p 8080:8080 to map Jenkins web interface port.
Not checking logs to get the initial admin password
You cannot unlock Jenkins and proceed with setup without this password.
Run docker logs my-jenkins and copy the password shown to unlock Jenkins.
Using docker run without --name and then having trouble managing the container
Without a container name, you must use container IDs which are harder to remember.
Use --name my-jenkins to give the container a friendly name.
Summary
Pull the official Jenkins Docker image using docker pull.
Run Jenkins in a container with docker run, mapping ports for access.
Check container logs to find the initial admin password.
Stop and start the Jenkins container using docker stop and docker start.