• Jenkins的Docker容器添加dotnet-sdk和git


    • 前言

      此文章仅仅是自己想弄一套自己构建程序

    •      第一步

         当前是自己写Dockerfile了。docker的仓库里面虽然有Jenkins的镜像。但是那镜像里面没有我们.netcore的构建环境。所以了,我需要做一个自己的镜像

         在https://hub.docker.com中找Jenkins的基础镜像。好像没几个版本直接用最新的版本。

        然后开始编写Dockerfile

        

    #此镜像从jenkin中构建,添加dotnetcore build环境和git
    #VERSION 1
    #Author: txb
    #
    
    #基础镜像
    FROM jenkins
    
    
    #使用root管理员
    USER root
    #清除了基础镜像设置的源,切换成腾讯云的jessie源
    #使用非腾讯云环境的需要将 tencentyun 改为 aliyun
    RUN echo '' > /etc/apt/sources.list.d/jessie-backports.list 
      && echo "deb http://mirrors.aliyun.com/debian jessie main contrib non-free" > /etc/apt/sources.list 
      && echo "deb http://mirrors.aliyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list 
      && echo "deb http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list
    
    #https://dotnet.microsoft.com/download/linux-package-manager/debian9/sdk-current
    #官方操作
    RUN wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.asc.gpg 
    && mv microsoft.asc.gpg /etc/apt/trusted.gpg.d/ 
    && wget -q https://packages.microsoft.com/config/debian/9/prod.list 
    && mv prod.list /etc/apt/sources.list.d/microsoft-prod.list 
    && chown root:root /etc/apt/trusted.gpg.d/microsoft.asc.gpg 
    && chown root:root /etc/apt/sources.list.d/microsoft-prod.list
    
    #更新源,安装dotnet-sdk RUN apt-get update && apt-get install -y dotnet-sdk-2.2

      没写完,先保存一下build一下看看会不会报错

    #后面有个点.
    docker build -t txb-jenkins .

      执行完了,还真报了一个错

    E: The method driver /usr/lib/apt/methods/https could not be found.
    E: Failed to fetch https://packages.microsoft.com/debian/9/prod/dists/stretch/InRelease
    E: Some index files failed to download. They have been ignored, or old ones used instead.
    The command '/bin/sh -c apt-get update && apt-get install -y dotnet-sdk-2.2' returned a non-zero code: 100
    

      看着像是应该缺少什么东西来访问https的地址,百度了一下,还是真是缺少 apt-transport-https,没有,那么我先装上呗,修改Dockerfile 

      在安装dotnet-sdk前面增加一句再重新build(最好不要这么去干,我试过了,总共下来的镜像有2G多。。等待时间有点久,有耐性就试试)

    RUN apt-get install -y apt-transport-https #这个也是会报错的
    RUN apt-get update && apt-get install -y apt-transport-https

      然后就是有点漫长的等待。不着急

       接下来,我们添加git进去,Debian嘛,安装套一样

      完整的Dockerfile文件

    #此镜像从jenkin中构建,添加dotnet-sdk环境和git
    #VERSION 1
    #Author: txb
    #
    
    #基础镜像
    FROM jenkins
    #使用root管理员
    USER root
    #清除了基础镜像设置的源,切换成腾讯云的jessie源
    #使用非腾讯云环境的需要将 tencentyun 改为 aliyun
    RUN echo '' > /etc/apt/sources.list.d/jessie-backports.list 
      && echo "deb http://mirrors.aliyun.com/debian jessie main contrib non-free" > /etc/apt/sources.list 
      && echo "deb http://mirrors.aliyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list 
      && echo "deb http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list
    
    RUN apt-get update && apt-get -y install apt-transport-https
    
    #安装dotnet-sdk环境参照官方操作文档
    #https://dotnet.microsoft.com/download/linux-package-manager/debian9/sdk-current
    RUN wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.asc.gpg 
    && mv microsoft.asc.gpg /etc/apt/trusted.gpg.d/ 
    && wget -q https://packages.microsoft.com/config/debian/9/prod.list 
    && mv prod.list /etc/apt/sources.list.d/microsoft-prod.list 
    && chown root:root /etc/apt/trusted.gpg.d/microsoft.asc.gpg 
    && chown root:root /etc/apt/sources.list.d/microsoft-prod.list
    
    #安装dotnet-sdk和git
    RUN apt-get update && apt-get install -y dotnet-sdk-2.2 git
    
    #入口操作
    ENTRYPOINT ["/bin/tini", "--", "/usr/local/bin/jenkins.sh"]

    开始构建容器

    docker run -d -p 8000:8080 -v /home/jenkins_home:/var/jenkins_home --name csit-dotnet-jenkins dotnet-jenkins

      docker run -d -p 4040:8080 -v /home/jenkins/jenkins_home:/var/jenkins_home -v /home/jenkins/jenkins_publish:/home/jenkins_publish  --name csit-dotnet-jenkins --privileged=true dotnet-jenkins

      docker run -d -p 4040:8080 -v /home/app/:/home/jenkins_publish  --name csit-dotnet-jenkins --privileged=true dotnet-jenkins

    说明:-d:后台运行

              -p:端口映射8000:8080    宿主主机端口8000,容器端口8080

              -v:映射目录  主机目录/home/jenkins_home  ,容器目录/var/jenkins_home

              --name:创建出容器的名字

              --privileged=true:应该跟权限有关,如果不带此参数的话,在启动容器的时候容器访问/home/jenkins_home会抛出一个permission deny问题

              dotnet-jenkins:镜像名称

    补充一个问题:

       后续在使用容器运行web的时候,Jenkins里面也需要使用宿主主机的docker命令,我不可能再Jenkins容器里面再装一个docker把后面百度了一下好像在容器内部使用docker命令也是可行

    贴出一个文章连接https://www.cnblogs.com/leolztang/p/6934694.html

    按照作者的思路,我又从新编辑了一下我的Dockerfile

    #此镜像从jenkin中构建,添加dotnetcore build环境和git
    #VERSION 1
    #Author: txb
    #
    
    #基础镜像
    FROM jenkins
    #使用root管理员
    USER root
    
    
    #清除了基础镜像设置的源,切换成腾讯云的jessie源
    #使用非腾讯云环境的需要将 tencentyun 改为 aliyun
    RUN echo '' > /etc/apt/sources.list.d/jessie-backports.list 
      && echo "deb http://mirrors.aliyun.com/debian jessie main contrib non-free" > /etc/apt/sources.list 
      && echo "deb http://mirrors.aliyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list 
      && echo "deb http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list
    
    RUN apt-get update && apt-get -y install apt-transport-https
    
    #安装dotnet-sdk环境参照官方操作文档
    #https://dotnet.microsoft.com/download/linux-package-manager/debian9/sdk-current
    RUN wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.asc.gpg 
    && mv microsoft.asc.gpg /etc/apt/trusted.gpg.d/ 
    && wget -q https://packages.microsoft.com/config/debian/9/prod.list 
    && mv prod.list /etc/apt/sources.list.d/microsoft-prod.list 
    && chown root:root /etc/apt/trusted.gpg.d/microsoft.asc.gpg 
    && chown root:root /etc/apt/sources.list.d/microsoft-prod.list
    
    #安装dotnet-sdk和git
    RUN apt-get update && apt-get install -y dotnet-sdk-2.2 git libltdl7
    
    
    #定义一个参数dockerGid
    ARG dockerGid=999
    
    #将docker内部的jenkins用户添加到docker用户组里面
    RUN echo "docker:x:${dockerGid}:jenkins" >> /etc/group
    
    USER jenkins
    
    #入口操作
    ENTRYPOINT ["/bin/tini", "--", "/usr/local/bin/jenkins.sh"]

    (后面更新dotnet3.1的Dockerfile)

    在创建容器的时候指定挂载一下docker目录

    创建容器的命令如下:

    docker run --name csit-dotnet-jenkins --privileged=true -d -p 4040:8080 -v /home/app:/home/jenkins_publish -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker dotnet-jenkins

    2020年6月12日 16:19:10更新安装dotnet3.1的Dockerfile

    #此镜像从jenkin中构建,添加dotnetcore build环境和git
    #VERSION 1
    #Author: txb
    #
    
    #基础镜像
    FROM jenkins/jenkins:lts
    #使用root管理员
    USER root
    
    #清除了基础镜像设置的源,切换成腾讯云的jessie源
    #使用非腾讯云环境的需要将 tencentyun 改为 aliyun
    RUN echo '' > /etc/apt/sources.list.d/jessie-backports.list 
      && echo "deb http://mirrors.aliyun.com/debian jessie main contrib non-free" > /etc/apt/sources.list 
      && echo "deb http://mirrors.aliyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list 
      && echo "deb http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list
    
    RUN apt-get update && apt-get -y install apt-transport-https
    
    #安装dotnet-sdk环境参照官方操作文档
    #https://dotnet.microsoft.com/download/linux-package-manager/debian9/sdk-current
    #RUN wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.asc.gpg 
    #&& mv microsoft.asc.gpg /etc/apt/trusted.gpg.d/ 
    #&& wget -q https://packages.microsoft.com/config/debian/9/prod.list 
    #&& mv prod.list /etc/apt/sources.list.d/microsoft-prod.list 
    #&& chown root:root /etc/apt/trusted.gpg.d/microsoft.asc.gpg 
    #&& chown root:root /etc/apt/sources.list.d/microsoft-prod.list
    #安装dotnet-sdk和git
    #RUN apt-get update && apt-get install -y dotnet-sdk-2.2 git libltdl7
    #定义一个参数dockerGid
    #ARG dockerGid=999
    #将docker内部的jenkins用户添加到docker用户组里面
    #RUN echo "docker:x:${dockerGid}:jenkins" >> /etc/group
    
    #更新安装dotnet-sdk3.1
    #https://docs.microsoft.com/zh-cn/dotnet/core/install/linux-debian#debian-10-
    RUN wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb -O packages-microsoft-prod.deb 
    && dpkg -i packages-microsoft-prod.deb
    #安装dotnet-sdk和git
    RUN apt-get update && apt-get install -y apt-transport-https && apt-get update && apt-get install -y dotnet-sdk-3.1 git libltdl7
    #定义一个参数dockerGid
    ARG dockerGid=999
    #将docker内部的jenkins用户添加到docker用户组里面
    RUN echo "docker:x:${dockerGid}:jenkins" >> /etc/group
    
    
    #安装Nodejs
    #RUN curl -sL https://rpm.nodesource.com/setup_8.x | bash - 
    #&& yum install nodejs
    ADD ./node-v10.15.1-linux-x64.tar /home
    RUN ln -s /home/node-v10.15.1-linux-x64/bin/node /usr/local/bin/node 
    && ln -s /home/node-v10.15.1-linux-x64/bin/npm /usr/local/bin/npm
    #RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - 
    #&& apt-get install -y nodejs
    
    
    #Jenkins时区问题
    RUN rm -rf /etc/localtime && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
    && echo 'Asia/Shanghai' > /etc/timezone 
    
    USER jenkins
    
    #入口操作
    ENTRYPOINT ["/bin/tini", "--", "/usr/local/bin/jenkins.sh"]
    
    
    
    #创建容器命令如下
    #docker run --name csit-dotnet-jenkins --privileged=true -d -p 4040:8080 
    #-v /home/app:/home/jenkins_publish     #程序目录
    #-v /home/jenkins/jenkins_home:/var/jenkins_home #jenkins目录
    #-v /var/run/docker.sock:/var/run/docker.sock   #docker命令
    #-v $(which docker):/usr/bin/docker                    #docker命令
    #-v /usr/local/bin/docker-compose:/usr/local/bin/docker-compose
    #dotnet-jenkins
    #docker run --name csit-dotnet-jenkins --privileged=true -d -p 4040:8080 -v /home/app:/home/jenkins_publish -v /home/jenkins/jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker -v /usr/local/bin/docker-compose:/usr/local/bin/docker-compose dotnet-jenkins

     2020年6月13日 更新:

    使用官方的jenkins镜像后面尝试一堆插件安装失败,百度发现其实已经有人遇到这写问题了(  https://www.jianshu.com/p/9a3814c88bf2),其实就是jenkins版本太低了

    解决方法,一个是自己官网下载包,然后打包成镜像,二种就是用别人的,我图懒,就用别人的了

    所以修改jenkins的基础镜像 

    FROM jenkins/jenkins:lts
  • 相关阅读:
    不重复随机数生成
    centos 输入密码正确进不去系统
    程序退出异常_DebugHeapDelete和std::numpunct
    iptables导致数据包过多时连接失败
    linux服务器并发与tcmalloc
    Windows server 2008 被ntlmssp安装攻击 解决
    转载 html div三列布局占满全屏(左右两列定宽或者百分比、中间自动适应,div在父div中居底)
    WIN2003使用IP安全策略只允许指定IP远程桌面连接
    如何让电脑公司Win7系统自动关闭停止响应的程序
    win7 64的系统安装。net4.0总是提示安装未成功
  • 原文地址:https://www.cnblogs.com/txb1989/p/12418897.html
Copyright © 2020-2023  润新知