1. 程式人生 > >Getting Started With ASP.NET Core & Docker

Getting Started With ASP.NET Core & Docker

Containers — Docker: Introduction

Now, let’s move on to a brief explanation on what are the main purposes of using containers in software development.

What is a container?

Containers are the idea of running multiple applications on a single host. It’s similar to compute virtualization, but instead of virtualizing a server to create multiple operating systems, containers offer a more lightweight alternative by essentially virtualizing the operating system, allowing multiple workloads to run on a single host.

What is Docker?

Docker is an open-source project for automating the deployment of applications as portable, self-sufficient containers that can run on the cloud or on-premises. It is a great tool to make it easier to create, deploy and run applications by using containers to do such tasks.

Docker (Containers) vs Virtual Machines (VM)

Docker does a more efficient usage of the underlying system and resources by sharing the same libraries and the same kernel. Therefore, it is more lightweight than virtual machines. Containers do not replace VMs entirely but they do compliment each other in many ways for us (developers).

VMs, unlike Docker, are great at providing full process isolation for applications, but this isolation comes at a great cost. (Heavy, inefficient, expensive). This doesn’t mean Docker is bad at isolating processes and applications but it’s not their intention to be like that either. These are tools for different scenarios and we welcome them as such.

Why docker?

Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.

Let’s get down to work!

Now let’s follow this simple and brief tutorial to create, build & deploy a ASP.NET Core MVC website into a Docker container.

I uploaded the project to a GitHub repository. However, I strongly encourage you to follow this guide. You will learn something new for sure!

What will we learn today?

  • To create a basic ASP.NET Core MVC web app.
  • To build a Dockerfile and understand its purpose.
  • To Deploy the web app into a Docker container.

Prerequisites

  • Windows 10 Pro-Enterprise with the latest anniversary update installed / macOS / Linux.
  • Docker for Windows/Linux/macOS
  • .NET Core 2.1 SDK (Software Development Kit).
  • A suitable text-editor. (I recommend using Visual Studio Code).

1. Setting up Docker

If you have a computer with macOS, please follow this guide from Docker website.

If you have any distribution of Linux installed please follow this guide.

For windows users, continue with this guide for setting it up:

Assuming you have already installed .NET Core SDK and you meet the OS requirements described earlier, let’s move on to installing Docker for Windows.

  1. Double click Docker for Windows Installer.exe to run the installer
  2. Follow the install wizard to accept the license, authorize the installer, and proceed with the install.
  3. Click Finish on the setup complete dialog to launch Docker.
  4. Docker will ask you to sign out to complete the installation.
  5. After signing in again, if you have Hyper-V and Containers windows features disabled, docker will ask you to enable them and then will restart your computer automatically. Go ahead and let docker enable these features for you.
Step 7

7. That’s it! You have successfully installed docker.

2. Starting Docker

Linux users: docker services start automatically after installing it.

Mac users:

  1. Double-click Docker.app in the Applications folder to start Docker.
  2. You are prompted to authorize Docker.app with your system password after you launch it. Privileged access is needed to install networking components and links to the Docker apps.
  3. The whale in the top status bar indicates that Docker is running, and accessible from a terminal.

Windows users:

  1. Docker does not start automatically after installation. To start it, search for Docker, select Docker for Windows in the search results, and click it.
  2. When the whale in the status bar stays steady, Docker is up-and-running, and accessible from any terminal window.

3. Creating a HelloWorld MVC Web App!

  1. Create a folder/directory named HelloWorld anywhere you please (this will be the root directory for the project.)
  2. Open the command prompt/terminal in the HelloWorld directory and execute:
> dotnet new mvc

This will create a base dummy project with the minimum necessary files and dependencies to run the ASP.NET Core MVC project.

Optionally, you can run this command in the project directory to build and start the website locally:

> dotnet run