One of my favorite aspects of .NET Core is its ability to run on multiple operating systems (Linux, Mac, Windows). I love being able to run applications on Linux in particular because it is cheaper 🙂 and it allows architectures to easily be containerized.
This is the first post of a two part series on hosting a .NET Core application in a Docker Swarm cluster. In this post, we will be creating a simple ASP.NET Core application, placing it in a Docker container, and pushing it to Docker Hub.
If you haven’t heard of Docker, I highly recommend reading up on it. Docker is one of the most popular containerization platforms in the market.
The first step is getting Docker installed on your machine. You can download the Docker Toolbox from the following link. I am working in a windows environment although, if you are using Mac or Linux the majority of the steps throughout this article will be conceptually the same.
Creating our ASP.NET Core Container
Our application could be created in a number of ways however, a major theme in this post is interoperability. Lets use tools that are available on Windows, Linux, or Mac.
With everything installed, we can get started creating our ASP.NET Core application. The .NET Core SDK provides us with the
dotnet new command. This creates a new project but it is bare-bones. In our case, we want to create an ASP.NET Core Web API. To get a better starting point, there is some extra tooling we can incorporate.
Lets use Yeoman to generate our base application. Yeoman is a great tool for scaffolding many different types of projects. If you haven’t heard of Yeoman you should be started by taking a look at the generators that are available.
First, we download yeoman with NPM.
λ npm install -g yo bower
Then we can download the aspnet yeoman generator. We are currently using version 0.2.6 as this is the latest version that supports the project.json format which we will be using.
λ npm install -g firstname.lastname@example.org
With Yeoman installed, creating our project is very easy.
This leaves us with the following project structure which comes equipped with a dockerfile!
Running the following commands will restore all package dependencies, build the project, and will run the web application.
λ dotnet restore ... λ dotnet build ... λ dotnet run ...
When browsing to http://localhost:5000/api/values we see the following output.
Now to containerize our application. The generated
dockerfile makes this very simple. Creating the container can be done with the following command.
λ docker build -t swarm-demo .
We can now observe our image by executing the
λ docker images REPOSITORY TAG IMAGE ID CREATED SIZE swarm-demo latest 95c6f9d132f2 24 hours ago 667 MB microsoft/dotnet 1.1.0-sdk-projectjson b14a33b603f2 6 days ago 585 MB
With our image built, the container can be triggered with the
λ docker run -p 8000:5000 swarm-demo
Now we can observe the exact same output when browsing to http://localhost:8000/api/values.
Pushing to Docker Hub
We could host our own docker registy but, for convenience we will just push our image to docker hub. Docker hub is a cloud-based registry which can be used to house both public and private Docker repositories. Here you can find TONS of containers such as nginx, redis, mongodb, or registry. Registy is a fun one because it is itself a Docker registry. Inception?
All official Docker repositories will be listed with the repository name. Unofficial ones must be prefixed with the owner’s account name. Lets tag our repository and prefix it with our account.
λ docker tag 95c6f9d132f2 jrob5756/swarm-demo:latest
Now if we execute the
images command again, we will find a new docker image.
This image is the same as the previous one however, it is prefixed with jrob5756. Next lets login with this account.
λ docker login Username: jrob5756 Password: Login Succeeded
We can now push our image to docker hub!!! This is done with, you guessed it, the
push command. After the push completes, we can view our image on docker hub.
λ docker push jrob5756/swarm-demo
In our next post, we will be picking up where we left off by taking our container and running it in a highly available Swarm cluster. Stay tuned to see all the fun things we can do with Docker!