Jenkins docker container

Jenkins is one of the most popular software for CICD. One of the favorite among the DevOps and System Administrator community. We personally use the Jenkins a lot in our day to day life for Automation, Build and Deployment etc.

Overview

In this article, we will run and configure Jenkins Docker container. Also, we will do some operational activity like stopping, starting the Jenkins docker container and removing Jenkins docker image. Technically, we will run the Jenkins containers in Docker Servers.

Prerequisites

Docker server should be installed and running. In Ubuntu, we have already article on how to Install Docker On Ubuntu.

    Once you have Docker server installed in your machines. Follow the given below steps.

    Steps To Run Jenkins Container In Docker Server

    Here we have two easiest method to setup the Jenkins container. You can use any one method as per your convenience.

    Run Docker Jenkins container

    Open the terminal and run the following command. Please read this section completely then do the copy paste of the command.

    docker run -p 8080:8080 -p 50000:50000 --restart=on-failure -v jenkins_home:/var/jenkins_home  --env JENKINS_OPTS="--prefix=/jenkins" jenkins/jenkins:lts-jdk17

    Let’s understand the above command.

    (1) docker run – command will help to create the container from the Docker image. If docker image is not available in Local system then it will pull the docker image from Dockerhub registery.
    (2)-p 8080:8080 -p 50000:50000 – Here local port numbers is mapped to container port number. 8080:8080 is for Jenkins Port mapping. And 50000:50000 is for Agent Listener Port.
    (3)--restart=on-failure – Restart the container if it exits due to an error, which manifests as a non-zero exit code.
    (4)-v jenkins_home:/var/jenkins_home – It creates a new Docker Volume called jenkins_home and mount inside the container at /var/jenkins_home . So this Docker Volume will keep all the Files and Directories of Jenkins Server. If accidentally the Jenkins Container get terminated, you can re-create a new container and re-use the same Docker Volume. By this way you will not loose the Data of Jenkins setup.
    (5)--env JENKINS_OPTS="--prefix=/jenkins" – Here, setting the prefix which can be useful while setting the Proxy Pass with Apache Or Nginx configuration. You will read about this in our next post.
    (6)jenkins/jenkins:lts-jdk17 – Here, read the given below explanation.

    • First jenkins refer to Dockerhub Repo Name
    • Second /jenkins refer to Docker Image Name
    • And lts-jdk17 refer to tag name set on that Docker Image.

    Now move to next step where we will follow the rest of the procedure to install the Jenkins docker container.

    Jenkins docker installation step from WebUI / Web browser

    Open the web browser and type the Jenkins server IP in URL field followed with port number 8080 and /jenkins.

    Then URL field in web browser will look like this. In given below code snippet, replace the 192.168.123.92 with Jenkins Server’s ip address.

    http://192.168.123.92:8080/jenkins/

    1. Unlock Jenkins: Use the initial password

    On first web page, it will ask for secret password. You can get this password from the terminal where you have done the docker run command.

    Copy the password from the terminal and paste into the Unlock Jenkins page at ‘Administrator password’ input field. Click ‘Continue’ button.

    2. Install Jenkins plugins

    Click on ‘Install suggested plugins’, it will install all the plugins which are suggested by the Jenkins. If you want to go for custom selection of plugins then click on ‘Select plugins to install’.

    In our scenario, we will select ‘Install suggested plugins’.

    In next section, you can see the list of plugins and installation status.

    3. Create first Jenkins admin user

    After this you will get the form to fill, here we will create the first Jenkins admin user account. In our scenario, we have given the user name ‘admin’ . You can choose any other name as per your wish.

    After providing the details, click on ‘Save and Continue’ button.

    4. Set Instance configuration

    Now you will get the ‘Instance Configuration’ page. This is for defining Jenkins URL. We are using IP address of Docker Server where Jenkins docker container is running. You can also use the DNS address or FQDN of Docker server if it is already set.

    In our scenario, we have kept the URL as it is. Click on ‘Save and Finish’ button.

    5. Jenkins is ready

    Now you are reached to final stage of installation page. You will be greeted with page ‘Jenkins is ready!’

    Click on ‘Start using Jenkins’ button, this will land you to main Jenkins Dashboard.

    Once you find the Jenkins Dashboard on first login, you can start using the Jenkins. Here, we will recommend to take a pause and lets finish the other action items. If you are first time Docker and Jenkins user then some useful tips you should follow.

    Jenkins container post installation steps

    You might find in the terminal where we are left with docker run command, the terminal is still showing the progress. First stop this by pressing CTRL+C from the keyboard. It will stop the Jenkins docker container.

    Because we are in Docker server, we will use some docker commands to administer the Jenkins docker container.

    Get the Jenkins docker container id. It will show the stopped docker containers. Find the Jenkins container id showing on terminal.

    # It will show stopped docker containers.
    docker ps -a

    Start the Jenkins container. For reference, find the follow up screenshot after given below command.

    # Start the Jenkins container. Replace with the Jenkins container id. Refer the screenshot.
    docker start jenkins-container-id
    
    # Validate the running docker container
    docker ps

    Now open the web browser and type your Jenkins URL again. Now login with your Admin user account which you have set during the installation.

    Welcome back to Jenkins dashboard. Now, you can start using the Jenkins.

    Frequently Asked Questions

    How to stop and start Jenkins container ?

    As we are using the Docker server where we have run the Jenkins container hence we will use the docker command to manage the Jenkins container.

    To stop Jenkins container use the given below command (Replace <jenkins-container-id> with actual Jenkins container id.
    docker stop <jenkins-container-id>

    To start Jenkins container use the given below command (Replace <jenkins-container-id> with actual Jenkins container id.
    docker start <jenkins-container-id>

    How to find Docker volume for Jenkins container?

    Use the given below docker command. In this document, we have used Docker volume and mounted with /var/jenkins_home directory.
    docker volume ls

    How to remove Jenkins container ?

    Like other docker containers, Jenkins container also has the same procedure of removing.

    1. Stop the Jenkins container by using its jenkins container id.
    2. Remove the Jenkins container by using its jenkins container id.
    3. (Optional) Remove Jenkins docker image from the system.

    Stop the Jenkins container

    Note: Replace <jenkins-container-id> with its actual Jenkins container id. Refer the screenshot for more understanding.

    First list the running docker containers. Find the Jenkins container id.

    docker ps

    Stop the Jenkins container.

    docker stop <jenkins-container-id>

    Remove the Jenkins container

    Follow the given below command to remove the Jenkins container.

    docker rm <jenkins-container-id>

    How to remove Jenkins docker image ?

    Note: To remove the Jenkins docker image. The Jenkins container should be removed first.

    List the docker images

    Run the given below command to find the Jenkins docker image id. Keep its docker image id handy for next step.

    docker images

    Remove Jenkins docker image

    Run given below command to remove the Jenkins docker image. Use image id here.

    docker rmi <docker-image-id>

    Further validate if the docker image is removed. The Jenkins docker image should not be in output list.

    docker images

    Read Some More Articles

    Leave a Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.