Code samples for my Desert Camp presentation, "Microservices with .NET Core and Docker"
To follow along w/ the demos, you'll need:
- .NET Core SDK
- Visual Studio Code
- npm
- Docker
- Postman for testing APIs
Once you have installed the pre-requisites, you can follow along with the demos
Execute the following steps in order to follow along with the first demo.
Fire up Visual Studio Code. Then create and open a new folder in Visual Studio Code.
Open command terminal via Ctrl + ` [Mac: Fn + `]
From the command terminal, execute dotnet new -t Web
Open package.json. Ensure that VS Code runs a restore against package.json
Start debugging the project via F5 [Mac: Fn + F5]
Once done, stop debugging via the Stop button.
Create a new file under root of the project named 'Dockerfile'.
Navigate to DockerHub and find an appropriate image for microsoft/dotnet:onbuild.
Configure the Dockerfile with the image we determined and set environment variables along with ports that we'll need to expose in the Docker host:
FROM microsoft/dotnet:1.0.0-preview2-onbuild
ENV ASPNETCORE_URLS http://*:5000
EXPOSE 5000
Back in the terminal (Ctrl + ` [Mac: Fn + `] if it closed), execute docker build -t dotnetfilenew .
in order to build our Docker image using the Dockerfile.
Examine the build output for informaiton about what Docker is doing. Also execute docker image
and examine the output. You should see the image we just created.
Now execute docker run -i --rm -p 8080:5000 dotnetfilenew
in order to run the sample.
Test this out via a web browser by navigating to http://localhost:8080/
Completed sample lives here: samples/DotNetNew
Execute the following steps in order to follow along with the first demo. This section of the demo leverages YeoMan to generate our files instead of dotnet new
.
Open Visual Studio Code and then open / create a new working directory.
Open the command terminal in Visual Studio Code (Ctrl + ` [Mac: Fn + `])
Install YeoMan via npm: npm install -g yo
Install the Docker Generator via npm: npm install -g generator-docker
Install the ASP.NET Generator via npm: npm install -g generator-aspnet
In the command terminal, run the aspnet generator: yo aspnet
. Select the option to create a "Web API Application". Name it whatever you like.
Browse the files to see what was created.
In the command terminal, navigate to the new folder that was created (cd xyz) and run the aspnet generator: yo docker
. Choose the following options for the prompts:
- Project: .NET Core
- Version: rtm
- Web Server? Y (which is the default if you press [Enter])
- Port: 8080 (or whatever you like)
- Name: (default if fine)
- Service: (default if fine)
- Compose: (default if fine)
- Overwrite Dockerfile? Y (which is the default if you press [Enter])
Move the .vscode folder to the root level.
Open the tasks.json file in the editor. Set the Windows cwd value to ${workspaceRoot}\\WebAPIApplication
and the OSX version to ${workspaceRoot}/WebAPIApplication
.
Open the launch.json file in the editor. Set the pipTransport > pipeCwd value to ${workspaceRoot}/WebAPIApplication
and the windows > pipeCwd value to ${workspaceRoot}\\WebAPIApplication
.
These values were specified so that we can have a basic project directory structure and debug our project (
<root>/WebAPIApplication
) from the root directory.
At some point you should have seen a "Restore" popup. If you didn't see this, or you did not click "Restore" when it was present, navigate within the terminal to the directory that houses our project.json file.
From there, execute dotnet restore
Start the debugger [F5] (Mac: [Fn] + [F5]) and pay close attention to the output.
If you see any errors, you may have missed one of the steps in Step 4
Examine the running containers in docker as well.
Navigate in your browser to http://localhost:8080/api/values
Set a breakpoint somewhere in the ValuesController and test.
Postman can be very useful for debugging RESTful services.
Completed sample lives here: samples/YoAspnetAndYoDocker
The remaining demos presented at Desert Code Camp are not documented herein, but all the source code is available in the samples directory.
Version 1 of the service with no backing state: samples/BoilerPlate
Version 2 of the service with state stored in a file: samples/StatefulFileStorage
Version 3 of the service with state stored in a separate MongoDB container: samples/StatefulMongoStorage