Microsoft launched the .Net Core framework with the support of a cross-platform framework. it means now we need to develop once only and we can run it on different platforms like Windows, Linux, and 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 the .net core application on the Ubuntu server,
Step 1
Need to create .net core console application POC.
Just for demo purposes added sample counter code. It will increase the counter number as per user input.
Step 2
Create a docker image of the dotnet core application.
To create a docker image need to install docker on your machine.
Download docker from the link: https://docs.docker.com/desktop/windows/install/
Before installing docker please make sure the below things are enabled on the machine.
Virtualization should be enabled. We can enable it from Windows features On or Off.
Now Install the docker downloaded from the link. After installation, it will look like below.
Also, make sure to check docker in the command prompt as below image.
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 the 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 the below Command in the package manager console
docker build -t counter-image -f Dockerfile.txt
Once the command runs successfully Run Command
docker images
It should show the created image in docker as below image,
Once an image is created. Run an image on a Windows machine. Run below command
docker run -it -rm counter-image
The app should start running like the 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.
After installation, it will look like the one below.
After installation open Ubuntu.
Configure username and password after opening Ubuntu as below,
Now Ubuntu is configured on a Windows machine.
Step 5 - Export the Image so we can import to the 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
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 the path load the image. Execute the below command,
sudo docker load -i docker_image/counter_image.tar
Run docker Image in Ubuntu machine
To run the docker image execute the below command in ubuntu.
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.