Bri*_*ian 5 linux jenkins docker
因此,在某些方面我很惊喜地看到 Jenkins Docker 插件将 Docker 图像“推送”到我的 Docker 主机金属,但这也令人困惑,因为我的构建是在 Docker 主机金属上运行的 Docker Slave Containers 中进行的。甚至我的 Jenkins master 也在 Docker 容器中运行,而不是直接在金属上运行......
遵循这个流行的Jenkins 主/从指南,我到达了让 Jenkins 构建在临时 Docker 容器中工作的地步。
这意味着当我为 Jenkins 构建我的一些源代码软件组件/服务时,构建在 Jenkins 从属设备中启动,该从属设备恰好是由 Jenkins Docker 插件启动的 Docker 容器。
Jenkins 的工作空间在这个从容器中,安装了 Docker Plugin 的 Jenkins master 会在构建完成后处理这个从容器。请参阅我为帮助解释而制作的图表:
在你消化了这个图之后,一些重要的后续要点:
所以在这个 Docker Slave 中,我的软件组件/服务构建工件被创建,它可以是,例如,一个 .dll 或一个 .war。尽管我的构建工件将是 Docker 映像,但碰巧是这种情况。需要明确的是,我正在一个正在运行的 Docker 容器(Jenkins Slave)中构建一个 Docker 镜像。
我的困惑始于我期望我必须显式运行 cmd 才能将我的软件组件 Docker 映像构建工件推送到 Docker 注册表。否则,当 Jenkins 构建作业完成时,Docker 插件将关闭 Docker 容器从属容器,处理 (rm) 从属容器,然后我将丢失该从属容器内的构建工件。
实际发生的事情,以及为什么我感到惊喜的原因,至少在我开始开发和运行的短期内,是构建工件 Docker 映像显示在 Docker 主机金属上,docker image ls
.
我很惊讶 Docker 插件会达到这种假设/帮助级别...我知道 Docker 插件允许您配置 Docker 注册表,并且您可以添加构建步骤以构建/发布到我假设的 Docker 云该云被视为图像的注册表,也可能是运行这些图像的地方:
特别有趣的是,我没有在任何构建步骤中使用 Docker 插件,我只是使用 Docker 插件来配置一个 Slave Container 来构建 Jenkins 项目:
我唯一的构建步骤是我执行一个 Shell 脚本,是的,这个脚本碰巧最终构建了一个 Docker 镜像,但 Docker 插件不会知道这一点:
Docker 插件启动 Docker Slave Containers,我配置 Docker 插件并告诉它一个 Docker 主机(在我的情况下是我的金属)一个 Cloud 是 Docker 插件所称的 Docker 主机以及 Docker 从属映像以在该 Docker 主机上使用/云:
我是否只是对 Jenkins 构建工作空间在 Docker 从属容器内的隔离程度有误解?
Docker 插件是否只是默认使用我为碰巧在 Jenkins Docker 从属容器内运行的所有 docker 命令设置的唯一一个 Docker Cloud(我的 Docker 主机金属)?(顺便说一下,一个从属容器确实安装了 Docker-CE)
我的詹金斯大师Dockerfile:
#reference
#https://engineering.riotgames.com/news/putting-jenkins-docker-container
FROM jenkins:2.60.1
MAINTAINER Brian Ogden
USER root
#Timezone
ENV TZ=America/Los_Angeles
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# Prep Jenkins Directories
RUN mkdir /var/log/jenkins
RUN mkdir /var/cache/jenkins
RUN chown -R jenkins:jenkins /var/log/jenkins
RUN chown -R jenkins:jenkins /var/cache/jenkins
# Copy in local config filesfiles
COPY plugins.sh /usr/local/bin/plugins.sh
RUN chmod +x /usr/local/bin/plugins.sh
# Install default plugins
# Set list of plugins to download / update in plugins.txt like this
# pluginID:version
# credentials:1.18
# maven-plugin:2.7.1
# ...
# NOTE : Just set pluginID to download latest version of plugin.
# NOTE : All plugins need to be listed as there is no transitive dependency resolution.
COPY plugins.txt /tmp/plugins.txt
RUN /usr/local/bin/plugins.sh /tmp/plugins.txt
USER jenkins
#give Jenkins a nice 8 GB memory pool and room to handle garbage collection
#ENV JAVA_OPTS="-Xmx8192m"
#give Jenkins a nice base pool of handlers and a cap
#ENV JENKINS_OPTS="--handlerCountStartup=100 --handlerCountMax=300"
ENV JENKINS_OPTS="--logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war"
Run Code Online (Sandbox Code Playgroud)
我将 docker-compose 和 Docker 卷与我的 Jenkins Master,我的 docker-compose.yml 一起使用:
version: '2'
services:
data:
build: data
image: tsl.devops.jenkins.data.image
container_name: tsl.devops.jenkins.data.container
master:
build: master
image: tsl.devops.jenkins.master.image
container_name: tsl.devops.jenkins.master.container
volumes_from:
- data
ports:
- "50000:50000"
#network_mode: "host"
nginx:
build: nginx
image: tsl.devops.jenkins.nginx.image
container_name: tsl.devops.jenkins.nginx.container
ports:
- "80:80"
links:
- master:jenkins-master
slavebasic:
build:
context: ./slaves
dockerfile: basic/Dockerfile
image: tsl.devops.jenkins.slave.basic.image
container_name: tsl.devops.jenkins.slave.basic.container
slavedotnetcore:
build:
context: ./slaves
dockerfile: dotnetcore/Dockerfile
image: tsl.devops.jenkins.slave.dotnetcore.image
container_name: tsl.devops.jenkins.slave.dotnetcore.container
Run Code Online (Sandbox Code Playgroud)
我的Jenkins 主卷/驱动器 Dockerfile:
#reference
#https://engineering.riotgames.com/news/docker-jenkins-data-persists
FROM centos:7
MAINTAINER Brian Ogden
#create the Jenkins user in this container
RUN useradd -d "/var/jenkins_home" -u 1000 -m -s /bin/bash jenkins
#NOTE: we set the UID here to the same one the Cloudbees Jenkins image uses
#so we can match UIDs across containers, which is essential if you want
#to preserve file permissions between the containers. We also use the same home directory and bash settings.
#Jenkins log directory
RUN mkdir -p /var/log/jenkins
RUN chown -R jenkins:jenkins /var/log/jenkins
#Docker volume magic
VOLUME ["/var/log/jenkins", "/var/jenkins_home"]
USER jenkins
#just a little output reminder of the container's purpose
CMD ["echo", "Data container for Jenkins"]
Run Code Online (Sandbox Code Playgroud)
我的从Dockerfile:
FROM centos:7
MAINTAINER Brian Ogden
#the USER will be root by default just explicitly
#expressing it for better documentation
USER root
# Install Essentials
RUN yum update -y && \
yum clean all
#############################################
# Jenkins Slave setup
#############################################
RUN yum install -y \
git \
wget \
openssh-server \
java-1.8.0-openjdk \
sudo \
make && \
yum clean all
# gen dummy keys, centos doesn't autogen them like ubuntu does
RUN /usr/bin/ssh-keygen -A
# Set SSH Configuration to allow remote logins without /proc write access
RUN sed -ri 's/^session\s+required\s+pam_loginuid.so$/session optional pam_loginuid.so/' /etc/pam.d/sshd
# Create Jenkins User
RUN useradd jenkins -m -s /bin/bash
# Add public key for Jenkins login
RUN mkdir /home/jenkins/.ssh
COPY /files/id_rsa.pub /home/jenkins/.ssh/authorized_keys
#setup permissions for the new folders and files
RUN chown -R jenkins /home/jenkins
RUN chgrp -R jenkins /home/jenkins
RUN chmod 600 /home/jenkins/.ssh/authorized_keys
RUN chmod 700 /home/jenkins/.ssh
# Add the jenkins user to sudoers
RUN echo "jenkins ALL=(ALL) ALL" >> etc/sudoers
#############################################
#############################################
# Docker and Docker Compose Install
#############################################
#install required packages
RUN yum install -y \
yum-utils \
device-mapper-persistent-data \
lvm2 \
curl && \
yum clean all
#add Docker CE stable repository
RUN yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
#Update the yum package index.
RUN yum makecache fast
#install Docker CE
RUN yum install -y docker-ce-17.06.0.ce-1.el7.centos
#install Docker Compose 1.14.0
#download Docker Compose binary from github repo
RUN curl -L https://github.com/docker/compose/releases/download/1.14.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
#Apply executable permissions to the binary
RUN chmod +x /usr/local/bin/docker-compose
#############################################
#############################################
# .NET Core SDK
#############################################
RUN yum install -y \
libunwind \
libicu
RUN curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?linkid=848821
RUN mkdir -p /opt/dotnet && tar zxf dotnet.tar.gz -C /opt/dotnet
RUN ln -s /opt/dotnet/dotnet /usr/local/bin
#add Trade Service Nuget Server
RUN mkdir -p /home/jenkins/.nuget/NuGet
COPY /files/NuGet.Config /home/jenkins/.nuget/NuGet/NuGet.Config
RUN chown -R jenkins /home/jenkins/.nuget
RUN chgrp -R jenkins /home/jenkins/.nuget
RUN chmod 600 /home/jenkins/.nuget/NuGet/NuGet.Config
RUN chmod 700 /home/jenkins/.nuget/NuGet
#speed up dotnet core builds
ENV NUGET_XMLDOC_MODE skip
ENV DOTNET_SKIP_FIRST_TIME_EXPERIENCE true
#############################################
# Expose SSH port and run SSHD
EXPOSE 22
#Technically, the Docker Plugin enforces this call when it starts containers by overriding the entry command.
#I place this here because I want this build slave to run locally as it would if it was started in the build farm.
CMD ["/usr/sbin/sshd","-D"]
Run Code Online (Sandbox Code Playgroud)
一个示例软件/组件Dockerfile 将在 Jenkins Slave Docker 容器内创建一个 Docker 镜像构建工件:
FROM centos:7
MAINTAINER Brian Ogden
#Timezone
ENV TZ=America/Los_Angeles
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN yum update -y && \
yum clean all
#############################################
# .NET Core SDK
#############################################
RUN yum install -y \
libunwind \
libicu
RUN curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?linkid=848821
RUN mkdir -p /opt/dotnet && tar zxf dotnet.tar.gz -C /opt/dotnet
RUN ln -s /opt/dotnet/dotnet /usr/local/bin
#speed up dotnet core builds
ENV NUGET_XMLDOC_MODE skip
ENV DOTNET_SKIP_FIRST_TIME_EXPERIENCE true
#############################################
#############################################
# .NET Sevrice setup
#############################################
ARG ASPNETCORE_ENVIRONMENT
# Copy our code from the "/src/MyWebApi/bin/Debug/netcoreapp1.1/publish" folder to the "/app" folder in our container
WORKDIR /app
COPY ./src/TSL.Security.Service/bin/Debug/netcoreapp1.1/publish .
# Expose port 5000 for the Web API traffic
ENV ASPNETCORE_URLS http://+:5000
ENV ASPNETCORE_ENVIRONMENT $ASPNETCORE_ENVIRONMENT
EXPOSE 5000
# Run the dotnet application against a DLL from within the container
# Don't forget to publish your application or this won't work
ENTRYPOINT ["dotnet", "TSL.Security.Service.dll"]
#############################################
Run Code Online (Sandbox Code Playgroud)
根据您的 Docker 插件配置,您正在用作172.17.0.1
Docker 主机。从从容器或主容器来看,这将是在主机上运行的 Docker 守护进程(这里没有 Docker 中的 Docker 发生)。当您的 Jenkins 从机构建映像时(无论从机是作为容器运行还是在主机上运行),它都会使用主机上的 Docker,这就是您的映像显示在主机上的原因。
值得注意的是,数据可能首先进入从属设备使用的 Docker 卷(根据https://github.com/jenkinsci/docker/blob/9f29488b77c2005bbbc5c936d47e697689f8ef6e/DockerfileDockefile
上的Jenkins ,默认值为)。就您而言,这只是服务中的一个卷(不过,在 Compose v2 格式中,您可以只定义一个命名卷,不需要创建数据容器)。从这里,您的代码将通过位于 的 API 发送到主机上的 Docker 构建上下文。/var/jenkins_home
data
Dockerfile
tcp://172.17.0.1:4243
归档时间: |
|
查看次数: |
12620 次 |
最近记录: |