But now, with .NET Core, you can develop and run .NET application on Linux. Now you can use all the benefits of the Linux platform.

It means you shouldn't worry about dependencies and software versions - everything is inside your container

.

ASP.NET Core

What is ASP.NET Core?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-based, Internet-connected applications.

What about .NET Core?

Install Net Core

Windows: you have to download and install the .NET SDK;

Linux: you can use the commands described here;

macOS: download and install the pkg;

$ dotnet --version

$ dotnet --version 2.2.203

Create ASP.NET Core application

NetCoreExample

$ dotnet new mvc

. ├── appsettings.Development.json ├── appsettings.json ├── Controllers │ └── HomeController.cs ├── Models │ └── ErrorViewModel.cs ├── NetCoreExample.csproj ├── obj │ ├── NetCoreExample.csproj.nuget.cache │ ├── NetCoreExample.csproj.nuget.dgspec.json │ ├── NetCoreExample.csproj.nuget.g.props │ ├── NetCoreExample.csproj.nuget.g.targets │ └── project.assets.json ├── Program.cs ├── Properties │ └── launchSettings.json ├── Startup.cs ├── Views │ ├── Home │ │ ├── Index.cshtml │ │ └── Privacy.cshtml │ ├── Shared │ │ ├── _CookieConsentPartial.cshtml │ │ ├── Error.cshtml │ │ ├── _Layout.cshtml │ │ └── _ValidationScriptsPartial.cshtml │ ├── _ViewImports.cshtml │ └── _ViewStart.cshtml └── wwwroot ├── css │ └── site.css ├── favicon.ico ├── js │ └── site.js └── lib ├── bootstrap │ ├── dist │ │ ├── css │ │ │ ├── bootstrap.css │ │ │ ├── bootstrap.css.map │ │ │ ├── bootstrap-grid.css │ │ │ ├── bootstrap-grid.css.map │ │ │ ├── bootstrap-grid.min.css │ │ │ ├── bootstrap-grid.min.css.map │ │ │ ├── bootstrap.min.css │ │ │ ├── bootstrap.min.css.map │ │ │ ├── bootstrap-reboot.css │ │ │ ├── bootstrap-reboot.css.map │ │ │ ├── bootstrap-reboot.min.css │ │ │ └── bootstrap-reboot.min.css.map │ │ └── js │ │ ├── bootstrap.bundle.js │ │ ├── bootstrap.bundle.js.map │ │ ├── bootstrap.bundle.min.js │ │ ├── bootstrap.bundle.min.js.map │ │ ├── bootstrap.js │ │ ├── bootstrap.js.map │ │ ├── bootstrap.min.js │ │ └── bootstrap.min.js.map │ └── LICENSE ├── jquery │ ├── dist │ │ ├── jquery.js │ │ ├── jquery.min.js │ │ └── jquery.min.map │ └── LICENSE.txt ├── jquery-validation │ ├── dist │ │ ├── additional-methods.js │ │ ├── additional-methods.min.js │ │ ├── jquery.validate.js │ │ └── jquery.validate.min.js │ └── LICENSE.md └── jquery-validation-unobtrusive ├── jquery.validate.unobtrusive.js ├── jquery.validate.unobtrusive.min.js └── LICENSE.txt

$ dotnet run

Hosting environment: Development Content root path: {project directory} Now listening on: https://localhost:5001 Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down.

Docker

What is Docker?

What are containers?

Install Docker

Windows: you can download the installer and read install instructions here;

Linux: here you can find manuals for install on CentOS, Fedora, Debian and Ubuntu;

macOS: you can download package and read install instructions here;

hello-world

$ sudo docker run hello-world

$ sudo docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/

Create a Dockerfile for an ASP.NET Core application

with a name

`Dockerfile`

# First we add a dotnet SDK image to build our app inside the container FROM microsoft/dotnet:sdk AS build-env WORKDIR /app # Copy csproj and restore as distinct layers COPY *.csproj ./ RUN dotnet restore # Copy everything else and build COPY . ./ RUN dotnet publish -c Release -o out # Here we use ASP.NET Core runtime to build runtime image FROM microsoft/dotnet:aspnetcore-runtime WORKDIR /app COPY --from=build-env /app/out . ENTRYPOINT ["dotnet", "NetCoreExample.dll"]

`.dockerignore`

bin\ obj\

Build and run the Docker image

$ sudo docker build -t netcoreexample .

`-t netcoreexample`

`netcoreexample`

`.`

$ sudo docker build -t netcoreexample . [sudo] password for user: Sending build context to Docker daemon 5.87MB Step 1/10 : FROM microsoft/dotnet:sdk AS build-env sdk: Pulling from microsoft/dotnet e79bb959ec00: Pull complete d4b7902036fe: Pull complete 1b2a72d4e030: Pull complete d54db43011fd: Pull complete 8216822d0007: Pull complete 9844002c3ceb: Pull complete 12393f54ba00: Pull complete Digest: sha256:c2b122784f488ab8200c292c3ee15ecc47d649801fb879cdb7e3df3af11f4d43 Status: Downloaded newer image for microsoft/dotnet:sdk ---> 913796b9a530 Step 2/10 : WORKDIR /app ---> Running in 2c5bbf9a1aa1 Removing intermediate container 2c5bbf9a1aa1 ---> 8f8a084711ed Step 3/10 : COPY *.csproj ./ ---> 6b3200116845 Step 4/10 : RUN dotnet restore ---> Running in fade61cbd033 Restore completed in 1.18 sec for /app/NetCoreExample.csproj. Removing intermediate container fade61cbd033 ---> 4068b74c1e11 Step 5/10 : COPY . ./ ---> 5a0078f9e396 Step 6/10 : RUN dotnet publish -c Release -o out ---> Running in af23a3bc28f1 Microsoft (R) Build Engine version 16.0.450+ga8dc7f1d34 for .NET Core Copyright (C) Microsoft Corporation. All rights reserved. Restore completed in 1.02 sec for /app/NetCoreExample.csproj. NetCoreExample -> /app/bin/Release/netcoreapp2.2/NetCoreExample.dll NetCoreExample -> /app/bin/Release/netcoreapp2.2/NetCoreExample.Views.dll NetCoreExample -> /app/out/ Removing intermediate container af23a3bc28f1 ---> 52160e57a4da Step 7/10 : FROM microsoft/dotnet:aspnetcore-runtime aspnetcore-runtime: Pulling from microsoft/dotnet 27833a3ba0a5: Pull complete b134f247e232: Pull complete 6ec125eff9bf: Pull complete 5abd259dfd59: Pull complete Digest: sha256:6c9c94185bb4d186f2f525a2be566f015a3399c987685082b867529501ff12fa Status: Downloaded newer image for microsoft/dotnet:aspnetcore-runtime ---> 091bfd07b907 Step 8/10 : WORKDIR /app ---> Running in d740a99581e3 Removing intermediate container d740a99581e3 ---> 8d06c6bd6f74 Step 9/10 : COPY --from=build-env /app/out . ---> 400e9ab4fbce Step 10/10 : ENTRYPOINT ["dotnet", "NetCoreExample.dll"] ---> Running in c3d639ad2cb4 Removing intermediate container c3d639ad2cb4 ---> 8b535431f160 Successfully built 8b535431f160 Successfully tagged netcoreexample:latest

`Successfully tagged netcoreexample:latest`

`netcoreexample:latest`

`docker build`

$ sudo docker run -d -p 8080:80 --name example netcoreexample

Option `-d` means that we want to run our container in background and print container ID.

means that we want to run our container in background and print container ID. Option `-p 8080:80` says that we want to bind the port 80 of the container to port 8080 of the host machine. It means that when you will access port 8080 of the host machine, in reality, you will see the output from the port 80 inside the container, where we host our site.

says that we want to bind the port 80 of the container to port 8080 of the host machine. It means that when you will access port 8080 of the host machine, in reality, you will see the output from the port 80 inside the container, where we host our site. Option `--name example` assigns the name `example` to the container.

assigns the name to the container. And finally, `netcoreexample` is the name of the container we want to run (remember `-t netcoreexample` on the build step?).

$ sudo docker run -d -p 8080:80 --name myapp netcoreexample 82995c0e787bd432eb5ff1bd6001b22593732a674b006b66fb19d016d48356ef

What's next?

$ sudo docker image ls

$ sudo docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE netcoreexample latest 8b535431f160 22 minutes ago 265MB 52160e57a4da 22 minutes ago 1.75GB microsoft/dotnet sdk 913796b9a530 10 hours ago 1.74GB microsoft/dotnet aspnetcore-runtime 091bfd07b907 10 hours ago 260MB hello-world latest fce289e99eb9 3 months ago 1.84kB

$ sudo docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 82995c0e787b netcoreexample "dotnet NetCoreExamp…" 25 minutes ago Up 25 minutes 0.0.0.0:8080->80/tcp myapp

`myapp`

$ sudo docker stop myapp myapp

Conclusion