Professional Documents
Culture Documents
Class : TE-IT
Roll No : A-4
ID : TU4F1819012
DevOps
Assignment No : 2
Ans :
Jenkins is one of the most popular tools for continuous integration and continuous delivery on
any platform. As a Java application, Jenkins has many plugins for automating almost everything
at the infrastructure level. The use of Jenkins has widely increased rapidly due to a rich set of
functionalities, which it provides in the form of plugins.
1. First, you need to install OpenJDK. Jenkins currently only supports JDK8. Once Java is
running, you can install Jenkins.
Click here https://www.jenkins.io/download/ to download the latest Jenkins package for
Windows (currently it is version 2.).
2. Once it is downloaded, it will open a wizard. Click “Next” to start the installation.
3. Click the “Change…” button if you want to install Jenkins in another folder. In this example, I
will keep the default option and click on the “Next” button.
Enter the service logon credentials. It is recommended to select the second option “Run service
as local or domain user” as it is safer. To run Jenkins service using this option, you have to
enter the domain username and password. Click on the “Test Credentials” button to test your
domain credentials and it will enable the “Next” button. Click on the “Next” button.
5. Enter the port on which Jenkins will be running. Click on the “Test Port” button to validate if it
is free.
6.Select the Java home directory and click on the “Next” button.
7. Select other services that need to be installed with Jenkins and click on the “Next” button.
8. Click the “Install” button to start the installation process.
9. The installation will proceed.
11. You will automatically be redirected to a local Jenkins page, or you can paste the URL
http://localhost:8080 in a browser.
12. To unlock Jenkins, copy the password from the file intialAdminPassword. This file should be
found under the Jenkins installation path (set at step 3 in Jenkins installation). If a custom path
was entered, then you should check that location.
Copy the content of the initialAdminPassword file and paste it in the Administrator password
field. Then, click the Continue button.
13. You can install either the suggested plugins or selected plugins you choose. To keep it
simple, we will install the suggested plugins.
14. Wait until the plugins are completely installed.
15. The next thing that you should do is create an Admin user for Jenkins. Then, enter your
details and click Save and Continue.
16. Click on Save and Finish to complete the Jenkins installation.
17. Now, click on Start using Jenkins to start Jenkins.
18. Finally, here is the default Jenkins page.
Q2) Dockerize java project given.
Ans :
Docker builds images by reading instructions from a Dockerfile. A Dockerfile is a simple text file
that contains instructions that can be executed on the command line. It is a fundamental
building block used when dockerizing your Java applications, and it is how you can create a
Docker image that can be used to create the containers you need for automatic builds.
Ensure that docker is installed by running and checking with the following command in the
terminal:
$ docker –version
Clone a sample Java application or in your Java Application, add an empty file in the directory
called Dockerfile.
A Dockerfile always starts with another image called a “base-image”. This is the building block
for our image, and examples include things like a red hat image or an Ubuntu image.
If we start with an image that already has Java installed, we won’t have to install Java later in
the Dockerfile. If we don’t start with an image that is already installed, then we’ll have to install
Java. At the end, this Dockerfile will be used to build another image. This final image (with all
dependencies and the application added) can be delivered to those who want to run the
application.
Building the Dockerfile
We have to choose an image to start with. To make our base image Ubuntu, add this line:
FROM ubuntu : latest
“latest” will pull the latest published ubuntu image in DockerHub which is a public registry of
images available for use.
Install a dependency, the jre (which is required on any machine to run a java application).
Dockerfiles have a RUN command that we can utilize as if we were installing a jre on an ubuntu
machine from the command line.
RUN \
# Update
apt-get update -y && \
# Install Java
apt-get install default-jre -y
This command
adds the local ./target/gs-serving-web-content-0.1.0.jar artifact as spring-mvc-example.jar .
When our application is running, it is running on port 8080. Use the docker EXPOSE command
to open up that port.
EXPOSE 8080
Utilize the
CMD docker command to run the jar.
Altogether, the
Dockerfile should look like this:
RUN \
# Update
Build and Run the Image Locally
To build the image with the name spring-mvc-sample-image :
docker build . -t spring-mvc-sample-image
We can verify the image built correctly with docker images
$ docker images
REPOSITORY TAG IMAGE ID CREATED
SIZE
spring-mvc-sample-image latest 8fa27ad00edd 34 minutes ago
540MB
Bring up a
container called ‘sample-mvc-sample-container’ based on the image ‘spring-mvc-sample-
image’.
docker run -t -p 8080:8080 --name sample-mvc-sample-container spring-mvc-sample-image
Use ctrl + c to exit the container. Now command docker ps -a
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
736f9cdc1499 spring-mvc-sample-image "/bin/sh -c 'java -j…" 2 hours ago
Up 2 hours 8080/tcp sample-mvc-sample-container
Finally, tag
and push the Image to the public Dockerhub
First, list the image and find the one you built
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
spring-mvc-sample-image latest 8fa27ad00edd 34 minutes ago 540MB
Tag the image by grabbing the image id(8fa27ad00edd) and tagging it with the docker
username a name of your choice.
A docker tag consists of a name and a version , separated by a :
docker tag 8fa27ad00edd username/spring-mvc-sample-image:0.1
Push
docker push username/spring-mvc-sample-image:0.1
You will see a Jenkins job configuration page. Scroll down to find Pipeline section. There are
two ways to execute a Jenkins pipeline. One way is by directly writing a pipeline script on
Jenkins, and the other way is by retrieving the Jenkins file from SCM (source control
management).
To start the process to build the pipeline, click Build Now. If everything works, you will see
your first pipeline (like the one below).
To see the output from the pipeline script build, click any of the Stages and click Log. You
will see a message like this.
Step 4: Configure and execute a pipeline job with SCM
Now, switch gears: In this step, you will Deploy the same Jenkins job by copying the Jenkinsfile
from a source-controlled GitHub. In the same GitHub repository, pick up the repository URL by
clicking Clone or download and copying its URL.
Click Configure to modify the existing job. Scroll to the Advanced Project Options setting, but
this time, select the Pipeline script from SCM option in the Destination dropdown. Paste the
GitHub repo's URL in the Repository URL, and type Jenkinsfile in the Script Path. Save by
clicking the Save button.
To build the pipeline, once you are back to the Task Overview page, click Build Now to execute
the job again. The result will be the same as before, except you have one additional stage
called Declaration: Checkout SCM.
Advantages:
⮚ There’s less clutter on the development machine
⮚ One can work on multiple projects side by side, which depend on slightly different
database versions
⮚ One can create a development environment on any OS in a reliable fashion
⮚ Everything is “documented” through automation and reproducible
Docker is great for running databases in a development environment. One can even use it for
databases of small, non-critical projects which run on a single server. Regular backups should
be performed.
To check if Docker has installed correctly, open terminal and enter docker ps. If no running
containers, following output would be shown:
To check if Node is installed, enter node -v. Should get npm automatically with Node
installation, but to check, try npm -v.
If all went well, a long number is seen, which is the container’s ID.
Enter docker ps, information about the image is seen that was used to create the container
(mysql:latest in this example), when the container was created and how long it’s been running,
ports available (should be 3306/tcp) and the name given to it(test-mysql).
Note the port number and enter the command below, where $PORTNUMBER is the port
assigned by Docker (32783 in this case):
mysql -P $PORTNUMBER --protocol=tcp -u root -p
Enter the password used when creating the container (whatever entered in place of my-secret-
pw).
Now connected to MySQL running in the container. This is helpful if we have a local datafile
NOT in SQL format that you want to load into a table.
Run docker ps again to find the port to use to connect, and use the local MySQL client to check
on the new database.
Then run docker-compose up -d in the same directory as the docker-compose.yml file. In this
example, we’ve assigned the port (8083). Docker will auto-magically generate a name for the
container.
Wait for npm to finish installing, then cd into the directory name.
Now create a connection to MySQL with lb datasource:
And enter the connection information.
Add properties.
Check to make sure the MySQL container is up and running with docker ps
To run the API, just run the Node app that LoopBack created with node .