How To Create Docker Image Using Jenkins
systemctl start jenkins systemctl staus jenkins
Jenkins is up and running.
Unlocking Jenkins
When you first access a new Jenkins instance, you are asked to unlock it using an automatically-generated password.
- Browse to
http://localhost:8080
(or whichever port you configured for Jenkins when installing it) and wait until theUnlock Jenkins page appears. - From the Jenkins console log output, copy the automatically-generated alphanumeric password (between the 2 sets of asterisks).
Note:- The command:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
will print the password at console. - If you are running Jenkins in Docker using the official
jenkins/jenkins
image you can usesudo docker exec ${CONTAINER_ID or CONTAINER_NAME} cat /var/jenkins_home/secrets/initialAdminPassword
to print the password in the console without having to exec into the container.
- The command:
- On theUnlock Jenkins page, paste this password into theAdministrator password field and clickContinue.
Step #2 : Configure the plugins
- Once you are inside the Jenkins Dashboard, selectManage Jenkins on the left.
- On the Configuration page, selectManage Plugins.
- Manage Plugins page will give you a tabbed interface. ClickAvailable to view all the Jenkins plugins that can be installed.
- Using the search box, search forGithub plugin. There are multiple Docker plugins, select Github plugin using the checkbox.
- While on this page, install theGit plugin for obtaining the source code from a Git repository.
- ClickInstall without Restart at the bottom.
The plugins will now be downloaded and installed. Once complete, click the linkGo back to the top page.
Step #3 : Add new Node for docker deployment
Step 1: On Jenkins master go to Manage Jenkins > Manage Nodes.
Step 2: Select New Node and enter node name.
Step 3: select Permanent Agent -> Press OK.
Step 4: On the next page, fill in the following fields:
Set a number of executors (one or more) as needed. Default is 1.
Set a Remote FS Root, a home directory for the master on the agent machine, e.g., "/root"
Set Usage to "Use this node as much as possible".
Set Availability, –> Keep this agent online as much as possible.
Set Labels , it's easier to use name of the node in this case
Set Launch method – Launch agents via SSH
- enter host ip or domain name
- add new credentials for this host
- set Host Key Verification Strategy to Non verifying verification strategy
Press Save.
Step #4 : Create and configure new CI/CD job
- On the Jenkins dashboard, selectCreate new jobsof typeFreestyle project& create new job ex. test.
- The build will depend on having access to Docker. Using the "Restrict where this project can be run" we can define the label we set of our configured Docker agent. The set "Label Expression" toslave01. You should have a configuration of "Label is serviced by no nodes and 1 cloud".
- Select the Repository type asGit and set the Repository
add Github credentials and set the branch to build
4. We can now add a new Build Step using the dropdown. SelectExecute Shell.
5. Dockerfile takes care of build, Jenkins only needs to call build and specify a friendly name.
Build step :
imageName=test:${BUILD_NUMBER} containerName=test docker system prune -af docker build -t $imageName . docker stop $containerName || true && docker rm -f $containerName || true docker run -p 3000:3000 -d --name $containerName $imageName
Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build WORKDIR /app ADD . /app/ EXPOSE 3000 RUN dotnet build WORKDIR /app/Core.Application/ ENTRYPOINT ["dotnet", "run", "Core.Application.dll"]
6. Click save and press Build now.
To monitor all the process, you can enter a build process and press Console Output
Everything went fine and now we have a deployed docker container on our SLAVE01 node, just go and check it
How To Create Docker Image Using Jenkins
Source: https://ep.gnt.md/index.php/how-to-build-and-deploy-docker-images-using-jenkins/
Posted by: lafranceshearompal.blogspot.com
0 Response to "How To Create Docker Image Using Jenkins"
Post a Comment