docker安装jenkins

#使用docker安装jenkins

一 、拉取jenkins镜像

镜像地址

docker镜像配置文档

#拉取镜像
docker pull jenkins/jenkins

二 、运行容器

2.1 docker安装

docker run -d 
-p 18081:8080 -p 50000:50000 
#数据文件挂载
-v /usr/local/use_data/jenkins/data:/var/jenkins_home 
#时间同步
-v /etc/localtime:/etc/localtime --name jenkins_01 
jenkins/jenkins:latest

注意事项:可能会出现的错误

  1. WARNING: IPv4 forwarding is disabled. Networking will not work.

    解决方法:

    vim /etc/sysctl.conf
    
    #配置转发
    net.ipv4.ip_forward=1
    
    #重启服务,让配置生效
    systemctl restart network
    
    #查看是否成功,如果返回为“net.ipv4.ip_forward = 1”则表示成功
    
    sysctl net.ipv4.ip_forward
    
  2. touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied
    Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

    解决方法:

    需要修改下目录权限, 因为当映射本地数据卷时,/usr/local/use_data/jenkins目录的拥有者为root用户,而容器中jenkins user的uid为1000
    执行如下命令即可:

    chown -R 1000:1000 /usr/local/use_data/jenkins
    
    

建议:在运行容器前先配合好

2.2 window安装

  1. Jenkins下载

  2. 使用管理员权限运行cmd,然后执行.msi安装包

  3. 安装完成后自动加到服务自动运行

  4. 修改端口号

    到安装目录下,找到jenkins.xml
    docker安装jenkins_第1张图片

  5. 重启服务

三 、登录界面

  1. 关闭防火墙

    # CentOS7环境
    # 临时关闭
    systemctl stop firewalld    
    # 禁止开机启动
    systemctl disable firewalld   
    
  2. 访问路径:http://192.168.xxx.xxx:18081

    docker安装jenkins_第2张图片

  3. 获取登录密码

    #进入jenkins容器
    docker exec -it 容器id /bin/bash
    
    #查看密匙
    cat /var/jenkins_home/secrets/initialAdminPassword
    
  4. 登录界面

    docker安装jenkins_第3张图片

  5. 选择安装推荐插件

  6. 新建一个管理员账户

    docker安装jenkins_第4张图片

四、配置插件

4.1 安装插件

  1. 安装maven插件

    docker安装jenkins_第5张图片

  2. 安装git插件

    docker安装jenkins_第6张图片

4.2 全局工具配置

  1. 安装jdk、git、maven

docker安装jenkins_第7张图片

docker安装jenkins_第8张图片

docker安装jenkins_第9张图片

docker安装jenkins_第10张图片

如果在安装的时候有些组件安装失败,可以后续根据页面上提示的插件,重新安装

**Manage Jenkins(**系统管理) -> Manage Plugins(插件管理) -> available(可选插件)

搜索插件然后安装,最后重启jenkins

  1. maven的settings.xml配置信息

    使用docker cp放进容器中

    #从主机复制到容器
    sudo docker cp settings.xml containerID:container_path
    

五、构建maven项目

5.1 新建任务

docker安装jenkins_第11张图片

docker安装jenkins_第12张图片

docker安装jenkins_第13张图片

docker安装jenkins_第14张图片

docker安装jenkins_第15张图片

5.2 JDK安装失败处理

因为JDK是自动安装的,而Oracle的JDK需要登录才能下载,所以会出现以下错误

docker安装jenkins_第16张图片

点击Your Oracle account doesn't appear valid. Please specify a valid username/password这个链接

docker安装jenkins_第17张图片

5.3 有可能会出现的nexus或者maven的jar包问题,docker host需要本机安装等等

这里给出pom.xml文件

<properties>
    <maven.build.timestamp.format>yyyyMMddHHmmssmaven.build.timestamp.format>

    <dockerRegistryUrl>192.168.25.142:8090dockerRegistryUrl>
    <dockerHost>http://192.168.25.142:2375dockerHost>
    <dockerProjectPrefix>taco-clouddockerProjectPrefix>
properties>


<distributionManagement>
    <repository>
        
        <id>releasesid>
        <url>http://192.168.25.1:18100/nexus/content/repositories/releases/url>
    repository>

    <snapshotRepository>
        <id>snapshotsid>
        <url>http://192.168.25.1:18100/nexus/content/repositories/snapshots/url>
    snapshotRepository>
distributionManagement>


<build>
    <resources>
        <resource>
            <directory>src/main/resourcesdirectory>
            <filtering>truefiltering>
        resource>
    resources>
    
    <finalName>appfinalName>
    <plugins>
        
        <plugin>
            <groupId>org.codehaus.mojogroupId>
            <artifactId>build-helper-maven-pluginartifactId>
            <version>3.0.0version>
            <executions>
                <execution>
                    <id>timestamp-propertyid>
                    <goals>
                        <goal>timestamp-propertygoal>
                    goals>
                execution>
            executions>
            <configuration>
                <name>current.timename>
                <pattern>yyyyMMddHHmmsspattern>
                <timeZone>GMT+8timeZone>
            configuration>
        plugin>

        <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
        plugin>
        
        <plugin>
            <groupId>com.spotifygroupId>
            <artifactId>docker-maven-pluginartifactId>
            <version>0.4.13version>
            <configuration>
                <serverId>docker-reposerverId>
                <forceTags>trueforceTags>
                <pushImage>truepushImage>
                
                

                
                <imageName>${dockerRegistryUrl}/${dockerProjectPrefix}/${project.artifactId}-${maven.build.timestamp}:${project.version}imageName>
                
                <baseImage>jdk1.8baseImage>
                <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]entryPoint>
                <resources>
                    <resource>
                        <targetPath>/targetPath>
                        <directory>${project.build.directory}directory>
                        <include>${project.build.finalName}.jarinclude>
                    resource>
                resources>
                
                <dockerHost>${dockerHost}dockerHost>
                
                <registryUrl>${dockerRegistryUrl}registryUrl>
            configuration>
        plugin>
        
        <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-resources-pluginartifactId>
            <version>3.1.0version>
            <configuration>
                <delimiters>
                    <delimit>$delimit>
                delimiters>
                
                <nonFilteredFileExtensions>
                    
                    <nonFilteredFileExtension>jksnonFilteredFileExtension>
                nonFilteredFileExtensions>
            configuration>

        plugin>

        <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
            
            <version>${spring-boot.version}version>
            
        plugin>

        <plugin>
            
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-surefire-pluginartifactId>
            <version>${maven-surefire-plugin.version}version>
            <configuration>
                <skipTests>trueskipTests>
            configuration>
        plugin>
    plugins>
build>

Maven的settings.xml

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

<localRepository>/usr/local/maven/repositorylocalRepository>
  
  <pluginGroups>
    
	<pluginGroup>com.spotifypluginGroup>
  pluginGroups>

 
  <proxies>
    
  proxies>

	 <server>
      <id>releasesid>
      <username>adminusername>
      <password>admin123password>
    server>
	<server>
      <id>snapshotsid>
      <username>adminusername>
      <password>admin123password>
    server>
	 <server>
      <id>docker-repoid>
      <username>taco-userusername>
      <password>Ha.123456password>
      <configuration>
        <email>[email protected]email>
      configuration>
    server>

    
  servers>

  
  <mirrors>
    
	
	<mirror>
      <id>alimavenid>
      <name>aliyun mavenname>
      <url>http://maven.aliyun.com/nexus/content/groups/public/url>
      <mirrorOf>centralmirrorOf>        
    mirror>
	
	
  mirrors>

 
  <profiles>
    
		<profile>     
		  <id>JDK-1.8id>       
		  <activation>       
			<activeByDefault>trueactiveByDefault>       
			<jdk>1.8jdk>       
		  activation>       
		  <properties>       
			<maven.compiler.source>1.8maven.compiler.source>       
			<maven.compiler.target>1.8maven.compiler.target>       
			<maven.compiler.compilerVersion>1.8maven.compiler.compilerVersion>       
		  properties>       
		profile> 
		<profile>   
		
			<id>devid>   
			<repositories>   
			<repository>  
			
				<id>nexusid>   
			
				<url>http://localhost:18100/nexus/content/groups/public/url>   
				
				<releases>   
				<enabled>trueenabled>   
				releases>   
			
				<snapshots>   
				<enabled>trueenabled>   
				snapshots>   
			repository>   
			repositories>  
			 <pluginRepositories>  
				
				<pluginRepository>  
					
					<id>publicid>  
					<name>Public Repositoriesname>  
					<url>http://localhost:18100/nexus/content/groups/public/url>  
				pluginRepository>  
			pluginRepositories>  
		profile>  
  profiles>
  
   <activeProfiles>
    <activeProfile>devactiveProfile>
  activeProfiles>
settings>

本地Maven仓库文件夹没有权限提示错误

#进入docker 容器
docker exec -it -u root ${containId} /bin/bash 
#赋予权限
chmod +rw ./repository

docker安装jenkins_第18张图片

5.4 查看打包结果

docker安装jenkins_第19张图片

六 、推荐安装方式

推荐直接安装在centos系统中,原因:

  1. maven、git插件可以复用
  2. maven本地仓库方便复制

你可能感兴趣的:(devops,docker)