(38976) Run Jenkins in Docker Container - Jenkins Pipeline Tutorial for Beginners 1/4 -…
- User
(38976) Run Jenkins in Docker Container - Jenkins Pipeline Tutorial for Beginners 1/4 - YouTube https://www.youtube.com/watch?v=pMO26j2OUME
Transcript: (00:00) so in this video we're gonna set up Jenkins from scratch in a docker container then we're gonna create a multi branch pipeline and connected with a git repository during this step I will also explain how to create different types of credentials in Jenkins and finally I will show you the basics of a Jenkins file to configure this multi branch pipeline so step one is to run Jenkins in a docker container so to use that we need to find an official image for Jenkins on docker hub so let's go and as you see (00:44) here Jenkins results in an official image and some other images and if you click here you see that this image the official image has been deprecated and the last version is 2.6 T which is very old Jenkins version and instead of it we're gonna use this image here so that would be actually this one because this one is now officially maintained by the Jenkins community and also if you see the text the latest image actually has Jenkins version of to point 219 which for most of the plugins it's important to have a (01:30) higher Jenkins version otherwise they can't be installed so we're going to take this image and this name of the image so on my command line I'm gonna execute docker run using this image right so I'm gonna dock run and the image however before I run this I need to add some options like I need to expose the port so that I can access it from my browser etc so let's actually go back and see the documentation of how to run this so all the explanation is here so these are some of the options that (02:08) we're gonna use and actually let's go and do it so first one is I'm gonna expose the ports and the first part I'm gonna expose is port 8080 the Jenkins application inside container will run it port 8080 because Jenkins runs on Tomcat which by default starts at this port and I'm gonna bind it to my hosts port 8080 another port we can expose is 50,000 which is basically a port where drink is master and slave communicate so this will enable my Jenkins to find slaves in case I had some so the (02:45) ports are open now another thing I'm gonna do is I'm gonna run it in detach mode and I'm also gonna bind volumes to it so to do that I'm gonna use minus B and I'm gonna use a named volume which I'm gonna call Jenkins home meaning I don't have to have this as a folder it's gonna be automatically created I'm just giving it a name which can be whatever you want and I'm gonna bind it to directory inside Jenkins container drink its image under slash bar slash Junkins hope this director is real and it's (03:28) inside of docker container and this will create automatically create a volume now you have to be careful here because if you were to use a host volume here which basically means that you take an already existing directory on your laptop machine and binding it to the Jenkins container you might get some permission issues meaning the Jenkins user might not be able to write to that folder and that could be a problem you can actually check out my video which I made about docker volumes where you can learn in more detail how this works and the (04:05) reason why we need to create a volume for Jenkins is to have data persistency for Jenkins so when we remove the container and restarted recreated the data will still be there and the reason why we need to persist this data is because all the Jenkins builds all the configuration and all the Jenkins plugins and also Jenkins users will be stored there so without this data you will basically have to reinitialize the whole Jenkins so important part here and we're gonna use the latest take of Jenkins and these are all the options we (04:44) need so I'm gonna correct this one here look at here and I'm gonna run this and it's pulling the image from the docker hub it's gonna start in just couple of seconds so the image has been pulled and the docker container must be running already so if I do docker PS I see my Jenkins container running and we actually need its locks so I'm gonna do docker locks with the container ID and I'm gonna see that Jenkins started and its initial setup is needed and this is the password I'm gonna need to initialize it so I'm (05:30) gonna copy that and if I go to localhost 8080 I will paste that password and it should start initializing so I'm going to go with the suggested plugins and this is gonna take some time to install all the plugins so depends on what technologies your application uses or what source management tools etc he will need different plugins for Jenkins and I'm just gonna go with the community one suggested ones and later of course in Jenkins you can manage these plugins you can delete them you can add new ones etc (06:16) this is just the base so now that it's done I'm gonna create the first admin user let's go at Jenkins user some password really interesting I'm gonna just mine and I'm gonna leave it localhost Jenkins is ready and this is view I get so if I go to new item because I installed all these plugins I see different types of Jenkins projects I can create but default usually have the freestyle project freestyle project is used for simple single tasks like if you want to just run tests for example (07:01) whereas with pipeline you can configure the whole delivery flow like test build package deploy your application etc pipeline is a more recent addition so before that they would just chain multiple freestyle projects to get a pipeline like project the pipeline project type is just for a single branch but in this video we're going to create a multi branch pipeline which will apply to multiple branches of the same repository and if I go to manage Jenkins that's where you as an admin user have all the tools to (07:41) configure Jenkins and under manage plugins you can actually add or delete or manage or update the plugins that you have so in available I have all the plugins that I could install in addition to my Jenkins and install I have all the ones that I have actually selected at the beginning you can remove them again or you can add new plugins in the next section of this video I'm gonna show you how to create a multi branch pipeline and how to connect it to your kid repository and how to build your project using that pipeline
Q- make this video translation into step by step details tutor with details docker commends ?
- Assistant
Step-by-Step Tutorial: Running Jenkins in a Docker Container and Setting Up a Multibranch Pipeline
Introduction
In this tutorial, we'll set up Jenkins from scratch using a Docker container. We'll cover:
- Running Jenkins in a Docker container.
- Creating a multibranch pipeline.
- Connecting Jenkins to a Git repository.
- Creating credentials in Jenkins.
- Understanding the basics of a Jenkinsfile for pipeline configuration.
By the end of this tutorial, you'll have a working Jenkins instance running in Docker, configured to automatically build your projects from a Git repository.
Prerequisites
- Docker installed on your machine.
- Basic knowledge of Docker commands.
- Internet connection to pull Docker images and Jenkins plugins.
- A Git repository (e.g., on GitHub) containing your project's source code.
Step 1: Run Jenkins in a Docker Container
1.1. Find the Official Jenkins Docker Image
Before running Jenkins in Docker, we need to find a suitable Docker image. While there used to be an official
jenkinsimage, it's now deprecated. Instead, we'll use thejenkins/jenkinsimage, which is maintained by the Jenkins community.Here's how to find it:
- Visit Docker Hub and search for Jenkins.
- Look for the image named
jenkins/jenkins– this is the current official image maintained by the Jenkins community. - Note that the
jenkins/jenkins:ltstag points to the latest Long-Term Support (LTS) version, which is recommended for production use.
1.2. Pull and Run the Jenkins Docker Image
We'll use the
docker runcommand to pull and start the Jenkins container with the necessary configurations.Docker Command:
docker run \ --name jenkins \ -d \ -p 8080:8080 \ -p 50000:50000 \ -v jenkins_home:/var/jenkins_home \ jenkins/jenkins:ltsExplanation:
docker run: Command to run a new container.--name jenkins: Assigns the name "jenkins" to the container for easy reference.-d: Runs the container in detached mode (in the background).-p 8080:8080: Maps port 8080 of the host to port 8080 of the container (Jenkins web interface).-p 50000:50000: Maps port 50000 of the host to port 50000 of the container (used for Jenkins agents).-v jenkins_home:/var/jenkins_home: Mounts a Docker named volume calledjenkins_hometo persist Jenkins data.jenkins/jenkins:lts: Specifies the Docker image to use.
Note: If you haven't pulled the
jenkins/jenkins:ltsimage before, Docker will download it automatically.1.3. Understanding the Ports and Volumes
Ports:
- Port 8080: Jenkins default web interface port.
- Port 50000: Used for communication between the Jenkins master and its agents.
Volume:
- jenkins_home: A Docker named volume that persists Jenkins data, including configurations, plugins, and build artifacts. This ensures that your Jenkins data is not lost when the container is stopped or removed.
1.4. Start the Jenkins Container
Run the command provided in step 1.2 in your terminal. Docker will pull the Jenkins image (if not already downloaded) and start the container.
1.5. Verify the Jenkins Container is Running
Check that the Jenkins container is running using:
docker psYou should see an entry for the
jenkinscontainer.1.6. Retrieve the Initial Admin Password
Jenkins requires an initial admin password for the first-time setup.
To get the password:
-
View the container logs:
docker logs jenkins -
Look for the lines that contain:
************************************************************* Jenkins initial setup is required. An admin user has been created and a password generated. Please use the following password to proceed to installation: [YOUR_INITIAL_ADMIN_PASSWORD] ************************************************************* -
Copy the password displayed. You'll need this in the next step.
Step 2: Access Jenkins and Complete Initial Setup
2.1. Open Jenkins in Your Browser
Navigate to
http://localhost:8080in your web browser. You should see the Unlock Jenkins screen.2.2. Unlock Jenkins
- Paste the initial admin password you copied from the container logs into the
Administrator passwordfield. - Click Continue.
2.3. Install Suggested Plugins
On the Customize Jenkins page:
- Choose Install suggested plugins. Jenkins will automatically install commonly used plugins.
- Wait for the installation to complete. This may take several minutes.
2.4. Create Your First Admin User
-
Fill out the Create First Admin User form:
- Username: Choose a username (e.g.,
admin). - Password: Create a strong password.
- Confirm password: Re-enter the password.
- Full name: Your name.
- Email address: Your email (useful for notifications).
- Username: Choose a username (e.g.,
-
Click Save and Continue.
2.5. Confirm Jenkins URL
- Jenkins will display the default URL (
http://localhost:8080/). - Confirm or change it if necessary.
- Click Save and Finish.
2.6. Start Using Jenkins
Click Start using Jenkins. You will be redirected to the Jenkins dashboard.
Step 3: Explore Jenkins Dashboard and Project Types
3.1. Jenkins Dashboard Overview
The Jenkins dashboard is where you can create and manage projects, view build history, manage users, and configure Jenkins.
3.2. Create a New Item
-
Click on New Item in the left-hand menu.
-
You'll see several project types:
- Freestyle project: Flexible project type for simple configurations.
- Pipeline: Defines the build process through code (
Jenkinsfile). - Multibranch Pipeline: Automatically creates pipelines for each branch.
- GitHub Organization: Scans GitHub organizations for repositories.
-
For this tutorial, we'll focus on creating a Multibranch Pipeline in a later step.
3.3. Manage Jenkins Plugins
-
From the dashboard, click on Manage Jenkins.
-
Select Manage Plugins.
-
Here you can:
- Updates: See plugins with available updates.
- Available: Browse and install new plugins.
- Installed: View and manage installed plugins.
- Advanced: Configure advanced settings.
Step 4: Create a Multibranch Pipeline
4.1. What is a Multibranch Pipeline?
A Multibranch Pipeline allows Jenkins to automatically discover, manage, and execute pipelines for branches and pull requests in a source control repository.
4.2. Create the Multibranch Pipeline Project
- From the Jenkins dashboard, click on New Item.
- Enter a name for your project (e.g.,
MyMultibranchPipeline). - Select Multibranch Pipeline.
- Click OK.
4.3. Configure the Multibranch Pipeline
4.3.1. Add a Branch Source
- In the project configuration, under Branch Sources, click Add source.
- Choose Git.
4.3.2. Provide the Repository URL
- In the Project Repository field, enter the URL of your Git repository (e.g.,
https://github.com/yourusername/your-repo.git).
4.3.3. Configure Credentials
If your repository is private, you'll need to provide credentials.
-
Next to Credentials, click Add > Jenkins.
-
In the Add Credentials window:
- Kind: Choose the appropriate type (e.g.,
Username with password,SSH Username with private key). - Scope: Leave it as
Global. - Username: Your Git username.
- Password: Your Git password or personal access token.
- ID and Description: Optional.
- Kind: Choose the appropriate type (e.g.,
-
Click OK to save the credentials.
-
Back in the project configuration, select the credentials you just added from the Credentials dropdown.
4.3.4. Configure Branch Discovery (Optional)
- Under Behaviors, you can add or remove behaviors to control which branches are built.
- By default, all branches are scanned and built if they contain a
Jenkinsfile.
4.3.5. Save the Configuration
Click Save at the bottom of the configuration page.
4.4. Build the Multibranch Pipeline
- Jenkins will scan the repository and create jobs for each branch containing a
Jenkinsfile. - You can view the scan logs and job statuses from the project's main page.
Step 5: Understanding and Creating a Jenkinsfile
5.1. What is a Jenkinsfile?
A
Jenkinsfileis a text file that contains the definition of a Jenkins Pipeline and is checked into source control. It allows you to define your build, test, and deployment stages as code.5.2. Basic Structure of a Jenkinsfile
Here's an example of a simple declarative pipeline:
pipeline { agent any stages { stage('Build') { steps { // Commands to build your application sh 'echo "Building..."' } } stage('Test') { steps { // Commands to test your application sh 'echo "Testing..."' } } stage('Deploy') { steps { // Commands to deploy your application sh 'echo "Deploying..."' } } } }5.3. Add a Jenkinsfile to Your Repository
- In your project's root directory, create a file named
Jenkinsfile. - Paste the pipeline script into the
Jenkinsfile. - Customize the stages and steps according to your project's build and deployment process.
- Commit and push the
Jenkinsfileto your repository.
5.4. Jenkins Automatically Detects and Builds the Pipeline
- After pushing the
Jenkinsfile, Jenkins will automatically scan the repository again. - It will detect the updated branch and execute the pipeline defined in the
Jenkinsfile.
Step 6: Managing Credentials in Jenkins
6.1. Why Use Credentials?
Managing credentials securely is crucial for tasks that require authentication, such as accessing private repositories, deployment servers, or integration with other services.
6.2. Types of Credentials
- Username with Password: Used for basic authentication.
- SSH Username with Private Key: Used for SSH-based authentication.
- Secret Text: For tokens or passwords without a username.
- Certificate: For SSL certificates.
- Secret File: Upload a secret file (e.g., JSON key).
6.3. Creating Credentials
- From the Jenkins dashboard, click on Credentials in the left menu.
- Click on System > Global credentials (unrestricted).
- Click Add Credentials on the left.
- Fill in the details based on the type of credentials you need.
- Click OK to save.
6.4. Using Credentials in Pipelines
In your
Jenkinsfile, you can reference credentials using thecredentialsId:pipeline { agent any stages { stage('Example') { steps { withCredentials([usernamePassword(credentialsId: 'my-creds-id', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { sh 'echo $USERNAME' sh 'echo $PASSWORD' } } } } }
Step 7: Additional Jenkins Configuration
7.1. Managing Plugins
- Go to Manage Jenkins > Manage Plugins.
- Install additional plugins as needed.
- Keep your plugins up to date for security and new features.
7.2. System Configuration
- Under Manage Jenkins, explore options like Configure System, Global Tool Configuration, and Configure Global Security.
- Configure tools like JDK, Maven, Git, etc., if your builds require them.
Step 8: Using Jenkins Agents (Optional)
If your builds require more resources or you want to distribute builds across multiple machines, you can set up Jenkins agents.
8.1. Exposing Port 50000
We exposed port 50000 earlier for agent communication. Ensure that this port is accessible from your agents to the Jenkins master.
8.2. Adding a New Agent
- From the Jenkins dashboard, click on Manage Jenkins > Manage Nodes and Clouds.
- Click New Node.
- Configure the node (agent) details, including launch method and credentials.
- Start the agent per the instructions provided by Jenkins.
Conclusion
Congratulations! You've successfully:
- Set up Jenkins in a Docker container.
- Configured Jenkins for initial use.
- Created a multibranch pipeline.
- Connected Jenkins to a Git repository.
- Managed credentials securely in Jenkins.
- Created a basic
Jenkinsfilefor your pipeline.
With this setup, Jenkins will automatically build and test your projects, integrating smoothly with your development workflow.
Appendix: Docker Commands Reference
Pull the Jenkins Image (Optional)
docker pull jenkins/jenkins:ltsStart the Jenkins Container
docker run \ --name jenkins \ -d \ -p 8080:8080 \ -p 50000:50000 \ -v jenkins_home:/var/jenkins_home \ jenkins/jenkins:ltsCheck Running Containers
docker psView Jenkins Container Logs
docker logs jenkinsStop the Jenkins Container
docker stop jenkinsStart the Jenkins Container (After Stopping)
docker start jenkinsRemove the Jenkins Container
docker rm jenkinsNote: Removing the container does not remove the
jenkins_homevolume. Your Jenkins data will persist and can be reused.
Additional Resources
- Jenkins Documentation: https://www.jenkins.io/doc/
- Jenkins Pipeline Syntax: https://www.jenkins.io/doc/book/pipeline/syntax/
- Docker Documentation: https://docs.docker.com/
By following this guide, you now have a solid foundation for using Jenkins with Docker to automate your build and deployment processes. Explore more advanced Jenkins features and plugins to further enhance your CI/CD pipelines.