Containers on Windows Server 2016 is one of the newest features on Microsoft’s Server Operating System. Among many other new features, containers offer a way to run application in an isolated environment that is also highly automated for builds and deployments with the new .NET Core command line utilities and the integration that Docker has with Windows Server 2016 and containers. This tutorial walks through the steps on how to go from zero to containerized app in no time.
On Server 2016
- Assuming Server is installed, run a PowerShell terminal. Either launch it from the Start Menu, or if you’re running without the GUI, just type in “powershell”.
- Add the repositories for Docker to the server
Install-Module -Name DockerMsftProvider -Repository PSGallery -Force
- Install the Container Engine and Docker on the server
Install-Package -Name docker -ProviderName DockerMsftProvider
NOTE: – if you get an error about needing to install updates or hotfixes, you can use sconfig from the CLI to install these. Simply type in sconfig and select option 6. After the updates run, try installing the continer engine again.
- Reboot the Server.
- Test Docker. Run a simple Docker command to make sure it is installed and working without any errors.
docker ps -a
Create a Project
Note: These steps in this section can be performed on a Windows Dev Box or the Server itself. If you’re just going through this, you can run these on the server.
- Download Node.JS. Node’s package manager (npm) is needed to install some of the dependencies that are used by the .NET command line.
Invoke-WebRequest -Uri https://nodejs.org/dist/v6.9.2/node-v6.9.2-x64.msi -OutFile node.msi
- Install Node.JS. Node’s package manager (npm) is needed to install some of the dependencies that are used by the .NET command line.
msiexec -i node.msi /quiet
- Download and install .NET Core SDK.
Invoke-WebRequest -Uri https://go.microsoft.com/fwlink/?LinkID=835014 -OutFile DotNetCore.exe
- Install .NET Core.
- Allow about 5 minutes for the node and .NET installers to finish.
- Restart the powershell terminal and install Gulp and Bower. These are used when publishing .NET apps.
npm install -g gulp npm install -g bower
- Create a project folder.
- CD to the project folder.
- Create a new .NET web project.
dotnet new -t Web
- Install all of the dependencies and libaries. Once the restore is done, you can develop on the project and the like. For this tutorial, it isn’t necessary to do anything, but this is the point were you would do develop your application.
- Publish the application. This will create a publish folder. Look at the output for the folder path. It’s usually under the app folder in bin\Debug\netcoreapp1.1\publish
- Change directories to the published folder.
- Create a Dockerfile. Note, this image is using a version of .NET that matches the output of the publish, which happens to match the version of the .NET SDK that was installed earlier.
echo @" FROM microsoft/dotnet:1.1.0-runtime-nanoserver ENTRYPOINT ["dotnet", "my-project.dll"] ENV ASPNETCORE_URLS http://+:80 EXPOSE 80 COPY . . "@ | Set-Content -Encoding utf8 Dockerfile
On Server 2016
- If the files were created on a Dev box other than the server, move the publish folder to the server and CD to that folder. Depending on the level of trust on the network or the location of the server….
- Temporary and secure: Use sconfig to enable an RDP session to the server and use the mapped drive feature in RDP.
- More permenant: Configure WinRM to allow remote control of the server and use Powershell to copy the files
- More permenant, less secure: Create a fileshare on the server and use copy to copy the files
- Create a Docker image.
docker build --tag my-project --no-cache .
- Run the Docker image.
docker run -dit --name my-project -p 80:80 my-project
- Your application is now running inside of a container on Windows! You can get IP address for the computer by typing in:
Look for the containers IP address. This IP is what can be used to access the web site running inside of the container. Luanch a browser and browse to http://[your-server-ip-address].
It’s a little long winded, with a little time and effort, you can be up and running with .NET core apps inside of Windows Containers in no time!