Tuesday, December 27, 2016

ASP.NET Core 1.0 SQLite app in Linux Docker container on Windows 10 with yeoman

The Windows 10 anniversary edition has build-in support for Docker. In this tutorial I will show you how to scaffold and deploy a SQLite database driven ASP.NET core app and deploy it into a Linux Docker container. If you are new to Docker on Windows 10, you can refer to a previous post that will help you install and configure it.

The pre-requisites needed to proceed are:
  1. Windows 10 Anniversary edition or later
  2. Hyper-V enabled on the host Windows 10 Anniversary operating system
  3. Yeoman command line interface
  4. .NET Core 1.0 is installed on your computer
You can get more information on installing the Yeoman command line interface at http://yeoman.io/codelab/setup.html.

Scaffolding an ASP.NET Core SQLite application with Yeoman

1) Open a command-line window in Windows 10.

2) In a suitable working directory on your hard-drive, create a folder named dev with the following command:
mkdir dev

3) Go into the newly created directory with:
cd dev

4) In the dev folder, create a file named global.json with the following content:
{
  "sdk": {
    "version": "1.0.0-preview2-003131"
  }
}
Note that for this example, we will be using ASP.NET Core version 1.0, as specified in the global.json file.

5) You will now use the Yeoman scaffolding tool to create an ASP.NET Core 1.0 application that uses SQLite as its database. Type the following inside the dev folder:

yo aspnet

The following menu will display:

image

6) Hit the down-arrow on your keyboard until you get to “Web Application” then hit ENTER.

image

7) Hit ENTER again to select Bootstrap.

image

8) When asked for an application name, type-in YoAspnet then hit ENTER. The application will get created for you. This is what it should look like when it is done:

image

9) Note these suggested instructions:
cd "YoAspnet"
dotnet restore
dotnet build
dotnet ef database update
dotnet run
Execute the above five instructions in sequence. Eventually, your command-line windows will be running the Kestrel web server on port 5000:

image

10) View the web application by pointing your browser to http://localhost:5000. This is what the web application, running on your host computer,  looks like:

image

We are now ready to deploy this SQLite database driven application into a Linux Docker container.

Deploying web app into Linux Docker container

Stop the web application by hitting CTRL + C on your keyboard.

Delete the bin and obj folders as we do not need to copy these folders into the container.

We will be deploying our web app into a Linux container. Therefore, right-click on the Docker (whale) icon in your system tray and make sure it displays:

image

If it does not show the above message then you should switch to the Linux container.

Create a text file in the YoAspnet folder named Dockerfile.linux and add to it the following content:
FROM microsoft/aspnetcore-build:1.0.3-projectjson
ENTRYPOINT ["dotnet", "run"]
ENV ASPNETCORE_ENVIRONMENT=Development
WORKDIR /app
COPY . .
RUN dotnet restore
RUN dotnet ef database update
EXPOSE 80
I will explain what each command in Dockerfile.linux does.

FROM microsoft/aspnetcore-build:1.0.3-projectjson We will be using the official Microsoft docker container named microsoft/aspnetcore-build:1.0.3-projectjson as our starting image. This is a suitable container because it already has the .NET Core sdk + npm + bower. Also, it is based on .NET Core version 1.0, which is compatible to our web app.
ENTRYPOINT ["dotnet", "run"] The web application will be started by executing the “dotnet run” command which starts the web server.
ENV ASPNETCORE_ENVIRONMENT=Development We are setting the ASPNETCORE_ENVIRONMENT environment variable so that we can see detailed error messages. In production, you will change this to Production.
WORKDIR /app A directory named /app is created in the Linux container to host our source code.
COPY . . Our source code is copied from the YoAspnet folder on our Windows 10 computer to the /app folder in the Linux container.
RUN dotnet restore This command executes “dotnet restore” in the /app folder in the Linux container.
RUN dotnet ef database update This command executes “dotnet ef database update” in the /app folder in the Linux container.
EXPOSE 80 Port 80 is the port number that will be exposed by the container.

Inside a command-prompt in the YoAspnet folder, execute the following to build an image named dotnetweb/linux that contains our web app:

docker build -t dotnetweb/linux -f Dockerfile.linux .

This will download the microsoft/aspnetcore-build:1.0.3-projectjson container from http://hub.docker.com and subsequently build our new image. When it is done, you can type the following command to see all the images you have:

docker images

image

As you can see, there are two images.
  1. dotnetweb/linux is the docker image we just built
  2. microsoft/aspnetcore-build:1.0.3-projectjson is the base image we used
To make a container from the image and run it, type the following command:

docker run -d -p 5000:80 dotnetweb/linux

As you can see, the container’s port 80 is exposed to the host as port 5000.
If you now point your browser to http://localhost:5000/, you will see the web application served by our Linux container.

image

We need to ensure that our database is indeed working. In order to do that, click on Register in the top-right corner of the web app and enter a user email and password:

image

After clicking on the Register button, you should see that the new user was indeed created:

image

Cleanup

Let us stop the container and clean up the image.

To see which containers are running, type the following:

docker ps

Take note of the container-id. In my case it is ee92b47148e1. To stop the container you can simply use the first two or three character of the container-id as follows:

docker stop ee9

To see which containers are still available, type:

docker ps -a

To delete the container completely, identify the first two to three character of the container-id then use the “docker rm” command similar to the following:

docker rm ee9

I you run “docker ps –a” again you will determine that the container is gone.
Find out what images you have by typing  the following command:

docker images

You can delete the image by id or by name, as follows:

docker rmi 5cc
                     OR                           docker rmi dotnetweb/linux

After you remove the image you can again type “docker images” to prove that the image is indeed deleted.

No comments:

Post a Comment