With the advent of Microsoft embracing Docker; it’s now possible to release .NET Core apps in Docker containers; and it’s a first class citizen. This means instead of creating custom Docker images, Microsoft has released multiple docker images you can use instead.
The cool thing about these Docker images is that their Dockerfiles are on Github, which is quite amazing if you like to create custom Docker images. Without more ado, here’s how I set up the project’s Dockerfile, and I created a
build.sh file so that I could script this repeatedly.
COPY $source .
ENTRYPOINT [“dotnet”, “MyProject.dll”]
Let’s take it line by line:
FROM microsoft/dotnet:1.1.0-runtime says to create a new image Microsoft’s dockerhub against the
dotnet repository, against the tag named
ARG source=./src/bin/Release/netcoreapp1.1/publish says to create a variable called
source that has the path of
./src/bin/Release/netcoreapp1.1/publish (the default publish directory in .NET Core 1.1). This path is relative to the
WORKDIR /app means to create a directory in the docker container and make it the working directory.
COPY $source . says to copy the files located at the
/app, since that directory was previously defined as the working directory.
EXPOSE 5000 tells docker to expose that port in the container so that it’s accessible from the host.
ENTRYPOINT ["dotnet", "MyProject.dll"] says the entrypoint for the container is the command:
This would be the same as:
CMD "dotnet MyProject.dll"
So that’s the docker file, but there are a few other steps to get a running container; first you have to make sure you’re running ASP.NET Core applications against something other than localhost, and then you still have to publish the application, create the docker image, and run the docker container based on that image. I created a
build.sh file to do that, but you could just as easily do it with PowerShell:
SERVICE="my-project" # change directory to location of project.json pushd ./src # run dotnet publish, specify release build dotnet publish -c Release # equivalent to cd .. (go back to previous directory) popd # Create a docker image tagged with the name of the project:latest docker build -t "$SERVICE":latest . # Check to see if this container exists. CONTAINER=`docker ps --all | grep "$SERVICE"` # if it doesn't, then just run this. if [ -z "$CONTAINER" ]; then docker run -i -p 8000:5000 --name $SERVICE -t $SERVICE:latest # if it does exist; nuke it and then run the new one else docker rm $SERVICE docker run -i -p 8000:5000 --name $SERVICE -t $SERVICE:latest fi
My ASP.NET Core directory structure is set up as follows:
MyProjectDirectory | - src \ - project.json - //snip.. - tests - Dockerfile - build.sh - build.ps1
This let’s me keep the files I care about (buildwise) as the base of the directory; so that I can have a master bootstrap file call each directory’s build files depending on the environment. You may want to mix these, but this also allows me to keep certain files out side of Visual Studio (I don’t want it to track or care about those files).
Then, all I have to do to build and deploy my ASP.NET Core 1.1 application is to run:
And it’ll then build, deploy, change the container if needbe, and start the container.
One thought on “How do I publish a .NET Core 1.1 application in a Docker container?”
can you provide an example for multi project solution. My dot net core 1.1 web application does not have project.json file.