Setting up Jenkins in a Container on Alibaba Cloud

Image for post
Image for post

By Bineli Manga, Alibaba Cloud Community Blog author.

During the time of DevOps and Microservices, every application is abstracted and served as services through containers. There are many container providers available today for use, including Docker, containerd, coreos, among other options. In this tutorial, we are going to look into how Docker engines can be used to leverage Jenkins as a service through containerization. That is, we are going to show you how to install and set up docker, how to install and set up Jenkins and also how to launch Jenkins through a Docker image. Because Docker is a cross-platform capable technology, you can use it on Windows, Mac, or Linux, but for the sake of simplicity, we will show you how to install in on a Linux Virtual Machine.

Jenkins is the most used open-source DevOps tool available nowadays, and Docker is the first open-source containerization platform. So, we think that running Jenkins on Docker will enable us to have better performance while also opening us up to the support of the open-source community for both products.

This tutorial will be organized as follows:

  • Installing docker on the virtual machine
  • Getting the Jenkins docker image from Docker hub
  • Running Jenkins in a Docker Container

Requirements

  • A reliable Internet connection.
  • A computer where you have administrator rights.

Installing Docker on the VM

1.Update the packages local repository

$ sudo apt-get update

2.Install the packages: apt-transport-https to enable the downloading of HTTPS encrypted packages, ca-certificates to enable the downloading of security certificates when you will try to download and install Docker images, curl to be able to query remote http endpoints, and gnupg-agent and software-properties-agent, which are packages required to install Docker:

$ sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent Software-properties-common

3.Add Docker’s offcial PGP key. This is to make sure that communications with docker registry remain secured through encryption. You can do so with the following command:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

4.Set up the Docker’s stable repository:

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) Stable"

5.Update apt packages index:

$ sudo apt-get update

6.Install the latest version of Docker engine and docker-compose:

$ sudo apt-get install docker docker-compose

7.Verify that you have correctly installed Docker by running the following command:

$ sudo docker run hello-world

This command will download a test image for docker and run that image. If the operation is success full, you should receive an image similar to the one in the image below:

Image for post
Image for post

Getting Jenkins’s Docker image from the Docker repository

1.Run the docker pull command to get the Jenkins’ image:

$ docker pull jenkins

After running this command, you should get similar text in your shell:

Image for post
Image for post

2.To check that the pull command has completed successfully, you can run the docker images command on your terminal and verify that jenkins images is already listed:

$ docker images

After running this command, you should get a print in your terminal similar to this one, where you can clearly identify the Jenkins image locally installed :

Image for post
Image for post

Running Jenkins in a Docker container

1.Check if our jenkins docker’s image is not running yet, by typing the docker ps command:

$ docker ps

After running this command in your terminal, you should have an output similar to this one:

Image for post
Image for post

In the above image, you can see the MySQL and wordpress docker images that are running, but no image of Jenkins.

We already have the docker image of Jenkins. But to run this image in a container, we have to build and run the Jenkins’ docker’s image.

2.To run the Jenkins’ docker image, you have to run the following command:

$ docker run -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts

The above command will run Jenkins in a container and make it available on port 8080 of localhost.

Note: The above command does not provide persistent storage for Jenkins, which means that once the container is stopped or deleted, all the data will be deleted. During production, though, we don’t want something like that to happen. In order to avoid such behavior, we will attach a storage device to our docker instances.

Docker provides volumes, which enable us to add storage for a docker container

3.Run the following command to run Jenkins without loosing the data when the container is stopped or deleted:

$ docker run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts

When this command is executed, it will also create a jenkins folder in the following path:

$ /var/jenkins_home jenkins/

4.While the docker container was created for jenkins, jenkins also automatically generated a password which can be found at the terminal as shown below or in a file found in the file:

$ /var/jenkins_home/secrets/initialAdminPassword

Here after is a screen capture of a sample of the content of initialAdminPassword file:

Image for post
Image for post

Note: The password will be different from the one showed there. You’ll need to know your password. You can copy your passport for your terminal to use it later.

5.Access Jenkins via your browser. To do this, Open your browser to the address localhost:8080, and you should see a web page similar to the one below:

Image for post
Image for post

6.You have to enter the administrator password you copied from the terminal, or get it from the file /var/jenkins_home/secrets/initialAdminPassword, then you will be taken to the install page.

Image for post
Image for post

7.Then, click Install suggested plugins to install plugins directly or Select plugins to install if you wish to install some particular Jenkins plugins before proceeding. Next, select the desired plugins and click install.

Image for post
Image for post

8.The installation process can take some time then you are redirected to the Create First Admin User page.

Image for post
Image for post

9.You chose to create another admin user or just continue with the admin user created by Jenkins. Here we would just continue with the default admin user. Click Continue as admin button. After that, you will be taken to the configuration page as show below.

Image for post
Image for post

You do not have to change these default settings, but you can simply click the Save and Finish button to save these settings.

10.After that, you will be directed to the last setup page, which simply tells you the credentials to use for login to Jenkins. Here is an example of such page, with admin as the default username and the administrative password you copied from the file initialPassword as the admin password:

Image for post
Image for post

11.Next, just click on Start Using Jenkins and you will be taken to the Jenkins Dashboard. There you will be able to create Jenkins jobs to run. Here is a screen capture of the Jenkins’ Dashboard:

Image for post
Image for post

Conclusion

Original Source

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store