How To Run .Net Core Console Application With Ubuntu

Microsoft launched .Net Core framework with the support of cross-platform framework. it means now we need to develop once only and we can run it on different platforms like windows, linux, macOS. Let's see with the example below how we can run a .net core console application with ubuntu.

Please follow the below steps to run .net core application on Ubuntu server,

Step 1

Need to create .net core console application POC.

Console App SP

Just for demo purpose added sample counter code. It will increase counter number as per user input.

Step 2

Create a docker image of dotnet core application.

To create a docker image need to install docker on your machine.

Download docker from link: https://docs.docker.com/desktop/windows/install/

Before installing docker please make sure the below things are enabled on the machine.

Docker Instructions

Virtualization should be enabled. We can enable it from windows features On or Off.

Now Install docker downloaded from link. After installation, it will look like below.

Installed Docker

Also, make sure to check docker in the command prompt as below image.

Installed Docker Command

Step 3

Once the docker is installed successfully. Start creating a docker image.

Create Dockerfile.txt in the root folder of the application and add below code in that.

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app

# Copy everything
COPY . ./

# Restore as distinct layers
RUN dotnet restore

# Build and publish a release
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DotNet.Docker.dll"]

Run below Command in package manager console

docker build -t counter-image -f Dockerfile.txt .

Once command runs successfully Run Command 

docker images

It should show created image in docker as below image,

Docker Images Check Command

Once an image is created. Run an image on a windows machine. Run below command 

docker run -it -rm counter-image

Window Docker Run

App should start running like above image.

Step 4

Run the same image on Ubuntu Machine.

To Run Ubuntu on Windows, we can use the Microsoft store application for ubuntu.

We can install it from the Microsoft Store.

Ubuntu Install

After installation, it will look like the below.

Start Ubuntu

After installation open Ubuntu.

Open Ubuntu

Configure username and password after opening ubuntu as below,

Configure Ubuntu

Now Ubuntu is configured on windows machine. 

Step 5 - Export Image so we can import in Ubuntu machine

Command to export image

docker save -o D:/Temp/docker_image/counter_image.tar counter-image

Once the command is executed successfully. In the directory, it will look like the below

File Explorer

Import Image in Ubuntu generated from a windows machine

Move to the path where the image is generated. ( you have to copy the image from the remote machine in your ubuntu machine.)

Once you moved to path now load image. Execute below command,

sudo docker load -i docker_image/counter_image.tar

Unbuntu Run Application

Run docker Image in Ubuntu machine

To run docker image execute below command in ubnutu.

docker run -it --rm --entrypoint "bash" counter-image

Once app starts you need to execute it using below command

dotnet DotNet.Docker.dll

Whatever the application name dll file is there needs to run it and the app will start running on the ubuntu machine.

References used for the above POC and documentation:

I hope you like this article and it's helpful. 

Similar Articles