How to Setup Jenkins Docker Container on Server

How to Setup Jenkins Docker Container on Server

ยท

3 min read

Introduction to Build Automation

Build Automation is a process of Automating the workflow that test your newly committed code, Build an Artifact ( Jar file or Docker image) and push that into a Private Repository or even Deploy it into Development server. buildAutomation.png

What is Jenkins ?

There are tools that do Build Automation and Jenkins is one of them. Jenkins is software which you can install it on your server. It also has a UI (User Interface) where you can config it.

What Jenkins can do ?

  1. It can Run Tests
  2. It can Build Artifacts
  3. It can Publish those Artifacts in a Private Repository
  4. It can Deploy Artifacts in Development Server

Jenkins can also send notifications to the team that all these steps get executed and whether it was successful or not. So, you might now have an idea that Jenkins is really a powerful tool to help you manage all these build automation process. In other words Jenkins is like a middle man between these technologies like Docker, Build tool, Repositories and Deployment Servers. Jenkins have different plugins in order to integrate it with all these technologies.

for example if you have a code hosted in Github or Gitlab repository and you want to build a Docker image for it then push it to private repository and deploy it on any deployment server. You can do all these by configuring plugins for these tools in Jenkins and all tasks will be automated. Isn't it cool, now you don't need to manually do all these steps which are really time consuming.

How does it work ?

  1. Run Tests : Build tools should be available & you need test database which you need to configure.
  2. Build Artifacts : Docker or Build tools should be available
  3. Publish Docker image : You need Docker credentials to store it in Jenkins to authenticate a Docker repository.

Jenkins should have access to all these technologies or tools in order to do it's job. Once, all the setup is done you can use it for multiple projects.

Install Jenkins

There are 2 ways to install Jenkins on a server :

  1. Install Jenkins directly on a OS . For that you have to Download Package and Install on server, then you have to create a separate Jenkins User on server which is again time consuming and take some efforts.
  2. Run Jenkins as Docker container . Just simply run Jenkins container no need to worry about user and other things.( much easier , less efforts and works the same)

Let's Install Jenkins

  1. Install Docker in your server
    apt install docker.io
    
    OR
    snap install docker
    
  2. Run Jenkins container
    docker run -p 8080:8080 -p 50000:50000 -d -v jenkins_vol:/var/jenkins_home jenkins/jenkins:lts
    
  3. -p 8080:8080 : mapping the Jenkins container port 8080 with server port 8080
  4. -p 50000:50000 : mapping the Jenkins container port 50000 with server port 50000 and this is the port where Jenkins master and worker node communicate. So, that mean Jenkins can be use as custer
  5. -v jenkins_vol:/var/jenkins_home : creating a Named Volume and mapping it with /var/jenkins_home folder inside the Jenkins Container for persistence data storage
  6. jenkins/jenkins:lts : name of Jenkins image in Docker Registry

  7. Now, Grab your server's IP address and check on port 8080 in your web browser <IP Address>:8080 It will open Jenkins UI in you web browser jenkins.png Now, grab your credential located at /var/jenkins_home/secrets/initialAdminPassword Run container in interactive mode

    docker exec -it 784ee6448e2b bash
    
    cat /var/jenkins_home/secrets/initialAdminPassword
    
  8. Setup a First Admin User for you Jenkins jenkins.png jenkins.png jenkins.png Congratulations, You Have Successfully Setup Jenkins on Your Server. Now, you can create Jobs to Automate the Task
ย