Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding .net core to docker container with Jenkins

I'm trying to create a dockerfile that will build an image with .net core 2.0 and Jenkins. I'm kind of new to Docker but want to include .net core 2.0 in my container with Jenkins so I don't have to worry about .net core being installed on the target machine and can build .net core apps with Jenkins in my container. Am I missing something here?

it builds fine up until it runs the apt-get update command and I get the following error: E: Malformed entry 1 in list file /etc/apt/sources.list.d/dotnetdev.list (component) E: The list of sources could not be read.

I'm using the steps to install on ubuntu at this link: https://learn.microsoft.com/en-us/dotnet/core/linux-prerequisites?tabs=netcore2x

My Dockerfile looks like this:
FROM jenkins # Install .NET Core SDK USER root RUN mkdir -p /jenkins WORKDIR /jenkins

ENV DOTNET_CORE_SDK_VERSION 2.0 RUN curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor >/jenkins/microsoft.gpg RUN mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg RUN sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod xenial main" > /etc/apt/sources.list.d/dotnetdev.list' RUN apt-get update RUN apt-get install dotnet-sdk-2.0.0

like image 429
Michael C Avatar asked Jan 04 '18 23:01

Michael C


3 Answers

As of this response you can use the following Dockerfile to get .NetCore 2 installed into the Jenkins container. You can obviously take this further and install the needed plugins and additional software as needed. I hope this helps you out!

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://learn.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
like image 81
Dennis Fazekas Avatar answered Oct 24 '22 02:10

Dennis Fazekas


You can run these commands inside the Docker container in order to install .NET Core. They can also be stored in a Dockerfile (as per @Zooly57)

Install the latest .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

Or LTS version of .NET Core

sudo apt install libunwind8 gettext apt-transport-https
curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --channel LTS

Contents of the script here: https://github.com/dotnet/cli/blob/master/scripts/obtain/dotnet-install.sh

like image 1
Tomy Avatar answered Oct 24 '22 02:10

Tomy


Fantastic answer by Dennis, it's exactly what I ended up doing. It was a nice introduction to Docker as well :-)

Here's my Dockerfile for Jenkins 2.249.2 (LTS at the time of writing) on Debian 9 (stretch):

# 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

The dotnet command worked without setting any paths.

I guess when a newer version of Jenkins is released that uses Debian 10, I'll just update the FROM line then the Microsoft repository URL.

like image 1
Shahin Dohan Avatar answered Oct 24 '22 02:10

Shahin Dohan