Use Jenkins from scratch to build a Docker container (Ubuntu 14.04)

Source: Internet
Author: User
Tags git client docker run

When the developer updated the code, submitted it to Gitlab, and then triggered Jenkins by the tester, a new version of the application was built. Sounds like a simple, duang~duang~duang, I used to be so, you use the same way, it seems that the process is very automated, in fact, there are a lot of stunts, there is no such hair, no, there is no such a simple way, the environment is very cumbersome to build the process, The contextual configuration of Jenkins is rather difficult. Today I'm going to introduce a simple new approach-using Jenkins to build an Apache container.

Target: Jenkins is placed in the host (Ubuntu14.04), the Apache container is placed in the container, after the Apache image is modified, triggering the creation of a Jenkins job, the host opportunity to stop the original container, to create a new container.

Enter the formal link below.

1. Install Jenkins

#wget -q-o-http:///Pkg.jenkins-ci.org/debian/jenkins-ci.org.key | #sudo apt-key add  - #sudosh'echo deb Http://pkg.jenkins-ci.org/debian binary/>/etc/apt/ Sources.list.d/jenkins.list'  #sudo apt-get update  #  sudoinstall Jenkins  

2. Login to http://IP:8080, enter Jenkins, in Http://IP: 8080/pluginmanager/available Install some necessary plugins for SCM Sync Configuration Plugin, GitHub Plugin, git Plugin, git client Plugin, restart Jenkins at the end of installation.

3. Re-login to http://IP:8080, enter Jenkins and create a new item named Apache.

    • Configure as below

The shell script is as follows, and the script will be from GitHub (if there is a local gitlab operation in the same vein)

#!/bin/SHIDSet+eEcho '>>> Get old container ID'CID=$ (DockerPS|grep "Apache"|awk '{print $}')Echo$CID/usr/local/bin/docker build-t Apache/var/lib/jenkins/jobs/apache/workspace |Tee/var/lib/jenkins/jobs/apache/workspace/Docker_build_result.logresult=$(Cat/var/lib/jenkins/jobs/apache/workspace/docker_build_result.log |Tail-N1)#if[["$RESULT"! = *successfully*]];then# Exit-1#fiEcho '>>> stopping old container'if["$CID"!=""]; Then  /usr/local/bin/Docker Stop $CIDfiEcho '>>> restarting Docker'Service Docker.io RestartSleep 5  Echo '>>> starting new container'/usr/local/bin/docker run-p the: the-D Apache

4. After the configuration is complete, save

5. Configure Jenkins Users

It cannot be built at this time because the Jenkins trigger script is not the root user, so Jenkins is required to be exempt from the user and the user is added to the Docker group, otherwise the container ID is not obtained.

[Email protected]:~# vim/etc/sudoers# User privilege specificationroot    all =(all:all) alljenkins all< /c2>=(All:all) all[email protected]:~$ usermod-g Docker Jenkins

6. Start building, you can see the output in the Jenkins build record, since Jenkins will automatically download the files on GitHub and place them in the workspace directory, so when the script is triggered, the Apache image is built directly.

7. Now look at the images and containers, login Apache website verification

The container-generated Apache page opens as follows

Use Jenkins from scratch to build a Docker container (Ubuntu 14.04)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.