To deploy Jenkins using a Docker image
Prerequisites :
AWs account with EC2 instance.
Docker Hub account.
Docker is a platform that runs applications in isolated environments called containers, where applications like Jenkins are downloaded as read-only images and run as containers, with images stored permanently and containers stored temporarily.
Use the official jenkins/jenkins image from Docker Hub for a production-ready Jenkins LTS release, but note it lacks Docker CLI and Blue Ocean plugins.
Copy the command from Docker hub
docker pull jenkins/jenkins
Build the Docker Image
docker build -t my-jenkins-image .
Run the Container
docker run -d -p 8080:8080 --name my-jenkins-container my-jenkins-image
-d: Runs the container in the background (detached mode).-p 8080:8080: Connects port 8080 on the host to port 8080 in the container.--name my-jenkins-container: Gives the container a name for easy reference.
Access Jenkins
Open your web browser and go to
http://<your_host_ip>:8080, replacing<your_host_ip>with your host machine's IP address.To unlock Jenkins, find the initial Admin Password in the container's logs by using
docker logs my-jenkins-container.Finish the initial setup wizard by installing recommended plugins and creating an admin user.
(Optional) Persist Jenkins Data
- To keep Jenkins data like jobs and configurations safe, you can attach a volume to the container.
docker run -d -p 8080:8080 --name my-jenkins-container -v /path/to/your/jenkins_data:/var/jenkins_home my-jenkins-image
Replace /path/to/your/jenkins_data with the directory path on your host machine where you want to store Jenkins data.
Key Considerations
Security: In production, use a dedicated user and group for Jenkins and limit permissions appropriately.
Plugins: Install essential plugins like the Git plugin and Kubernetes plugin within the container.
Security: Set a strong password for the initial admin user and activate security features such as two-factor authentication.
Resource Limits: Set limits on CPU and memory for the container to avoid using too many resources.
Dockerfile - Create a file and add the following code.
FROM jenkins/jenkins:lts
# Copy your Jenkins configuration files (if any)
# into the container's /usr/share/jenkins/ref/ directory
# (This is optional)
# COPY jenkins_home/config.xml /usr/share/jenkins/ref/config.xml
# Expose the Jenkins port (8080 by default)
EXPOSE 8080
# Run Jenkins in the foreground
CMD ["java", "-jar", "/usr/share/jenkins/lib/jenkins.war"]
To build and run the Docker image:
- Save the Dockerfile and build the Docker image: Save the provided code as
Dockerfilein your project directory, then build the Docker image.
- Save the Dockerfile and build the Docker image: Save the provided code as
docker build -t my-jenkins-image .
This command creates a Docker image and tags it as my-jenkins-image.
Run the Docker container
docker run -d -p 8080:8080 --name my-jenkins-container my-jenkins-imageThe
-doption runs the container in detached mode, meaning it operates in the background.-p 8080:8080: Connects port 8080 on your computer to port 8080 in the container.--name my-jenkins-container: Assigns a name to the container for easier management.
Access Jenkins:
Open a web browser and visit
http://<your_host_ip>:8080, replacing<your_host_ip>with your computer's IP address.Follow the on-screen instructions to unlock Jenkins and finish the initial setup.
Few Screencap







