使用 Jenkins 将 .net 核心添加到 docker 容器
adding .net core to docker container with Jenkins
我正在尝试创建一个将使用 .net core 2.0 和 Jenkins 构建映像的 dockerfile。我是 Docker 的新手,但想在我的 Jenkins 容器中包含 .net core 2.0,这样我就不必担心 .net core 安装在目标机器上并可以构建 .net core 应用程序在我的容器中使用 Jenkins。我在这里错过了什么吗?
在运行 apt-get update 命令之前,它构建良好,我收到以下错误:
E: Malformed entry 1 in list file /etc/apt/sources.list.d/dotnetdev.list (component)
E: The list of sources could not be read.
我正在使用 ubuntu 上 link 上的安装步骤:
https://docs.microsoft.com/en-us/dotnet/core/linux-prerequisites?tabs=netcore2x
我的 Docker 文件如下所示:
<br>
来自詹金斯
# 安装.NET Core SDK
用户根
运行 mkdir -p /jenkins
WORKDIR /詹金斯</p>
<p>环境 DOTNET_CORE_SDK_VERSION 2.0
运行 卷曲 <a href="https://packages.microsoft.com/keys/microsoft.asc" rel="noreferrer">https://packages.microsoft.com/keys/microsoft.asc</a> | gpg --dearmor >/jenkins/microsoft.gpg
运行 mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
运行 sh -c 'echo "deb [arch=amd64] <a href="https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod" rel="noreferrer">https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod</a> xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
运行 获取更新
运行 apt-get 安装 dotnet-sdk-2.0.0
我认为您应该改用以下方法:
开发您的 asp.net 核心应用程序并签入 Git(任何源代码控制)
有一个构建服务器,其中安装了 Jenkins、.Net Core、Docker
配置Jenkins与Git通信(webhook/polling-查看是否有签入)
并配置一个 Jenkins 作业,它将执行以下操作
- 从 Git、
中提取最新的
- 恢复,
- 构建,
- 发布 asp.net 核心应用程序,
- 创建一个 docker 图像,该图像能够 运行 其中的 asp.net 核心应用程序
- 将刚刚创建的 docker 图片上传到您的 Docker 集线器
您可能不想完全按照上面提到的方式进行操作,尤其是源代码管理部分。但这种方法效果很好。
我在进行上述设置时遵循了这个 link。
希望对您有所帮助。谢谢!
截至此回复,您可以使用以下 Dockerfile 将 .NetCore 2 安装到 Jenkins 容器中。您显然可以更进一步,并根据需要安装所需的插件和其他软件。希望对您有所帮助!
FROM jenkins/jenkins:lts
# Switch to root to install .NET Core SDK
USER root
# Just for my sanity... Show me this distro information!
RUN uname -a && cat /etc/*release
# Based on instructiions at https://docs.microsoft.com/en-us/dotnet/core/linux-prerequisites?tabs=netcore2x
# Install depency for dotnet core 2.
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
curl libunwind8 gettext apt-transport-https && \
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg && \
mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg && \
sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/dotnetdev.list' && \
apt-get update
# Install the .Net Core framework, set the path, and show the version of core installed.
RUN apt-get install -y dotnet-sdk-2.0.0 && \
export PATH=$PATH:$HOME/dotnet && \
dotnet --version
# Good idea to switch back to the jenkins user.
USER jenkins
您可以 运行 在 Docker 容器中使用这些命令来安装 .NET Core。它们也可以存储在 Docker 文件中(根据 @Zooly57)
安装最新的 .NET Core 2.0:
sudo apt install libunwind8 gettext apt-transport-https
curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --channel 2.0
或 .NET Core 的 LTS 版本
sudo apt install libunwind8 gettext apt-transport-https
curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --channel LTS
这里的脚本内容:
https://github.com/dotnet/cli/blob/master/scripts/obtain/dotnet-install.sh
丹尼斯 太棒了,这正是我最终所做的。这也是对 Docker 的一个很好的介绍:-)
这是我在 Debian 9(stretch)上的 Jenkins 2.249.2(撰写本文时为 LTS)的 Docker文件:
# Extend Jenkins 2.249.2 on Debian 9 (stretch)
FROM jenkins/jenkins:2.249.2-lts
# Switch to root user to install .NET SDK
USER root
# Print kernel and distro info
RUN echo "Distro info:" && uname -a && cat /etc/*release
# Install needed tools and upgrade installed packages
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
curl apt-transport-https software-properties-common \
&& apt-get upgrade -y
# Add Microsoft repository for .NET SDK
RUN curl -sSL https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN apt-add-repository https://packages.microsoft.com/debian/9/prod/
# Install .NET SDK
RUN apt-get update \
&& apt-get install -y dotnet-sdk-3.1
# Switch back to jenkins user
USER jenkins
dotnet 命令在没有设置任何路径的情况下工作。
我想当使用 Debian 10 的新版本 Jenkins 发布时,我将只更新 FROM 行,然后更新 Microsoft 存储库 URL。
对于最近为这个主题苦苦挣扎的任何人,这是我添加到我的 Dockerfile 底部以安装 .NET SDK 的内容;
USER root
# Install dependencies
RUN apt-get install wget
RUN wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
RUN dpkg -i packages-microsoft-prod.deb
RUN rm packages-microsoft-prod.deb
# Install .NET SDK 6.0
RUN apt-get update;
RUN apt-get install -y apt-transport-https
RUN apt-get update
RUN apt-get install -y dotnet-sdk-6.0
RUN dotnet --version
这基于在 Ubuntu 18.04 上安装 SDK,因为这是 AKS 使用的版本,非常适合我的场景
https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu#dependencies
我正在尝试创建一个将使用 .net core 2.0 和 Jenkins 构建映像的 dockerfile。我是 Docker 的新手,但想在我的 Jenkins 容器中包含 .net core 2.0,这样我就不必担心 .net core 安装在目标机器上并可以构建 .net core 应用程序在我的容器中使用 Jenkins。我在这里错过了什么吗?
在运行 apt-get update 命令之前,它构建良好,我收到以下错误:
E: Malformed entry 1 in list file /etc/apt/sources.list.d/dotnetdev.list (component)
E: The list of sources could not be read.
我正在使用 ubuntu 上 link 上的安装步骤: https://docs.microsoft.com/en-us/dotnet/core/linux-prerequisites?tabs=netcore2x
我的 Docker 文件如下所示:
<br>
来自詹金斯
# 安装.NET Core SDK
用户根
运行 mkdir -p /jenkins
WORKDIR /詹金斯</p>
<p>环境 DOTNET_CORE_SDK_VERSION 2.0
运行 卷曲 <a href="https://packages.microsoft.com/keys/microsoft.asc" rel="noreferrer">https://packages.microsoft.com/keys/microsoft.asc</a> | gpg --dearmor >/jenkins/microsoft.gpg
运行 mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
运行 sh -c 'echo "deb [arch=amd64] <a href="https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod" rel="noreferrer">https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod</a> xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
运行 获取更新
运行 apt-get 安装 dotnet-sdk-2.0.0
我认为您应该改用以下方法:
开发您的 asp.net 核心应用程序并签入 Git(任何源代码控制)
有一个构建服务器,其中安装了 Jenkins、.Net Core、Docker
配置Jenkins与Git通信(webhook/polling-查看是否有签入)
并配置一个 Jenkins 作业,它将执行以下操作
- 从 Git、 中提取最新的
- 恢复,
- 构建,
- 发布 asp.net 核心应用程序,
- 创建一个 docker 图像,该图像能够 运行 其中的 asp.net 核心应用程序
- 将刚刚创建的 docker 图片上传到您的 Docker 集线器
您可能不想完全按照上面提到的方式进行操作,尤其是源代码管理部分。但这种方法效果很好。 我在进行上述设置时遵循了这个 link。
希望对您有所帮助。谢谢!
截至此回复,您可以使用以下 Dockerfile 将 .NetCore 2 安装到 Jenkins 容器中。您显然可以更进一步,并根据需要安装所需的插件和其他软件。希望对您有所帮助!
FROM jenkins/jenkins:lts
# Switch to root to install .NET Core SDK
USER root
# Just for my sanity... Show me this distro information!
RUN uname -a && cat /etc/*release
# Based on instructiions at https://docs.microsoft.com/en-us/dotnet/core/linux-prerequisites?tabs=netcore2x
# Install depency for dotnet core 2.
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
curl libunwind8 gettext apt-transport-https && \
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg && \
mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg && \
sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/dotnetdev.list' && \
apt-get update
# Install the .Net Core framework, set the path, and show the version of core installed.
RUN apt-get install -y dotnet-sdk-2.0.0 && \
export PATH=$PATH:$HOME/dotnet && \
dotnet --version
# Good idea to switch back to the jenkins user.
USER jenkins
您可以 运行 在 Docker 容器中使用这些命令来安装 .NET Core。它们也可以存储在 Docker 文件中(根据 @Zooly57)
安装最新的 .NET Core 2.0:
sudo apt install libunwind8 gettext apt-transport-https
curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --channel 2.0
或 .NET Core 的 LTS 版本
sudo apt install libunwind8 gettext apt-transport-https
curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --channel LTS
这里的脚本内容: https://github.com/dotnet/cli/blob/master/scripts/obtain/dotnet-install.sh
丹尼斯
这是我在 Debian 9(stretch)上的 Jenkins 2.249.2(撰写本文时为 LTS)的 Docker文件:
# Extend Jenkins 2.249.2 on Debian 9 (stretch)
FROM jenkins/jenkins:2.249.2-lts
# Switch to root user to install .NET SDK
USER root
# Print kernel and distro info
RUN echo "Distro info:" && uname -a && cat /etc/*release
# Install needed tools and upgrade installed packages
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
curl apt-transport-https software-properties-common \
&& apt-get upgrade -y
# Add Microsoft repository for .NET SDK
RUN curl -sSL https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN apt-add-repository https://packages.microsoft.com/debian/9/prod/
# Install .NET SDK
RUN apt-get update \
&& apt-get install -y dotnet-sdk-3.1
# Switch back to jenkins user
USER jenkins
dotnet 命令在没有设置任何路径的情况下工作。
我想当使用 Debian 10 的新版本 Jenkins 发布时,我将只更新 FROM 行,然后更新 Microsoft 存储库 URL。
对于最近为这个主题苦苦挣扎的任何人,这是我添加到我的 Dockerfile 底部以安装 .NET SDK 的内容;
USER root
# Install dependencies
RUN apt-get install wget
RUN wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
RUN dpkg -i packages-microsoft-prod.deb
RUN rm packages-microsoft-prod.deb
# Install .NET SDK 6.0
RUN apt-get update;
RUN apt-get install -y apt-transport-https
RUN apt-get update
RUN apt-get install -y dotnet-sdk-6.0
RUN dotnet --version
这基于在 Ubuntu 18.04 上安装 SDK,因为这是 AKS 使用的版本,非常适合我的场景 https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu#dependencies