Here's a simple example for getting an ASP.NET Core project running in a Docker container.
This was created to teach the core concepts of Docker.
Definitions:
- Dockerfile: A manifest file that describes your Docker Image.
- Docker Image: The output of a build of your Dockerfile. Use
docker build
command to create a Docker Image. - Docker Container: An runtime instance of your Docker Image. Use
docker run
to start a new container instance.
- Install .NET Core SDK: https://www.microsoft.com/net/core#windowscmd
- Install Docker: https://docs.docker.com/engine/installation/
- If you are running Windows, switch to Windows containers.
To just run the sample:
- Clone this repo
- Run
build.cmd
in the root.
The
build.cmd
file will build the aspnetcore project, copy the output to ./publish folder, build the Docker Image and run the Docker Container.
- Copy and Paste the outputted IP address into a browser.
You will see "Hello World" - an ASP.NET Core site hosted in a container!
Follow these instructions to create this sample from scratch.
Open a command prompt and execute the following:
mkdir aspnetcorecontainer
dotnet new web
dotnet restore
dotnet run
This will use the aspnetcore image and copy the files from publish to the /app directory and then configure the dotnet entrypoint to the dll.
FROM microsoft/aspnetcore
WORKDIR /app
COPY ./publish .
ENTRYPOINT ["dotnet", "aspnetcorecontainer.dll"]
dotnet build
dotnet publish -o ./publish
docker build -t aspnetcore-image .
docker run -d -p 8000:80 --name aspnetcore-container aspnetcore-image
docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" aspnetcore-container
Load the IP address returned in the previous step in a browser
To show that the container is running in an isolated space, we are going to see the process count difference between the container and the host machine.
docker exec -ti aspnetcore-container powershell
From the container's PowerShell prompt:
(Get-Process).Count
If you are in cmd, run the following:
powershell (Get-Process).Count
If you are in a PS prompt, run the following:
(Get-Process).Count
Execute the following command to remove the container before you re-run these steps.
docker rm /aspnetcore-container -f