Skip to main content

Command Palette

Search for a command to run...

To deploy Jenkins using a Docker image

Published
3 min read
A
Hi there! I'm a passionate AWS DevOps Engineer with 2+ years of experience in building and managing scalable, reliable, and secure cloud infrastructure. I'm excited to share my knowledge and insights through this blog. Here, you'll find articles on: AWS Services: Deep dives into core AWS services like EC2, S3, Lambda, and more. DevOps Practices: Best practices for CI/CD, infrastructure as code, and automation. Security: Tips and tricks for securing your AWS environments. Serverless Computing: Building and deploying serverless applications. Troubleshooting: Common issues and solutions in AWS. I'm always eager to learn and grow, and I hope this blog can be a valuable resource for fellow DevOps enthusiasts. Feel free to connect with me on [LinkedIn/Twitter] or leave a comment below!

Prerequisites :

  1. AWs account with EC2 instance.

  2. 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 Dockerfile in your project directory, then build the Docker image.
    docker build -t my-jenkins-image .

This command creates a Docker image and tags it as my-jenkins-image.

  1. Run the Docker container

     docker run -d -p 8080:8080 --name my-jenkins-container my-jenkins-image
    
    • The -d option 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.
  2. 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

More from this blog

CloudHub

94 posts

Hi there! I'm a passionate AWS DevOps Engineer with 2+ years of experience in building ,reliable, and secure cloud infrastructure. I'm excited to share my knowledge and insights through this blog.